<?php

namespace App\Form;

use App\Entity\Tag;
use App\Entity\Article;
use App\Entity\Categorie;
use App\Repository\TagRepository;
use App\Repository\CategorieRepository;
use Symfony\Component\Form\AbstractType;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('titre',TextType::class,[
                'label' => 'Titre',
                'attr'=> [
                    'class' => 'form-control'
                ]
            ])
            ->add('contenu', CKEditorType::class, [
            'label' => 'Contenu',
            'attr' => [
                'id' => 'editor',
                'class' => 'form-control',
                'rows' => 5, // Nombre de lignes visibles
                'placeholder' => 'Saisissez votre texte ici...'
                ]
            ])
           ->add('datePublication', DateTimeType::class, [
                'widget' => 'single_text',
                'required' => false,
                'html5' => true, // ← Changé à true
                'input' => 'datetime_immutable',
                'attr' => [
                    'class' => 'form-control'
                ]
            ])
       
            ->add('categorie', EntityType::class, [
                'label'=>'Catégorie',
                'attr'=> [
                    'class' => 'form-select'
                ],
                'class' => Categorie::class,
                'choice_label' => function (Categorie $categorie) { return $categorie->getNom() ;},
                'query_builder' => function (CategorieRepository $er) {
                                return $er->createQueryBuilder('c')
                                ->orderBy('c.nom', 'ASC'); // Tri par code ASC
                }
            ])
            ->add('tag', EntityType::class, [
                'attr'=> [
                    'class' => 'form-select'
                ],
                'class' => Tag::class,
                'choice_label' => function (Tag $tag) { return $tag->getNom() ;},
                'query_builder' => function (TagRepository $tag) {
                    return $tag->createQueryBuilder('t')
                    ->orderBy('t.nom', 'ASC');
                },
                'multiple' => true,
                'required' => false
            ])
            ->add('published',CheckboxType::class,[
                'label' => 'Publier ',
                'required' => false,
                'attr' => [
                    'class' => 'form-check-input', // Classe pour l'input
                ],
                'row_attr' => [
                    'class' => 'form-check form-switch form-check-reverse mb-3' // Reverse pour aligner à droite
                ]
            ])
        ;
    }

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