<?php

namespace Cadoles\PortalBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Tetranz\Select2EntityBundle\Form\Type\Select2EntityType;
use Cadoles\CoreBundle\Form\Type\IconChoiceType;

use Symfony\Component\OptionsResolver\OptionsResolver;

use Doctrine\ORM\EntityRepository;

class PageSubmitType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $access=$options["access"];
        $user=$options['user'];

        $builder
            ->add('submit', SubmitType::class, [
                "label"         => ($options["mode"]=="delete"?"Confirmer la Suppression":"Valider"),
                "attr"          => ($options["mode"]=="delete"?array("class" => "btn btn-danger"):array("class" => "btn btn-success"))
            ])

            ->add('name', TextType::class, [
              'label'           => 'Nom'
            ])  

            ->add('pagecategory', EntityType::class, [
                'label'         => 'Catégorie de Page',
                'class'         => 'CadolesPortalBundle:Pagecategory',
                'choice_label'  => 'name',
                'placeholder'   => '-- Sélectionnez une catégorie --',
                'query_builder' => function(EntityRepository $er) use ($access) {
                    if($access=="config")
                        return $er->createQueryBuilder('pc')
                                  ->select('pc');             
                    else
                        return $er->createQueryBuilder('pc')
                                  ->select('pc')
                                  ->where('pc.id>0');

                },                
            ])

            ->add('usage', ChoiceType::class, [
                "label"         => 'Usage',
                "mapped"        => false,
                "choices"       => ["Personnel" => 'user',"Groupe de Travail" => 'group'],                
                "disabled"      => ($options["mode"]=="delete"?true:false),
            ])

            ->add('fonticon', IconChoiceType::class, [
                'label' => 'Icône',
                'required' => false,
            ])

            ->add('page',
                Select2EntityType::class, array(
                    'label' => "Modèle de Page",
                    'disabled' => ($options["mode"]=="delete"?true:false),
                    'required'    => false,
                    'multiple' => false,
                    'remote_route' => 'cadoles_portal_user_pagetemplate_ajax_selectlist',
                    'class' => 'Cadoles\PortalBundle\Entity\Page',
                    'req_params' => ['usage' => 'parent.children[usage]'],
                    'primary_key' => 'id',
                    'text_property' => 'name',
                    'minimum_input_length' => 0,
                    'page_limit' => 10,
                    'allow_clear' => true,
                    'delay' => 250,
                    'cache' => false,
                    'cache_timeout' => 60000, // if 'cache' is true
                    'language' => 'fr',
                    'placeholder' => '-- Selectionner un modèle de page --',
                    'attr' => array("class" => "form-control", "style" => "margin-bottom:15px")
                )
            )         

            ->add('groups', EntityType::class, [
                'label'         => 'Partager avec les Groupes',
                'class'         => 'CadolesCoreBundle:Group',
                'choice_label'  => 'label',
                'required'      => false,
                'multiple'      => true,
                'expanded'      => true,
                'placeholder'   => '-- Selectionner un groupe --',
                'query_builder' => function(EntityRepository $er) use ($user) {
                    return $er->createQueryBuilder('g')
                              ->select('g')
                              ->From('CadolesCoreBundle:UserGroup','ug')
                              ->where('g.fgcanshare=:fgcanshare')
                              ->andWhere('g=ug.group')
                              ->andWhere('ug.user=:user')
                              ->andWhere('ug.rolegroup>=:role')
                              ->setParameter('fgcanshare',true)
                              ->setParameter('role',90)
                              ->setParameter('user',$user);
                },
            ])

            ->add('roworder', IntegerType::class, [
                'label'         => 'Ordre',
            ])                

            ->add('maxwidth', IntegerType::class, [
                'label'         => "Largeur maximum (0 pour largeur de l'écran)",
            ]);   

        if($access=="config") {
            $builder->add('user', Select2EntityType::class, [
                'label' => "Propriétaire",
                'disabled' => ($options["mode"]=="delete"?true:false),
                "required"    => false,
                'multiple' => false,
                'remote_route' => 'cadoles_core_config_user_ajax_selectlist',
                'class' => 'Cadoles\coreBundle\Entity\User',
                'primary_key' => 'id',
                'text_property' => 'username',
                'minimum_input_length' => 2,
                'page_limit' => 10,
                'allow_clear' => true,
                'delay' => 250,
                'cache' => false,
                'cache_timeout' => 60000, // if 'cache' is true
                'language' => 'fr',
                'placeholder' => 'Selectionner un propriétaire',
                'attr' => array("class" => "form-control", "style" => "margin-bottom:15px")
            ]);            
        }

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'Cadoles\PortalBundle\Entity\Page',
            'mode' => 'string',
            'access' => 'string',
            'user' => 'Cadoles\CoreBundle\Entity\User'
        ]);
    }
}
