<?php

namespace App\Form;

use App\Entity\Assetregister;
use App\Entity\Group;
use App\Entity\Subgroup;
use App\Entity\Assetname;
use App\Entity\Vendor;
use App\Entity\Businessunit;
use App\Entity\Location;
use App\Entity\Unit;
use App\Repository\GroupRepository;
use App\Repository\SubgroupRepository;
use App\Repository\AssetnameRepository;
use App\Repository\VendorRepository;
use App\Repository\BusinessunitRepository;
use App\Repository\LocationRepository;
use App\Repository\UnitRepository;
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\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Validator\Constraints\File;

class AssetregisterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->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,
                ])

            ->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('assetname', EntityType::class, [
                // looks for choices from this entity
                'class' => Assetname::class,
                'query_builder' => function (AssetnameRepository $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 Asset Name...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Asset Name*',
                
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ]) 
            ->add('acquisitiondate', DateType::class, [
                'attr' => ['class' => 'form-control',
                'style' => 'line-height:20px;'],
                'widget' => 'single_text',
                'empty_data' => null,    
                'label' => 'Acquisition Date*',                            
                ])
            ->add('acquisitioncost', NumberType::class, [
                'attr' => ['class' => 'form-control'], 
                'label' => 'Acquisition Cost*',                                 
                ])            
            ->add('serialno',TextType::class, [       
                'attr' => ['class' => 'form-control'],                
            ])
            ->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',
                    ],
                'label' => 'Unit*',
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ])
            ->add('quantity', NumberType::class, [
                'attr' => ['class' => 'form-control'], 
                'label' => 'Quantity*',                                  
                ])
           // ->add('disposaldate')
           // ->add('disposalamount')
           // ->add('docpath')
                     
            ->add('vendor', EntityType::class, [
                // looks for choices from this entity
                'class' => Vendor::class,
                'query_builder' => function (VendorRepository $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 Vendor...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Vendor*',
                
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ]) 

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

            ->add('location', EntityType::class, [
                // looks for choices from this entity
                'class' => Location::class,
                'query_builder' => function (LocationRepository $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 Location...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'required' => false,
                
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ])  

            ->add('docpath', FileType::class, [
                'label' => 'Document Upload',
    
                // unmapped means that this field is not associated to any entity property
                'mapped' => false,
    
                // make it optional so you don't have to re-upload the PDF file
                // every time you edit the Product details
                'required' => false,
    
                // unmapped fields can't define their validation using annotations
                // in the associated entity, so you can use the PHP constraint classes
                'constraints' => [
                    new File([
                            'maxSize' => '1024k',
                            'mimeTypes' => [
                                'image/*',
                                'application/pdf',
                                'application/x-pdf',
                            ],
                            'mimeTypesMessage' => 'Please upload a Doc',
                            ])
                        ],
                    ])
    
                ->add('remarks',TextType::class, [       
                    'attr' => ['class' => 'form-control'], 
                    'required' => false,               
                    ])

            // ->add('openingbalence')
            // ->add('note')
            // ->add('spnote')
            // ->add('status')
            // ->add('type')
            // ->add('active')
            // ->add('entrydate')
            // ->add('entryby')
            // ->add('editdate')
            // ->add('editby')          
            // ->add('unit')
           
        ;
    }

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