<?php

namespace App\Form\BackendAdmin;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

use Symfony\Component\Validator\Constraints\File;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;

use FOS\CKEditorBundle\Form\Type\CKEditorType;

use App\Entity\Annonce;
use App\Entity\Societe;
use App\Entity\Categorie;

use App\Repository\SocieteRepository;
use App\Repository\CategorieRepository;

class AnnonceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('titre')
            ->add('description', CKEditorType::class)
            ->add('prix_marche')
            ->add('prix_promo')
            ->add('pourcentage_reduction')
            ->add('largeur', ChoiceType::class, array(
                'label' => 'Liste des groupes',
                'required' => true,
                'placeholder' => "Sélectionnez la dimension d'annonce",
                'choices'  => [
                    '1/4   (438 * 240) pixels' => 1,
                    '2/4   (876 * 240) pixels' => 2,
                    '3/4  (1314 * 240) pixels' => 3,
                    '4/4  (1752 * 240) pixels' => 4,
                ],
            )) 

            ->add('societe', EntityType::class, array(
                'label' => 'availability.host',
                'class' => Societe::class,
                'placeholder' => 'Sélectionnez une Société',
                'required' => true,
                'query_builder' => function (SocieteRepository $er){
                    return $er->createQueryBuilder('S')
                                ->orderBy('S.id', 'DESC');
                },

                'choice_label' => function (Societe $societe) {
                    return $societe->getId() . ' ' . $societe->getNom();
                }//,'attr' => ['class' => 'select-filter select2bs4 select-filter-speaker'],
            ))
            ->add('categorie', EntityType::class, array(
                'label' => 'Liste des groupes',
                'class' => Categorie::class,
                'required' => true,
                'placeholder' => 'Sélectionnez une catégorie',
                'query_builder' => function (CategorieRepository $er) {
                    return $er->createQueryBuilder('u')
                              ->orderBy('u.nom', 'ASC');
                },
                'choice_label' => function (Categorie $categorie) {
                    return $categorie->getId() . ' - ' . $categorie->getNom();
                }
            ))
            ->add("save", SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Annonce::class,
        ]);
    }
}
