<?php

namespace App\Form;

use App\Entity\User;
use App\Entity\Filial;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use App\Bundle\Infoclinica\Region;

class ReferenceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $sendingChoices = $options['isAuthorized'] ? [
            'Лично в клинике' => 1,
            'Отправка в налоговую инспекцию' => 2,
        ] : [
            'Лично в клинике' => 1,
        ];

        $builder
            ->add('autorName', TextType::class, ['mapped' => false])
            ->add('birthDate', TextType::class, ['mapped' => false])
            ->add('inn', TextType::class, [
                'mapped' => false,
            ])
            ->add('phone', TextType::class, [
                'mapped' => false,
            ])
            ->add('periodFirst', DateType::Class, array(
                'mapped' => false,
                'format' => 'yyyy-MM-dd',
                'widget' => 'single_text',
            ))
            ->add('periodLast', DateType::Class, array(
                'mapped' => false,
                'format' => 'yyyy-MM-dd',
                'widget' => 'single_text',
            ))
            ->add('responsible', ChoiceType::class, [
                'mapped' => false,
                'expanded' => false,
                'choices' => [
                    'себя' => 1,
                    'другого' => 0,
                ]
            ])
            ->add('sending', ChoiceType::class, [
                'mapped' => false,
                'expanded' => false,
                'choices' => $sendingChoices,
                'data' => 1,
            ])
            ->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.city', 'ASC');
                },
                'choice_label' => 'address',
                'choice_value' => 'address',
                'mapped' => false,
                'multiple' => false, 
                'empty_data' => [],
                'required' => false
            ])
            ->add('filialSending', EntityType::class, [
                'class' => Filial::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er
                        ->createQueryBuilder('f')
                        ->where('f.fid <> :fid')
                        ->setParameter('fid', 0)
                        ->orderBy('f.city', 'ASC');
                },
                'choice_label' => 'address',
                'choice_value' => 'address',
                'mapped' => false,
                'multiple' => false, 
                'empty_data' => [],
                'required' => false
            ]);
    }

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