<?php

namespace RxConnexionBundle\Form;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;
use RxConnexionBundle\Entity\ItemSizeEntity;
use RxConnexionBundle\Entity\ItemSubCategoryEntity;
use RxConnexionBundle\Entity\ItemUnitEntity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
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 RxConnexionBundle\Form\DataTransformer\DataTransformer;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;


class ItemForm 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('code', TextType::class, array(
                'label' => 'Code 1',
                'required' => false
            ))
            ->add('code2', TextType::class, array(
                'label' => 'Code 2',
                'required' => false
            ))
            ->add('brand', TextType::class, array(
                'label' => 'Brand',
                'required' => false
            ))
            ->add('description', TextType::class, array(
                'label' => 'Description',
                'label_attr' => array(
                    'class' => 'required middle'
                ),
                'required' => true
            ))
            ->add('is_itemised', CheckboxType::class, array(
                'label' => 'Itemised',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('is_package', CheckboxType::class, array(
                'label' => 'Package',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('is_non_inventory', CheckboxType::class, array(
                'label' => 'Non Inventory',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('is_zero_price', CheckboxType::class, array(
                'label' => 'Allow Zero Price',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('is_service', CheckboxType::class, array(
                'label' => 'Service',
                'label_attr' => array(
                    'class' => ''
                ),
                'required' => false
            ))
            ->add('type', ChoiceType::class, array(
                'label' => 'Type',
                'label_attr' => array(
                    'class' => 'middle'
                ),
                'required' => false,
                'choices' => [
                    'type1' => 'Type 1',
                    'type2' => 'Type 2',
                    'type3' => 'Type 3',
                    'type4' => 'Type 4'
                ]
            ))

            ->add('itemSubCategory', EntityType::class, array(
                'class' => ItemSubCategoryEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Subcategory',
                'label_attr' => array(
                    'class' => 'required'
                ),
                'required' => true
            ))

            ->add('item_size', EntityType::class, array(
                'class' => ItemSizeEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Size',
                'label_attr' => array(
                    'class' => ''
                ),
            ))

            ->add('item_unit', EntityType::class, array(
                'class' => ItemUnitEntity::class,
                'choice_label' => 'description',
                'query_builder' => function(EntityRepository $er) use ($options) {

                    return $er->createQueryBuilder('ct')
                        ->where('ct.isDeleted = 0')
                        ->orderBy('ct.description', 'ASC');
                },
                'label' => 'Unit',
                'label_attr' => array(
                    'class' => ''
                ),
            ))
            ->add('supplier', HiddenType::class)
            ->add('item_company', HiddenType::class)
            ->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {

                $form = $event->getForm();
                $data = $event->getData();
                $itemCompany = $data->getItemCompany();
                $supplier = $data->getSupplier();

                $form
                    ->add('start_date', TextType::class, array(
                        'label' => 'Start Date',
                        'label_attr' => array(
                            'class' => 'required middle'
                        ),
                        'required' => true,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getStartDateAsString() : ''
                    ))
                    ->add('end_date', TextType::class, array(
                        'label' => 'End Date',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'attr' => array(
                            'class' => 'date'
                        ),
                        'data' => $data ? $data->getEndDateAsString() : ''
                    ))
                    ->add('item_company_desc', TextType::class, array(
                        'label' => 'Company',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'required' => false,
                        'mapped' => false,
                        'data' => $itemCompany ? $itemCompany->getDescription() : ''
                    ))
                    ->add('supplier_desc', TextType::class, array(
                        'label' => 'Supplier',
                        'label_attr' => array(
                            'class' => 'middle'
                        ),
                        'mapped' => false,
                        'data' => $supplier ? $supplier->getDescription() : ''
                    ));
            });

        $builder->get('item_company')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:CompanyEntity', false));
        $builder->get('supplier')->addModelTransformer(new DataTransformer($this->manager, 'RxConnexionBundle:SupplierEntity', false));
    }

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

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