<?php

namespace App\Form;

use App\Entity\Assetname;
use App\Entity\Unit;
use App\Entity\Group;
use App\Entity\Subgroup;
use App\Repository\UnitRepository;
use App\Repository\GroupRepository;
use App\Repository\SubgroupRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;

class AssetnameType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name',TextType::class, [       
                'attr' => ['autofocus' => true, 'class' => 'form-control'],
                'label' => 'Asset Name*',
                
            ])
            ->add('unit', EntityType::class, [
                // looks for choices from this entity
                'class' => Unit::class,
                'query_builder' => function (UnitRepository $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 Unit...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Unit*',
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ])
            ->add('remarks',TextType::class, [       
                'attr' => ['class' => 'form-control'], 
                'required' => false,              
                ])
            ->add('groups', EntityType::class, [
                // looks for choices from this entity
                'class' => Group::class,
                'query_builder' => function (GroupRepository $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 Group...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Group*',
              
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ])
                   
                ;
                
                // This Used for only populate Sub Group item by filtering GroupId of Edit and new entry as usual
                $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
                    $assetname = $event->getData();
                    $form = $event->getForm();
                    $groupId = $assetname->getGroups();
            
                    // checks if the Product object is "new"
                    // If no data is passed to the form, the data is "null".
                    // This should be considered a new "Product"
                    if (!$assetname || null === $assetname->getId()) {
                        $form ->add('subgroup', EntityType::class, [
                            // looks for choices from this entity
                            'class' => Subgroup::class,
                            'query_builder' => function (SubgroupRepository $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 Subgroup...',
                            'attr' => [
                                'class' => 'form-control select-tags',
                                ],
                            'label' => 'Sub Group*',
                            
                            // used to render a select box, check boxes or radios
                            // 'multiple' => true,
                            // 'expanded' => true,
                            ])   ;
                    }
                    else
                    {
                        $form ->add('subgroup', EntityType::class, [
                            // looks for choices from this entity
                            'class' => Subgroup::class,
                            'query_builder' => function (SubgroupRepository $er) use ($groupId) {
                                return $er->createQueryBuilder('s')  
                                    ->where('s.groups = :groupid') 
                                    ->setParameter('groupid', $groupId)                    
                                    ->orderBy('s.name', 'ASC');
                            },
                            // uses the User.username property as the visible option string
                            'choice_label' => 'name',
                            'placeholder' => 'Select a Subgroup...',
                            'attr' => [
                                'class' => 'form-control select-tags',
                                ],
                            'label' => 'Sub Group*',
                            
                            // used to render a select box, check boxes or radios
                            // 'multiple' => true,
                            // 'expanded' => true,
                            ])   ;
                    }
                });

                // This used for after submit all Sub group list available in the form of edit
                $builder->addEventListener(FormEvents::SUBMIT , function (FormEvent $event) {
                   $form = $event->getForm();

                   $form ->add('subgroup', EntityType::class, [
                    // looks for choices from this entity
                    'class' => Subgroup::class,
                    'query_builder' => function (SubgroupRepository $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 Subgroup...',
                    'attr' => [
                        'class' => 'form-control select-tags',
                        ],
                    'label' => 'Sub Group*',
                    
                    // used to render a select box, check boxes or radios
                    // 'multiple' => true,
                    // 'expanded' => true,
                    ])   ;

                });
           
          
            // ->add('note', HiddenType::class, [
            //     'empty_data' => '',
            // ])
            // ->add('status', HiddenType::class, [
            //     'empty_data' => '',
            // ])
            // ->add('type', HiddenType::class, [
            //     'empty_data' => '',
            // ])
            // ->add('entrydate', HiddenType::class, [
            //     'empty_data' => '',
            // ])
            // ->add('entryby', HiddenType::class, [
            //     'data' => '',
            // ])
            // ->add('editdate', HiddenType::class, [
            //     'empty_data' => '',
            // ])
            // ->add('editby', HiddenType::class, [
            //     'empty_data' => '',
            // ])
           
        
    }

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