<?php

namespace Lc\ShopBundle\Form\Backend\ReductionCatalog;

use Doctrine\ORM\EntityManagerInterface;

use Lc\ShopBundle\Context\ProductCategoryInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductCategoriesFilterType extends AbstractType
{
        protected $em;

        public function __construct(EntityManagerInterface $entityManager)
        {
                $this->em = $entityManager;
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
                $class = $this->em->getClassMetadata(ProductCategoryInterface::class);

                $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($class) {
                        $builder = $event->getForm()->getParent();

                        $builder->add('productCategoriesActive', CheckboxType::class, array(
                                'label' => 'field.Reduction.productCategoriesActive',
                                'translation_domain'=>'lcshop',
                                'mapped' => false,
                                'required'=>false,
                                'attr' => ['class' => 'big']
                        ));
                        $builder->add('productCategories', EntityType::class, [
                                'class' => $class->name,
                                'required' => false,
                                'expanded' => false,
                                'multiple' => true,
                                'translation_domain'=>'lcshop',
                        ]);
                });
        }

        public function configureOptions(OptionsResolver $resolver)
        {
                $resolver->setDefaults([
                //        'translation_domain' => 'lcshop',
                ]);
        }
}
