<?php

namespace App\Form;

use App\Entity\Page;
use App\Entity\CategoryPage;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use FOS\CKEditorBundle\Form\Type\CKEditorType;

class PageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('description', CKEditorType::class)
            ->add('active')
            ->add('alias')
            ->add('category', EntityType::class, [
                'class' => CategoryPage::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er
                        ->createQueryBuilder('c')
                        ->where('c.active in(:a)')
                        ->setParameter('a', true)
                        ->orderBy('c.id', 'ASC');
                },
                'choice_label' => 'name',
                'choice_value' => 'id',
                'mapped' => false,
                'multiple' => false, 
                'empty_data' => [],
                'required' => false
            ])
        ;
    }

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