<?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\ChoiceType;
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\PurchaseOrderBreakdownItemEntity;
use RxConnexionBundle\Entity\PurchaseOrderBreakdownItemDeliveryEntity;

class PurchaseOrderBreakdownItemDeliveryForm 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('purchaseOrderBreakdownItem', HiddenType::class, array(
                'mapped' => false,
                'data' => $options['purchaseOrderBreakdownItem_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('invoice_no', TextType::class, array(
                'label' => 'Invoice No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('dr_no', TextType::class, array(
                'label' => 'DR No.',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('payment_method', ChoiceType::class, array(
                'label' => 'Payment Method',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'choices' => array(
                    'Cash' => 'Cash',
                    'Check' => 'Check',
                    'Credit' => 'Credit'
                )
            ))
            ->add('bank', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($options) {

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

                $form
                    ->add('quantity', TextType::class, array(
                        'label' => 'Quantity',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'quantity '. ($options['isItemised'] ? 'itemised_quantity' : ''),
                            'readonly' => $options['isItemised'],
                            'data-key' => 0
                        ),
                        'data' => $data ? intval($data->getQuantity()) : ''
                    ))
                    ->add('bank_description', TextType::class, array(
                        'mapped' => false,
                        'label' => 'Bank',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'data' => $data && $data->getBank() ? $data->getBank()->getDescription() : ''
                    ))
                    ->add('invoiceDate', TextType::class, array(
                        'label' => 'Invoice Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'placeholder' => 'MM/DD/YYYY',
                            'class' => 'date'
                        ),
                        'data' => $data && $data->getInvoiceDate() ? $data->getInvoiceDateAsString() : ''
                    ));
            });
        $builder->get('bank')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:BankEntity', false));
    }

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

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