<?php

namespace App\Form\Blog;

use App\Entity\Blog\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
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 Symfony\Component\Validator\Constraints\NotBlank;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class CategoryType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'title',
            TextType::class,
            [
                'label' => 'Titre',
                'required' => true,
                'attr' => [
                ],
                'constraints' => [
                    new NotBlank(),
                ],
            ]
        );

        $builder->add(
            'parentCategory',
            EntityType::class,
            [
                'label' => 'Catégorie parente',
                'class' => Category::class,
                'choice_label' => 'title',
                'required' => false,
                'multiple' => false,
                'attr' => [
                    'data-jschoice' => '',
                ],
                'query_builder' => function (EntityRepository $repo) {
                    return $repo->createQueryBuilder('a')
                        ->orderBy('a.title', 'ASC')
                    ;
                },
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'subTitle',
            TextareaType::class,
            [
                'label' => 'Sous-titre',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'description',
            TextareaType::class,
            [
                'label' => 'Description',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'slug',
            TextType::class,
            [
                'label' => 'Slug',
                'required' => false,
                'help' => 'Laisser vide pour une génération automatique',
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'isActive',
            CheckboxType::class,
            [
                'label' => 'Activée',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );
    }

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