<?php

namespace Lc\ShopBundle\Form\Backend\ReductionCommon;

use Doctrine\ORM\EntityManagerInterface;

use Doctrine\ORM\EntityRepository;
use Lc\ShopBundle\Context\ReductionCatalogInterface;
use Lc\ShopBundle\Context\UserInterface;
use Lc\ShopBundle\Repository\GroupUserRepository;
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 UsersFilterType extends AbstractType
{
        protected $em;

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

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

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

                        $builder->add('usersActive', CheckboxType::class, array(
                                'label' => 'field.Reduction.usersActive',
                                'mapped' => false,
                                'translation_domain'=>'lcshop',

                                'required'=>false,
                                'attr' => ['class' => 'big']
                        ));
                        $builder->add('users', EntityType::class, [
                                'class' => $userClass->name,
                                'required' => false,
                                'query_builder'=> function (EntityRepository $er) {
                                        $query = $er->findByMerchantQuery()
                                                ->leftJoin('e.groupUsers', 'groupUsers')->addSelect('groupUsers')
                                                ->leftJoin('e.supplier', 'supplier')->addSelect('supplier')
                                        ;
                                        return $query;
                                },
                                'expanded' => false,
                                'multiple' => true,
                                'translation_domain'=>'lcshop',
                                'choice_attr' => function ($choice, $key, $value) {
                                        $data = array();
                                        foreach ($choice->getGroupUsers() as $groupUser) {
                                                $data[] = '_'.$groupUser->getId().'_';
                                        }
                                        return ['data-group-users' => json_encode($data)];
                                }
                        ]);
                });
        }

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