<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\PatientChartPaymentEntity;
use RxConnexionBundle\Form\Type\PatientChartPaymentOtherProcedureForm;
use RxConnexionBundle\Form\Type\PatientChartPaymentProcedureForm;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;

class PatientChartPaymentForm 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('patient_chart', HiddenType::class, array(
                'attr' => array(
                    'value' => $options['patientChart_id']
                )
            ))
            ->add('id', HiddenType::class)
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'attr' => array(
                    'rows' => 5
                )
            ))
            ->add('or_no', TextType::class, array(
                'label' => 'OR #',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('dr_no', TextType::class, array(
                'label' => 'DR #',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('barcode', TextType::class, array(
                'label' => 'Barcode',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('check_no', TextType::class, array(
                'label' => 'Check #',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false
            ))
            ->add('payment_type', ChoiceType::class, array(
                'label' => 'Payment Type',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'choices' => array(
                    PatientChartPaymentEntity::PAYMENT_TYPE_CASH => 'Cash',
                    PatientChartPaymentEntity::PAYMENT_TYPE_CHECK => 'Check',
                )
            ))
            ->add('amount', TextType::class, array(
                'label' => 'Amount',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true,
                'attr' => array(
                    'class' => 'amount'
                ),

            ))
            ->add('procedures', CollectionType::class, array(
                'entry_type' => PatientChartPaymentProcedureForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false,
                'required' => false,
                'delete_empty' => function($patientChartPaymentProcedure = null){
                        return !$patientChartPaymentProcedure->getPatientChartProcedure();
                }

            ))
            ->add('otherProcedures', CollectionType::class, array(
                'entry_type' => PatientChartPaymentOtherProcedureForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false,
                'required' => false,
                'delete_empty' => function($patientChartPaymentOtherProcedure = null){
                    return !$patientChartPaymentOtherProcedure->getPatientChartOtherProcedure();
                }

            ))
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

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

                $form
                    ->add('payment_date', TextType::class, array(
                        'label' => 'Payment Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getPaymentDateAsString() : ''
                    ))
                    ->add('check_date', TextType::class, array(
                        'label' => 'Check Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getCheckDateAsString() : ''
                    ));
            });

        $builder->get('patient_chart')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:PatientChartEntity', true));
    }

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

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