<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\TicketSeverityEntity;
use RxConnexionBundle\Entity\TicketStatusEntity;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use RxConnexionBundle\Form\Type\PatientChartTicketForm;
use RxConnexionBundle\Form\Type\PatientTicketForm;
use RxConnexionBundle\Form\Type\TicketCcForm;
use RxConnexionBundle\Form\Type\TicketNoteForm;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
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\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;


class TicketForm 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('ticketStatus', EntityType::class, array(
                'class' => TicketStatusEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => false
            ))
            ->add('ticketSeverity', EntityType::class, array(
                'class' => TicketSeverityEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Severity',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => false
            ))
            ->add('patientTickets', CollectionType::class, array(
                'entry_type' => PatientTicketForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('patientChartTickets', CollectionType::class, array(
                'entry_type' => PatientChartTicketForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('ticketCcs', CollectionType::class, array(
                'entry_type' => TicketCcForm::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => false,
                'by_reference' => false
            ))
            ->add('note', TextareaType::class, array(
                'label' => 'Notes',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => true,
                'attr' => array(
                    'rows' => 10
                ),
                'mapped' => false,
                'data' => null
            ))
            ->add('accountUser', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

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

                $form
                    ->add('accountUser_fullName', TextType::class, array(
                        'label' => 'User',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'mapped' => false,
                        'data' => $accountUser ? $accountUser->getFullName() : ''
                    ));
            });
        $builder->get('accountUser')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:AccountUserEntity', false));
    }

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

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