<?php
namespace App\Form;
use App\Entity\Agenda;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Entity\User;
use App\Entity\Procedimientos;
use App\Entity\SeguroPrivado;
use App\Entity\OrdenProcedimiento;
use App\Entity\Sala;
use App\Entity\Especialidad;
use App\Entity\Paciente;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use App\Repository\UserRepository;
class AgendaType extends AbstractType
{
private UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
//dd($options['doctor'],$options['unidad']);
if($options['doctor']>0){
$builder
->add('inicio', DateTimeType::class, [
'widget' => 'single_text',
])
->add('fin', DateTimeType::class, [
'widget' => 'single_text',
])
->add('doctor', EntityType::class, [
'class' => User::class,
'query_builder' => function (EntityRepository $er) use($options) {
return $er->createQueryBuilder('u')
->orderBy('u.tipousuario', 'ASC')
->addOrderBy('u.apellido1', 'ASC')
->andWhere('u.tipousuario IN (:tipos)')
//->andWhere('u.id = :val1')
//->setParameter('val1',$options['doctor'])
->andWhere('u.estado = 1')
->setParameter('tipos', [3, 4]);
},
'choice_label' => function ($user) {
return $user->getDr().' '.$user->getApellido1().' '.$user->getApellido2().' '.$user->getNombre1().' '.$user->getNombre2();
},
'data' => is_numeric($options['doctor'])
? $this->userRepository->find($options['doctor'])
: $options['doctor'],
'attr' => [
'class' => 'form-control select2'
],
])
->add('procedimiento', EntityType::class, [
'class' => Procedimientos::class,
'query_builder' => function (EntityRepository $er) use($options) {
return $er->createQueryBuilder('p')
->andWhere('p.estado = 1')
//->andWhere('p.imagen = :val1')
//->setParameter('val1', $options['unidad'])
->orderBy('p.id', 'ASC');
},
'choice_label' => 'nombre',
// 🚀 HABILITAR MULTIPLE
'multiple' => true,
'expanded' => false, // false = SELECT MULTIPLE (true = checkboxes)
'mapped' => false, // <---- CLAVE
// Opcional si usas Select2
'attr' => [
'class' => 'form-control select2',
],
])
->add('especialidad', EntityType::class, [
'class' => Especialidad::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('es')
->orderBy('es.nombre', 'ASC')
->andWhere('es.estado = 1');
},
'choice_label' => function ($especialidad) {
return $especialidad->getNombre();
},
'preferred_choices' => [$options['especialidad']],
])
->add('seguro', EntityType::class, [
'class' => SeguroPrivado::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('sg')
->orderBy('sg.nombre', 'ASC')
->andWhere('sg.estado = 1');
},
'choice_label' => function ($seguro) {
return $seguro->getNombre();
}
])
->add('observacion')
->add('validacion', TextType::class, [
'required' => false,
'label' => 'Motivo Consulta',
])
/*->add('orden', EntityType::class, [
'class' => OrdenProcedimiento::class,
'query_builder' => function (EntityRepository $er) use ($options) {
$qb = $er->createQueryBuilder('o')
->andWhere('o.estado = :estado')
->setParameter('estado', 1)
->orderBy('o.id', 'ASC');
// si no hay paciente, no cargar nada
if (!empty($options['paciente'])) {
$qb->andWhere('o.paciente = :paciente')
->setParameter('paciente', $options['paciente']); // objeto Paciente
} else {
$qb->andWhere('1 = 0');
}
return $qb;
},
'mapped' => false,
'required' => false,
// ✅ MULTIPLE SELECT
'multiple' => true,
'expanded' => false, // false = <select multiple>, true = checkboxes
'choice_label' => function (OrdenProcedimiento $orden) {
return $orden->getProcedimiento()->getNombre();
},
// ✅ Select2
'attr' => [
'class' => 'form-control select2',
],
])*/
;
}else{
$builder
->add('inicio', DateTimeType::class, [
'widget' => 'single_text',
])
->add('fin', DateTimeType::class, [
'widget' => 'single_text',
])
->add('doctor', EntityType::class, [
'class' => User::class,
'query_builder' => function (EntityRepository $er) use($options) {
return $er->createQueryBuilder('u')
->orderBy('u.tipousuario', 'ASC')
->addOrderBy('u.apellido1', 'ASC')
->andWhere('u.tipousuario IN (:tipos)')
->andWhere('u.estado = 1')
->setParameter('tipos', [3, 4])
;
},
'choice_label' => function ($user) {
return $user->getDr().' '.$user->getApellido1().' '.$user->getApellido2().' '.$user->getNombre1().' '.$user->getNombre2();
}
])
->add('procedimiento', EntityType::class, [
'class' => Procedimientos::class,
'query_builder' => function (EntityRepository $er) use($options) {
return $er->createQueryBuilder('p')
->andWhere('p.estado = 1')
//->andWhere('p.imagen = :val1')
//->setParameter('val1', $options['unidad'])
->orderBy('p.id', 'ASC');
},
'choice_label' => 'nombre',
// 🚀 HABILITAR MULTIPLE
'multiple' => true,
'expanded' => false, // false = SELECT MULTIPLE (true = checkboxes)
'mapped' => false, // <---- CLAVE
// Opcional si usas Select2
'attr' => [
'class' => 'form-control select2',
],
])
->add('especialidad', EntityType::class, [
'class' => Especialidad::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('es')
->orderBy('es.nombre', 'ASC')
->andWhere('es.estado = 1');
},
'choice_label' => function ($especialidad) {
return $especialidad->getNombre();
},
'preferred_choices' => [$options['especialidad']],
])
->add('seguro', EntityType::class, [
'class' => SeguroPrivado::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('sg')
->orderBy('sg.nombre', 'ASC')
->andWhere('sg.estado = 1');
},
'choice_label' => function ($seguro) {
return $seguro->getNombre();
},
])
->add('observacion')
->add('validacion', TextType::class, [
'required' => false,
'label' => 'Motivo Consulta',
])
/*->add('orden', EntityType::class, [
'class' => OrdenProcedimiento::class,
'query_builder' => function (EntityRepository $er) use ($options) {
$qb = $er->createQueryBuilder('o')
->andWhere('o.estado = :estado')
->setParameter('estado', 1)
->orderBy('o.id', 'ASC');
// si no hay paciente, no cargar nada
if (!empty($options['paciente'])) {
$qb->andWhere('o.paciente = :paciente')
->setParameter('paciente', $options['paciente']); // objeto Paciente
} else {
$qb->andWhere('1 = 0');
}
return $qb;
},
'mapped' => false,
'required' => false,
// ✅ MULTIPLE SELECT
'multiple' => true,
'expanded' => false, // false = <select multiple>, true = checkboxes
'choice_label' => function (OrdenProcedimiento $orden) {
return $orden->getProcedimiento()->getNombre();
},
// ✅ Select2
'attr' => [
'class' => 'form-control select2',
],
])*/
;
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Agenda::class,
'doctor' => null,
'especialidad' => null,
'unidad' => null,
'paciente' => null,
]);
}
}