<?php

namespace App\Form\Type;

use App\Entity\Groupe;
use App\Entity\Prestataire;
use Doctrine\ORM\EntityManagerInterface;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PrestataireInfosFormType extends AbstractType
{
    protected $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('raison', TextType::class, [
                'label' => 'Raison :',
                'required' => true,
            ])
            ->add('statut', TextType::class, [
                'label' => 'Statut :',
                'required' => false,
            ])
            ->add('siret', TextType::class, [
                'label' => 'SIRET :',
                'required' => false,
            ])
            ->add('iban', TextType::class, [
                'label' => 'IBAN :',
                'required' => false,
            ])
            ->add('responsable', TextType::class, [
                'label' => 'Responsable :',
                'required' => false,
            ])
            ->add('metier', TextType::class, [
                'label' => 'Métier responsable :',
                'required' => false,
            ])
            ->add('description', CKEditorType::class, [
                'label' => 'Description :',
                'required' => false,
            ])
            ->add('groupe', ChoiceType::class, [
                'required' => true,
                'disabled' => true,
                'label' => 'GROUPE LOCAL',
                'choices' => $this->em->getRepository(Groupe::class)->findAll(),
                'choice_label' => 'name',
                'placeholder' => 'Choisir un groupe',
            ])
            ->add('geolocs', CollectionType::class, [
                'entry_type' => GeolocPrestataireFormType::class,
                'entry_options' => ['label' => false],
                'required' => false,
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'label' => false,
            ])
            ->add('save', SubmitType::class, ['label' => 'Valider les modifications'])
        ;
    }

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

    public function getBlockPrefix()
    {
        return 'formPrestataireInfos';
    }
}
