<?php

namespace OfficeBrain\Bundle\ServiceGroupBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ServiceRuleMappingGroupType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('serviceGroupId', 'entity',
                array(
                    'class' => 'OfficeBrainServiceGroupBundle:ServiceGroupLanguage',
                    'query_builder' => function (\OfficeBrain\Bundle\ServiceGroupBundle\Entity\ServiceGroupLanguageRepository $er) {
                        return $er->createQueryBuilder('sgl')
                                ->leftJoin('OfficeBrainServiceGroupBundle:ServiceGroup', 'sg', 'WITH', 'sg.id = sgl.serviceGroup')
                                ->where('sgl.languageId=:languageId')
                                ->andWhere('sg.countryId=:countryId')
                                ->setParameter('countryId', 1)
                                ->setParameter('languageId', 1);
                    },
                    'choice_label' => 'groupName',
                    'choice_value' => 'serviceGroup.id',
                    'choice_attr' => array(
                        //'serviceGroup.id' => array('data-class' => 'text-muted'),
                    ),
                    'multiple' => true,
                    'required' => true,
                    'constraints' => array(new NotBlank(array('message' => 'admin.service_rule_mapping.select_group'))),
                )
            )
            ->add('serviceRuleMappingId', 'hidden', array('required' => false))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OfficeBrain\Bundle\ServiceGroupBundle\Entity\serviceRuleMappingGroup',
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'officebrain_bundle_servicegroupbundle_servicerulemappinggroup';
    }
}
