<?php

namespace OfficeBrain\Bundle\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\Validator\ExecutionContextInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Doctrine\ORM\EntityRepository;

class LocateDistributorType extends AbstractType
{
    protected $states;

    /**
     *
     * @param ContainerInterface $container
     * @param $mode
     */
    public function __construct($states, $configOptions=array())
    {
        $this->states = $states;
        $this->locateConfig = $configOptions;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $constraints['locatedDistributorId'] = array(
            new Assert\NotBlank(array('message' => 'user.locatedDistributorId.not_blank')),
        );


        if ($this->locateConfig['option']['state']['status']==1) {
            $builder->add('stateId', 'choice', array(
                        'choices' => $this->states,
            ));
        }

        if ($this->locateConfig['option']['zipcode']['status']==1) {
            $builder->add('zipcode', 'text');
        }

        $builder->add('locatedDistributorId', 'hidden', array('constraints' => $constraints['locatedDistributorId'], 'attr' => array('class' => 'js-locate-distributor-value')));
    }

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


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