<?php

namespace App\Form;

use App\Entity\Customer;
use App\Entity\Devis;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NewDevisType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('num', TextType::class, [
                'label' => 'Numéro du devis',
                'required' => true,
                'attr' => [
                    'readonly' => true,
                ]
            ])
            ->add('createA', DateType::class, [
                'label' => 'Date du devis',
                'required' => true,
                'widget' => 'single_text', // Recommandé pour un meilleur rendu HTML5
                'attr' => [
                    'readonly' => true,
                ]
            ])
            ->add('startAt',DateTimeType::class,[
                'label' =>'Date de début de \'événément',
                'required' => true,
                'widget' => 'single_text',
            ])
            ->add('endAt',DateTimeType::class,[
                'label' =>'Date de fin de \'événément',
                'required' => true,
                'widget' => 'single_text',
            ])
            ->add('isNotAddCaution', CheckboxType::class, [
                'label' => 'Ne pas ajouter de caution',
                'required' => false,
            ])
            ->add('customer', EntityType::class, [
                'label' => 'Client',
                'required' => true,
                'class' => Customer::class,
                // Utilisation d'une fonction anonyme pour concaténer Nom et Prénom
                'choice_label' => function (Customer $customer) {
                    return sprintf('%s - %s',
                        strtoupper($customer->getSurname()), // Nom en majuscules
                        $customer->getName()               // Prénom
                    );
                },
                'placeholder' => 'Sélectionnez un client...',
                'attr' => [
                    'class' => 'select2' // Optionnel : si tu utilises Select2 ou TomSelect
                ]
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefault('data_class', Devis::class);
    }
}
