<?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\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\PurchaseInvoicePaymentEntity;

class PurchaseInvoiceOtherCostForm 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('purchaseInvoice', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['purchaseInvoice_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('description', TextType::class, array(
		        'label' => 'Other Cost',
		        'label_attr' => array(
			        'class' => 'required middle'
		        ),
	        ))
	        ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($options) {

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

		        $form
			        ->add('amount', TextType::class, array(
				        'label' => 'Amount',
				        'label_attr' => array(
					        'class' => 'required middle'
				        ),
				        'required' => true,
				        'attr' => array(
					        'class' => 'amount',
				        ),
				        'data' => $data && $data->getAmount() ? round($data->getAmount(), 2) : ''
			        ));
	        });

        $builder->get('purchaseInvoice')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PurchaseInvoiceEntity', true));

    }

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

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