<?php

namespace App\Form;

use App\Entity\PriceList;
use App\Entity\PriceDepartment;
use App\Entity\Filial;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Bundle\Infoclinica\Region;

class PriceListAdminFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('schname', TextType::class, [
                'empty_data' => null,
                'mapped' => true,
                'required' => false
            ])
            ->add('kodoper', TextType::class, [
                'empty_data' => null,
                'mapped' => true,
                'required' => false
            ])
            ->add('groupId', EntityType::class, [
                'class' => PriceDepartment::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('d')
                        ->orderBy('d.name', 'ASC');
                },
                'choice_value' => 'groupId',
                'choice_label' => 'name',
                'placeholder' => 'Департамент',
                'empty_data' => null,
                'mapped' => false,
                'required'   => true
            ])
            ->add('filial', EntityType::class, [
                'class' => Filial::class,
                'query_builder' => function (EntityRepository $er) {
                   return $er
                        ->createQueryBuilder('f')
                        ->where('f.fid <> :fid')
                        ->setParameter('fid', 0)
                        ->orderBy('f.address', 'DESC');
                },
                'choice_label' => 'address',
                'choice_value' => 'fid',
                'placeholder' => 'Все филиалы',
                'mapped' => false,
                'multiple' => false, 
                'empty_data' => null,
                'required' => false
            ]);
    }
    
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => PriceList::class,
        ]);
    }
}
