<?php

namespace Lc\ShopBundle\Form\Backend\Merchant;

use App\Entity\MerchantConfig;
use CKSource\Bundle\CKFinderBundle\Form\Type\CKFinderFileChooserType;
use Doctrine\Common\Collections\ArrayCollection;
use Lc\ShopBundle\Context\MerchantConfigInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Vich\UploaderBundle\Form\Type\VichImageType;

class MerchantConfigsType extends AbstractType
{
        protected $em;

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

        public function buildForm(FormBuilderInterface $builder, array $options)
        {

                $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {

                        $builder = $event->getForm()->getParent();

                        // Reindex collection using id
                        $indexedCollection = new ArrayCollection();
                        foreach ($event->getData() as $collectionItem) {
                                $indexedCollection->set($collectionItem->getName(), $collectionItem);
                        }


                        $builder->add('merchantConfigs', CollectionType::class, [
                                'translation_domain' => 'lcshop',
                                'entry_type'=> MerchantConfigType::class,
                                'data'=>$indexedCollection,
                                'required' => false,
                                'allow_add'=> false,
                                'allow_delete'=>false,
                                'by_reference'=>false
                        ]);


                });

        }

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

