<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
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\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Entity\SupplierEntity;

class SupplierForm 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('code', TextType::class, array(
                'label' => 'Code',
                'label_attr' => array(
                    'class' => 'middle'
                ),
            ))
            ->add('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            ->add('mobile_no', TextType::class, array(
                'label' => 'Mobile No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('phone_no', TextType::class, array(
                'label' => 'Phone No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('email', EmailType::class, array(
                'label' => 'Email',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('fax', TextType::class, array(
                'label' => 'Fax',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('contact_person', TextType::class, array(
                'label' => 'Contact Person',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('street', TextType::class, array(
                'label' => 'Street',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('tin_no', TextType::class, array(
                'label' => 'Tin #',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'mapped' => false,
                'required' => false
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('payment_type', ChoiceType::class, array(
                'label' => 'Payment Type',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'choices' => array(
                    'Cash' => 'Cash',
                    'Check' => 'Check',
                    'Credit' => 'Credit'
                )
            ))
            ->add('payment_term', TextType::class, array(
                'label' => 'Payment Term',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'class' => 'quantity'
                )
            ))
            ->add('city', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

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

                $form
                    ->add('city_description', TextType::class, array(
                        'label' => 'City',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'mapped' => false,
                        'required' => false,
                        'data' => $city ? $city->getDescription() : ''
                    ))
                    ->add('start_date', TextType::class, array(
                        'label' => 'Start Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        '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() : ''
                    ));
            });
        $builder->get('city')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CityEntity', false));
    }

    public function getName()
    {
        return 'supplier';
    }

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