<?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\Bridge\Doctrine\Form\Type\EntityType;

use App\Repository\GroupeCategorieRepository;

use App\Entity\Categorie;
use App\Entity\GroupeCategorie;

class CategorieType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('nom')
            ->add('groupeCategorie', EntityType::class, array(
                'label' => 'Liste des groupes',
                'class' => GroupeCategorie::class,
                'required' => false,
                'placeholder' => 'Sélectionnez un groupe',
                'query_builder' => function (GroupeCategorieRepository $er) {
                    return $er->createQueryBuilder('u')
                              ->orderBy('u.nom', 'ASC');
                },
                'choice_label' => function (GroupeCategorie $group) {
                    return $group->getId() . ' - ' . $group->getNom();
                }
            ))
            ->add("save", SubmitType::class)
        ;
    }

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