<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\AnneeScolaire;
use App\Entity\Ecole;
use App\Entity\Section;
use Doctrine\ORM\EntityRepository;
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\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class AnneeScolaireType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        /** @var Ecole $ecole */ $ecole = $options['ecole'];
        $builder
            ->add('libelle', TextType::class, ['label' => 'Libellé', 'attr' => ['placeholder' => 'Année 2026']])
            ->add('section', EntityType::class, [
                'class' => Section::class, 'choice_label' => 'nom', 'label' => 'Section', 'required' => false, 'placeholder' => 'Toutes les sections',
                'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('s')->andWhere('s.ecole = :ecole')->setParameter('ecole', $ecole)->orderBy('s.ordre', 'ASC'),
            ])
            ->add('dateDebut', DateType::class, ['label' => 'Date début', 'required' => false, 'widget' => 'single_text'])
            ->add('dateFin', DateType::class, ['label' => 'Date fin', 'required' => false, 'widget' => 'single_text'])
            ->add('statut', ChoiceType::class, ['label' => 'Statut', 'choices' => ['Brouillon' => 'BROUILLON', 'Active' => 'ACTIVE', 'Clôturée' => 'CLOTUREE']])
            ->add('active', CheckboxType::class, ['label' => 'Année active', 'required' => false]);
    }
    public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults(['data_class' => AnneeScolaire::class, 'ecole' => null]); $resolver->setRequired('ecole'); $resolver->setAllowedTypes('ecole', [Ecole::class]); }
}
