<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use RxConnexionBundle\Entity\FaxTranEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class FaxTranForReferralForm extends AbstractType
{
    private $manager;

    public function __construct(ObjectManager $manager) {
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', HiddenType::class)
            ->add('remote_id', TextType::class, array(
                'label' => 'From Fax #',
                'attr'  => array(
//                    'readonly'  => true,
                )
            ))
            ->add('file_name', TextType::class, array(
                'label' => 'File Name',
                'attr'  => array(
//                    'readonly'  => true,
                )
            ))
            ->add('receive_status', TextType::class, array(
                'label' => 'Receive Status',
                'attr'  => array(
                    'readonly'  => true,
                )
            ))
            ->add('pages', TextType::class, array(
                'label' => 'Pages',
                'attr'  => array(
                    'readonly'  => true,
                )
            ))
            ->add('size', TextType::class, array(
                'label' => 'Size',
                'attr'  => array(
                    'readonly'  => true,
                )
            ))
            ->add('viewed_status', TextType::class, array(
                'label' => 'Viewed Status',
                'attr'  => array(
                    'readonly'  => true,
                )
            ))
            ->add('fax_id', TextType::class, array(
                'label' => 'Fax ID',
                'attr'  => array(
                    'readonly'  => true,
                )
            ))
            ->add('fax_category', ChoiceType::class, array(
                'label' => 'Category',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true,
                'choices' => FaxTranEntity::CATEGORY
            ))
            ->add('fax_status', ChoiceType::class, array(
                'label' => 'Status',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true,
                'choices' => FaxTranEntity::STATUS
            ))
            ->add('health_facility', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();
                $data = $event->getData();
                $form
                    ->add('fax_date', TextType::class, array(
                        'label' => 'Fax Date',
                        'attr' => array(
                            'class' => 'date',
//                            'readonly'  => true,
                        ),
                        'data' => $data ? $data->getFaxDateAsString() : ''
                    ));
            });
    }

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

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' 	  => 'RxConnexionBundle\Entity\FaxTranEntity',
        ));
    }
}