<?php

namespace App\Form;

use App\Entity\TourRequest;
use App\Entity\ExpenseType;
use App\Entity\ExpensePurpose;
use App\Repository\ExpenseTypeRepository;
use App\Repository\ExpensePurposeRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class TourRequestType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder            
            ->add('expenseType', EntityType::class, [
                // looks for choices from this entity
                'class' => ExpenseType::class,
                'query_builder' => function (ExpenseTypeRepository $er) {
                    return $er->createQueryBuilder('v')                       
                        ->orderBy('v.name', 'ASC');
                },              
                // uses the User.username property as the visible option string
                'choice_label' => 'name',
                'placeholder' => 'Select a Type...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Expense Type',  
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ])          
            ->add('expensePurpose', EntityType::class, [
                // looks for choices from this entity
                'class' => ExpensePurpose::class,
                'query_builder' => function (ExpensePurposeRepository $er) {
                    return $er->createQueryBuilder('v')                       
                        ->orderBy('v.name', 'ASC');
                },              
                // uses the User.username property as the visible option string
                'choice_label' => 'name',
                'placeholder' => 'Select a Purpose...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Expense Purpose',  
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ])
            ->add('tourScope', ChoiceType::class, [                           
                'choices'  => [                    
                    'Domestic' => 'Domestic', 
                    'Abroad' => 'Abroad',               
                ],
                'attr' => ['class' => 'form-control'],                     
                'expanded' => true,
                'multiple' => false,               
                ])           
            ->add('placeVisit',TextType::class, [       
                'attr' => ['class' => 'form-control'],
                'label' => 'Place to Visit',
                ])
            ->add('expectVisit',TextType::class, [       
                'attr' => ['class' => 'form-control'],
                'label' => 'Expect to Visit (Party / Customer Name)',
                ])
            ->add('startingDate', DateTimeType::class, [
                'attr' => ['class' => 'form-control',
                'style' => 'line-height:20px;'],
                'widget' => 'single_text',
                'empty_data' => null, 
                'label' => 'Tour Starting Date & Time'                                              
                ])
            ->add('endingDate', DateTimeType::class, [
                'attr' => ['class' => 'form-control',
                'style' => 'line-height:20px;'],
                'widget' => 'single_text',
                'empty_data' => null, 
                'label' => 'Tour Ending Date & Time'                                              
                ])
            ->add('contactPhone',TextType::class, [       
                'attr' => ['class' => 'form-control'],
                'label' => 'Contact Phone During Tour Period',
                ])
            ->add('contactAddress', TextareaType::class, [
                'attr' => ['class' => 'form-control',                   
                    ],    
                'label' => 'Contact Address During Tour Period',                           
                ])
            ->add('transportationMode', ChoiceType::class, [                           
                'choices'  => [
                    'Office Car' => 'Office Car',
                    'Air' => 'Air',
                    'Road' => 'Road', 
                    'Rail' => 'Rail', 
                    'Water' => 'Water',
                               
                ],
                'placeholder' => 'Select a Transportation Mode...',
                'attr' => ['class' => 'form-control select-tags'],  
                'label' => 'Mode of Transportation',                         
                // 'expanded' => true,
                // 'multiple' => false,                
                ])     
            ->add('tourPurpose', TextareaType::class, [
                'attr' => ['class' => 'form-control',              
                    ],   
                'label' => 'Brief Description of Purpose of Tour',                                
                ])
            // ->add('officialDuties', TextareaType::class, [
            //     'attr' => ['class' => 'form-control',              
            //         ], 
            //     'label' => 'Arrangements of official duties & responsibilities during tour period',                             
            //     ]) 
            ->add('advanceCurrency', ChoiceType::class, [                           
                'choices'  => [
                    'BDT' => 'BDT',
                    'USD' => 'USD', 
                    'GBP' => 'GBP',
                    'EURO' => 'EURO',                           
                ],
                'placeholder' => 'Select a Currency...',
                'attr' => ['class' => 'form-control select-tags'], 
                'label' => 'Advance Amount Currency Required for this Tour Purpose',                    
                // 'expanded' => true,
                // 'multiple' => false,                
                ])
            ->add('advanceAmount',NumberType::class, [       
                'attr' => ['class' => 'form-control'],
                'label' => 'Advance Amount Required for this Tour Purpose',     
                ])
           
        ;

        // $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        //     // ... add a choice list of friends of the current application user
        //     $tourRequest = $event->getData();
        //     $form = $event->getForm();

        //  //   if (!$tourRequest || null === $tourRequest->getId()) {
        //         $form->add('expensePurpose', EntityType::class, [
        //             // looks for choices from this entity
        //             'class' => ExpensePurpose::class,
        //             'query_builder' => function (ExpensePurposeRepository $er) use ($tourRequest)  {
        //                 $varExpenseType = $tourRequest->getExpenseType();
        //                 return $er->createQueryBuilder('v')  
        //                     ->Where('v.expenseType = :varExpenseType') 
        //                     ->setParameter('varExpenseType', $varExpenseType)                     
        //                     ->orderBy('v.name', 'ASC');
        //             },              
        //             // uses the User.username property as the visible option string
        //             'choice_label' => 'name',
        //             'placeholder' => 'Select a Purpose...',
        //             'attr' => [
        //                 'class' => 'form-control select-tags',
        //                 ],
        //             'label' => 'Expense Purpose',  
        //             // used to render a select box, check boxes or radios
        //             // 'multiple' => true,
        //             // 'expanded' => true,
        //             ]);
        //  //   }
        // });
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => TourRequest::class,
        ]);
    }
}
