<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\ItemSubCategoryEntity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;


class ItemForm extends AbstractType
{
    private $manager;

    public function __construct(ObjectManager $manager) {
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('action', HiddenType::class, array(
                'mapped' => false,
                'attr' => array(
                    'class' => 'form-action'
                )
            ))
            ->add('id', HiddenType::class)
            ->add('code', TextType::class, array(
                'label' => 'Code',
                'required' => false
            ))
            ->add('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            ->add('type', ChoiceType::class, array(
                'label' => 'Type',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'choices' => [
                    'type1' => 'Type 1',
                    'type2' => 'Type 2',
                    'type3' => 'Type 3',
                    'type4' => 'Type 4'
                ]
            ))

            ->add('itemSubCategory', EntityType::class, array(
                'class' => ItemSubCategoryEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'SubCategory',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))
            ->add('item_company', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $itemCompany = $data->getItemCompany();

                $form
                    ->add('start_date', TextType::class, array(
                        'label' => 'Start Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getStartDateAsString() : ''
                    ))
                    ->add('end_date', TextType::class, array(
                        'label' => 'End Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getEndDateAsString() : ''
                    ))
                    ->add('item_company_desc', TextType::class, array(
                        'label' => 'Company',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $itemCompany ? $itemCompany->getDescription() : ''
                    ));
            });

        $builder->get('item_company')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CompanyEntity', false));
    }

    public function getBlockPrefix()
    {
        return 'item';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' 	  => 'RxConnexionBundle\Entity\ItemEntity',
            'csrf_protection' => true,
            'csrf_field_name' => '_token',
            // a unique key to help generate the secret token
            'intention'       => 'itemEntity_intention',
            'action'          => 'n'
        ));
    }
}