<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\AccountUserEntity;
use RxConnexionBundle\Entity\ChartStatusEntity;
use RxConnexionBundle\Entity\ChartTypeEntity;
use RxConnexionBundle\Entity\InternalStatusEntity;
use RxConnexionBundle\Entity\PatientNoteEntity;
use RxConnexionBundle\Entity\TicketSeverityEntity;
use RxConnexionBundle\Entity\TicketStatusEntity;
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\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 PatientChartUpdateIsDoneForm 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', TextType::class, array(
                'label' => 'Linked Encounter ID',
                'attr' => array(
                    'readonly' => 'true'
                )
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Note',
                'mapped' => false,
                'required' => false,
                'attr' => array(
                    'rows' => 5
                )
            ))
            ->add('accountUserMoaAssignment', EntityType::class, array(
                'class' => AccountUserEntity::class,
                'choice_label' => 'fullName',
                'query_builder' => function(EntityRepository $er) {

                    return $er->createQueryBuilder('au')
                        ->leftJoin('au.accountUserType', 'aut')
                        ->where("aut.description IN ('MOA', 'MOA Manager')")
                        ->andWhere("au.isActive = 1")
                        ->orderBy('au.fullName', 'DESC');
                },
                'label' => 'Assign To',
                'required' => false
            ))
            ->add('internal_status', EntityType::class, array(
                'class' => InternalStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) {

                    return $er->createQueryBuilder('it')
                        ->where('it.isActive = 1')
                        ->orderBy('it.description', 'ASC');
                },
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => false
            ))
            ->add('ticketUserAssignment', EntityType::class, array(
                'class' => AccountUserEntity::class,
                'choice_label' => 'fullName',
                'query_builder' => function(EntityRepository $er) {

                    return $er->createQueryBuilder('au')
                        ->leftJoin('au.accountUserType', 'aut')
                        ->where("au.isActive = 1")
                        ->orderBy('au.fullName', 'DESC');
                },
                'label' => 'Endorsed To',
                'label_attr' => array(
                    'class' => 'required middle hide endorsed-remove'
                ),
                'required' => false,
                'attr' => array(
                    'class' => 'hide endorsed-remove'
                ),
                'mapped' => false
            ))
            ->add('ticketSeverity', EntityType::class, array(
                'class' => TicketSeverityEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) {

                    return $er->createQueryBuilder('au')
                        ->where('au.isDeleted = 0')
                        ->orderBy('au.description', 'DESC');
                },
                'label' => 'Severity',
                'label_attr' => array(
                    'class' => 'required middle hide endorsed-remove'
                ),
                'required' => false,
                'attr' => array(
                    'class' => 'hide endorsed-remove'
                ),
                'mapped' => false
            ))
            ->add('ticketStatus', EntityType::class, array(
                'class' => TicketStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) {

                    return $er->createQueryBuilder('au')
                        ->where('au.isDeleted = 0')
                        ->orderBy('au.description', 'DESC');
                },
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required middle hide endorsed-remove'
                ),
                'required' => false,
                'attr' => array(
                    'class' => 'hide endorsed-remove'
                ),
                'mapped' => false
            ))
            ->add('chartType', EntityType::class, array(
                'class' => ChartTypeEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('ct')
                        ->addSelect("(CASE WHEN ct.description like 'HSAT' THEN 0
                                                 WHEN ct.description like 'HSAT Results Interpretation' THEN 1
                                                 WHEN ct.description like 'CPAP Setup' THEN 2
                                                 WHEN ct.description like 'CPAP Setup Sold' THEN 3
                                                 WHEN ct.description like 'CPAP Eval' THEN 4
                                                 WHEN ct.description like 'CPAP End Eval' THEN 5
                                                 ELSE ct.description END
                                           ) AS HIDDEN customerOrderEncounter")
                        ->where("ct.isDeleted = 0")
                        ->andWhere("ct.isActive = 1")
                        ->orderBy('customerOrderEncounter', 'ASC');
                },
                'label' => 'Encounter Type',
                'required' => false,
                'mapped' => false
            ))
            ->add('chartStatus', EntityType::class, array(
                'class' => ChartStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('cs')
                        ->where('cs.isActive = 1')
                        ->andWhere('cs.isDeleted = 0')
                        ->orderBy('cs.description', 'ASC');
                },
                'choice_attr' => function($chartStatus){
                    return array('class' => $chartStatus->getChartType()->getId());
                },
                'label' => 'Encounter Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => false,
                'mapped' => false
            ))
            ->add('note_type', ChoiceType::class, array(
                'label' => 'Note Type',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => false,
                'choices' => array(
                    PatientNoteEntity::TYPE_CLINICAL => 'Clinical',
                    PatientNoteEntity::TYPE_BILLING => 'Billing',
                    PatientNoteEntity::TYPE_APPOINTMENT => 'Appointment',
                    PatientNoteEntity::TYPE_TEMPLATE => 'Template',
                ),
                'mapped' => false
            ));
    }

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

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