<?php

namespace App\Form;

use App\Bundle\Infoclinica\Region;
use App\Entity\SpecialistView;
use App\Entity\LocationView;
use App\Entity\Department;
use App\Entity\Filial;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class SpecialistSearchType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('onlineMode', CheckboxType::class, [
                'label' => 'Онлайн консультация',
                'mapped' => false,
                'required' => false
            ])
            ->add('name', null, [
                'mapped' => false,
                'required' => false
            ])
            ->add('kinder', ChoiceType::class, [
                'choices'  => [
                    'Детский врач' => 1,
                ],
                'placeholder' => 'Взрослый врач',
                'mapped' => false,
                'empty_data' => null,
                'required'   => false
            ])
           ->add('department', EntityType::class, [
                'class' => Department::class,
                'query_builder' => function (EntityRepository $er) use ($options) {
                    $qb = $er->createQueryBuilder('d')
                        ->distinct()
                        ->innerJoin('App\Entity\LocationView', 'l', 'WITH', 'l.department = d.did AND l.active = true')
                        ->innerJoin('App\Entity\SpecialistView', 's', 'WITH', 's.id = l.specialistId AND s.active = true')
                        ->leftJoin('App\Entity\Filial', 'f', 'WITH', 'f.fid = l.filial')
                        ->where('f.address LIKE :address')
                        ->andWhere('d.did <> :did')
                        ->setParameter('did', 0)
                        ->setParameter('address', '%' . Region::getCurrentName() . '%');
                    
                    // Добавляем фильтр по региону, если он указан
                    if (!empty($options['regionId']) && $options['regionId'] > 0) {
                        $qb->andWhere('s.regionId = :regionId')
                           ->setParameter('regionId', $options['regionId']);
                    }
                    
                    // Добавляем фильтр по детским специализациям, если выбран "Детский врач"
                    if (!empty($options['kinder']) && $options['kinder'] == 1) {
                        $qb->andWhere('s.sType = :sType')
                           ->setParameter('sType', 1);
                    }
                    
                    return $qb->orderBy('d.name', 'ASC');
                },
                'choice_value' => 'did',
                'choice_label' => 'name',
                'placeholder' => 'Все специализации',
                'empty_data' => null,
                'mapped' => false,
                'required'   => false
            ])
            ->add('filial', EntityType::class, [
                'class' => Filial::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er
                        ->createQueryBuilder('f')
                        ->where("f.address LIKE :address and f.active = true and f.fid <> 0")
                        ->setParameter('address', '%' . Region::getCurrentName() . '%')
                        ->orderBy('f.id', 'ASC');
                },
                'choice_value' => 'fid',
                'choice_label' => 'name',
                'mapped' => false,
                'multiple' => true, 
                'empty_data' => [],
                'required' => false
            ])
             ->add('category', ChoiceType::class, [
                'choices' => [
                    'Первая' => 'Первая',
                    'Вторая' => 'Вторая',
                    'Высшая' => 'Высшая'
                ],
                'placeholder' => 'Выберите категорию',
                'mapped' => false,
                'multiple' => false,
                'empty_data' => null,
                'required' => false
            ])
            ->add('current_date', null, [
                'mapped' => false
            ])
            ->add('order_by', null, [
                'mapped' => false,
                'required'   => false
            ])
        ;
    }

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