<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\Entity;
use RxConnexionBundle\Entity\AccountUserEntity;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
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\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Entity\MaterialRequisitionEntity;

class MaterialRequisitionForm 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('reference_no', TextType::class, array(
                'label' => 'Reference No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('title', TextType::class, array(
                'label' => 'Title',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('specific_purpose', TextareaType::class, array(
                'label' => 'Specific Purpose',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
//            ->add('canvass_type', ChoiceType::class, array(
//                'label' => 'Canvas Type',
//                'label_attr' => array(
//                    'class' => 'required middle'
//                ),
//                'required' => true,
//                'choices' => array(
//                    'Main' => 'Main',
//                    'Branch' => 'Branch'
//                ),
//                'data' => 'Branch'
//            ))
            ->add('healthFacility', HiddenType::class)
            ->add('purpose', HiddenType::class)
            ->add('account_user', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $healthFacility = $data->getHealthFacility();
                $purpose = $data->getPurpose();

                $form
                    ->add('request_date', TextType::class, array(
                        'label' => 'Request Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'placeholder' => 'MM/DD/YYYY',
                            'class' => 'date'
                        ),
                        'data' => $data && $data->getRequestDate() ? $data->getRequestDateAsString() : date('m/d/Y')
                    ))
                    ->add('required_date', TextType::class, array(
                        'label' => 'Required Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'placeholder' => 'MM/DD/YYYY',
                            'class' => 'date'
                        ),
                        'data' => $data && $data->getRequiredDate() ? $data->getRequiredDateAsString() : date('m/d/Y')
                    ))
                    ->add('healthFacility_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Branch',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'data' => $healthFacility ? $healthFacility->getDescription() : ''
                    ))
                    ->add('purpose_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Purpose',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'data' => $purpose ? $purpose->getDescription() : ''
                    ))
                    ->add('account_user_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'User',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'data' => $data && $data->getAccountUser() ? $data->getAccountUser()->getFullName() : ''
                    ));

            });
        $builder->get('healthFacility')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:HealthFacilityEntity', false));
        $builder->get('purpose')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PurposeEntity', false));
        $builder->get('account_user')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
    }

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

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