<?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\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Entity\TransferInventoryItemEntity;
use RxConnexionBundle\Entity\TransferInventoryItemDeliveryEntity;

class TransferInventoryItemDeliveryForm 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('transferInventoryItem', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['transferInventoryItem_id']
                )
            ))
            ->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('quantity', TextType::class, array(
                'label' => 'Quantity',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'attr' => array(
                    'class' => 'amount'
                )
            ))
            ->add('location', EntityType::class, array(
                'class' => 'RxConnexionBundle:LocationEntity',
                'label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {
                    return $er->createQueryBuilder('l')
                        ->where('l.healthFacility = :branchId')
                        ->setParameter('branchId', $options['branchId']);
                },
                'required' => true,
                'label' => 'Location',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
            ))
            ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {

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

                $form
                    ->add('receive_date', TextType::class, array(
                        'label' => 'Receive Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getReceiveDateAsString() : ''
                    ));
            });

        $builder->get('transferInventoryItem')->addModelTransformer(new DataTransformer($this->manager,'RxConnexionBundle:TransferInventoryItemEntity', true));
    }

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

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