<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\Entity;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Entity\TransferInventoryEntity;

class TransferInventoryForm 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('created_by', HiddenType::class)
            ->add('created_at', HiddenType::class)
            ->add('updated_by', HiddenType::class)
            ->add('updated_at', HiddenType::class)
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'required' => false,
                'attr' => array(
                    'rows' => 4
                )
            ))
            /*->add('branchFrom', 'entity', array(
                'class' => 'ZapconBundle:BranchEntity',
                'property' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {
                    return $er->createQueryBuilder('b')
                        //->where('b.id IN (:branchIds)')
                        //->setParameter('branchIds', $options['user_branchIds'])
                        ->orderBy('b.description');
                },
                'label' => 'Origin Branch',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            ->add('branchTo', 'entity', array(
                'class' => 'ZapconBundle:BranchEntity',
                'property' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {
                    return $er->createQueryBuilder('b')
                        ->where('b.id IN (:branchIds)')
                        ->setParameter('branchIds', $options['user_branchIds'])
                        ->orderBy('b.description');
                },
                'label' => 'Destination Branch',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ));*/
            ->add('branchFrom', HiddenType::class)
            ->add('branchTo', HiddenType::class)
            ->add('locationFrom', HiddenType::class)
            ->add('locationTo', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $branchFrom = $data->getBranchFrom();
                $branchTo = $data->getBranchTo();
                $locationFrom = $data->getLocationFrom();
                $locationTo = $data->getLocationTo();

                $form
                    ->add('branchFrom_description', TextType::class, array(
                        'mapped' => false,
                        //'required' => false,
                        'label' => 'Origin',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'attr' => array(
                            'placeholder' => 'Branch'
                        ),
                        'data' => $branchFrom ? $branchFrom->getDescription() : '',
                    ))
                    ->add('locationFrom_description', TextType::class, array(
                        'mapped' => false,
                        'required' => false,
                        'label' => 'Location',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'attr' => array(
                            'placeholder' => 'Location'
                        ),
                        'data' => $locationFrom ? $locationFrom->getDescription() : '',
                    ))
                    ->add('branchTo_description', TextType::class, array(
                        'mapped' => false,
                        //'required' => false,
                        'label' => 'Destination',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'attr' => array(
                            'placeholder' => 'Branch'
                        ),
                        'data' => $branchTo ? $branchTo->getDescription() : '',
                    ))
                    ->add('locationTo_description', TextType::class, array(
                        'mapped' => false,
                        'required' => false,
                        'label' => 'Location',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'attr' => array(
                            'placeholder' => 'Location'
                        ),
                        'data' => $locationTo ? $locationTo->getDescription() : '',
                    ))
                    ->add('transferDate', TextType::class, array(
                        'label' => 'Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'placeholder' => 'MM/DD/YYYY',
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getTransferDateAsString() : ''
                    ));
            });

        $builder->get('branchFrom')->addModelTransformer(new DataTransformer($this->manager,'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('branchTo')->addModelTransformer(new DataTransformer($this->manager,'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('locationFrom')->addModelTransformer(new DataTransformer($this->manager,'RxConnexionBundle:LocationEntity', false));
        $builder->get('locationTo')->addModelTransformer(new DataTransformer($this->manager,'RxConnexionBundle:LocationEntity', false));
    }

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

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