<?php

namespace App\Form\BackendAdmin;

use Symfony\Component\Form\AbstractType;
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\SubmitType;

use App\Entity\User;
use App\Entity\Societe;
use App\Entity\Partenaire;

class PartenaireType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('partenaire', EntityType::class, array(
                'class' => User::class,
                'label' => 'Nom Partenaire',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('U')
                            ->where("U.isDeleted = false")
                            ->orderBy('U.firstname', 'ASC');
                },
                'choice_label' => 'firstname',
                'multiple'  => false,
                'required' => true,
                'by_reference' => false,
            ))
            ->add('societe', EntityType::class, array(
                'class' => Societe::class,
                'label' => 'Société',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('S')
                            ->where("S.active = false")
                            ->orderBy('S.nom', 'ASC');
                },
                'choice_label' => 'nom',
                'multiple'  => false,
                'required' => true,
                //'by_reference' => false,
            ))
            ->add("save", SubmitType::class)
        ;
    }

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