<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\AccountingBillingEntity;
use RxConnexionBundle\Entity\PatientBillingEntity;
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\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Validator\Constraints\Choice;


class AccountingBillingForm 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('type', ChoiceType::class, array(
                'label' => 'Type',
                'label_attr' => array(
                    'class' => 'middle required'
                ),
                'required' => true,
                'choices' => array(
                    '' => '',
                    AccountingBillingEntity::TYPE_INVOICE  => 'Invoice',
                    AccountingBillingEntity::TYPE_PAYMENT => 'Payment',
                    AccountingBillingEntity::TYPE_REFUND => 'Refund',
                ),
                'attr' => array(
                    'class' => ''
                )
            ))
            ->add('status', TextType::class, array(
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => '',
                ),
                'required' => false,
            ))
            ->add('amount', NumberType::class, array(
                'label' => 'Amount',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'attr' => array(
                    'class' => '',
                ),
                'required' => false,
            ))
            ->add('patientBilling', HiddenType::class)
            ->add('patientPayment', HiddenType::class)
            ->add('patientBillingRefund', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $patientBilling = $data->getPatientBilling();
                $patientPayment = $data->getPatientPayment();
                $patientBillingRefund = $data->getPatientBillingRefund();

                $form
                    ->add('encodeDate', TextType::class, array(
                        'label' => 'Encode Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getEncodeDateAsString() : ''
                    ))
                    ->add('checkDate', TextType::class, array(
                        'label' => 'Check Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getCheckDateAsString() : ''
                    ))
                    ->add('patientBillingInvoiceNumber', TextType::class, array(
                        'label' => 'Invoice No',
                        'mapped' => false,
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => ''
                        ),
                        'data' => $patientBilling ? $patientBilling->getInvoiceNo() : ''
                    ))
                    ->add('patientPaymentNumber', TextType::class, array(
                        'label' => 'Payment (Invoice No.)',
                        'mapped' => false,
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => ''
                        ),
                        'data' => $patientPayment ? $patientPayment->getId() : ''
                    ))
                    ->add('patientBillingRefundNumber', TextType::class, array(
                        'label' => 'Refund (Invoice No.)',
                        'mapped' => false,
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => ''
                        ),
                        'data' => $patientBillingRefund ? $patientBillingRefund->getId() : ''
                    ));

            });
        $builder->get('patientBilling')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientBillingEntity', false));
        $builder->get('patientPayment')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientPaymentEntity', false));
        $builder->get('patientBillingRefund')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientBillingRefundEntity', false));
    }

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

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