<?php

namespace App\Form;

use App\Entity\CollectionFractal;
use App\Repository\CarteFractalRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CollectionFractalType extends AbstractType
{
        public function buildForm(FormBuilderInterface $builder, array $options): void
        {
                //dump($options);
                $collection = $options['data'] ?? null;
                $member = $collection->getMembreFractal();

                $builder
                        ->add('name')
                        ->add('ispublic')
                        ->add('membreFractal', null, [
                                'disabled'   => true,
                        ])
                        ->add('cartesFractal', null, [
                                'query_builder' => function (CarteFractalRepository $er) use ($member) {
                                                return $er->createQueryBuilder('o')
                                                ->leftJoin('o.classeurFractal', 'i')
                                                ->andWhere('i.membreFractal = :member')
                                                ->setParameter('member', $member)
                                                ;
                                        },
                                // avec 'by_reference' => false, sauvegarde les modifications
                                'by_reference' => false,
                                // classe pas obligatoire
                                //'class' => [Object]::class,
                                // permet sélection multiple
                                'multiple' => true,
                                // affiche sous forme de checkboxes
                                'expanded' => true
                                ])
                ;
        }


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