<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\Ecole;
use App\Entity\Niveau;
use App\Entity\Section;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Doctrine\ORM\EntityRepository;

final class NiveauType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        /** @var Ecole $ecole */ $ecole = $options['ecole'];
        $builder
            ->add('nom', TextType::class, ['label' => 'Nom du niveau'])
            ->add('slug', TextType::class, ['label' => 'Slug', 'attr' => ['placeholder' => 'qaida-nouraniyya']])
            ->add('section', EntityType::class, [
                'class' => Section::class,
                'choice_label' => 'nom',
                'label' => 'Section',
                'placeholder' => 'Toutes les sections',
                'required' => false,
                'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('s')->andWhere('s.ecole = :ecole')->setParameter('ecole', $ecole)->orderBy('s.ordre', 'ASC'),
            ])
            ->add('description', TextareaType::class, ['label' => 'Description', 'required' => false, 'attr' => ['rows' => 4]])
            ->add('ordre', IntegerType::class, ['label' => 'Ordre', 'attr' => ['min' => 0]])
            ->add('active', CheckboxType::class, ['label' => 'Niveau actif', 'required' => false]);
    }
    public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults(['data_class' => Niveau::class, 'ecole' => null]); $resolver->setRequired('ecole'); $resolver->setAllowedTypes('ecole', [Ecole::class]); }
}
