<?php

namespace App\Form;

use App\Entity\Subgroup;
use App\Entity\Group;
use App\Repository\GroupRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class SubgroupType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name',TextType::class, [       
                'attr' => ['autofocus' => true, 'class' => 'form-control'],
                'label' => 'Subgroup Name*',
            ])
            ->add('groups', EntityType::class, [
                // looks for choices from this entity
                'class' => Group::class,
                'query_builder' => function (GroupRepository $er) {
                    return $er->createQueryBuilder('v')                       
                        ->orderBy('v.name', 'ASC');
                },
                // uses the User.username property as the visible option string
                'choice_label' => 'name',
                'placeholder' => 'Select a Group...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Group Name*',
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
            ])
            // ->add('remarks')
           
        ;
    }

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