<?php
namespace OfficeBrain\Bundle\TaxBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;



class EditTaxMappingType extends AbstractType {
	public function __construct($allTaxMasterId,$country,$state,$cat,$confi,$taxMapId,$taxDetail,$action)
	{
		/*print_r($allTaxMasterId);
		print_r($country);print_r($state);print_r($cat);print_r($confi);print_r($taxMapId);
		exit();*/ 
		$this->taxMaster = array();
		foreach ($allTaxMasterId as $masterDetail)
		{
			$this->taxMaster[$masterDetail['id']] = $masterDetail['title'];
		}
		$this->country = $country;
		$this->state = $state;
		$this->cat=$cat;
		$this->confi = $confi;
		$this->taxMapId = $taxMapId;
		$this->taxDetail = $taxDetail;
		$this->action = $action;
	}
	/**
	 *
	 * @param FormBuilderInterface $builder
	 * @param array $options        	
	 */
	public function buildForm(FormBuilderInterface $builder, array $options) 
	{
		if(is_array($this->taxDetail)){
			$taxid = $this->taxDetail['taxMappingId'];
			if(isset($this->taxDetail['countryId']))
			{
				$countryId=$this->taxDetail['countryId'];
			}
			else
			{
				$countryId = '';
			}
			if(isset($this->taxDetail['stateId']))
			{
				$stateId=$this->taxDetail['stateId'];
			}
			else
			{
				$stateId = '';
			}
			if(isset($this->taxDetail['categoryId']))
			{
				$categoryId=$this->taxDetail['categoryId'];
			}
			else
			{
				$categoryId = '';
			}

			$this->taxMapId = $this->taxDetail['id'];
		}else{
			$taxid =$this->taxDetail->getTaxId()->getId();
			$countryId = $this->taxDetail->getCountryId();
			$stateId =$this->taxDetail->getStateId();
			$categoryId = $this->taxDetail->getCategoryId();
		}
		//echo $this->taxDetail->getTaxId()->getId();die;
		$builder->add ('taxMappingId' , 'choice', array(
				'choices' => $this->taxMaster , 'multiple'=>false,
				'data' => $taxid,
				'required'  => false,
				'placeholder'=>'admin.tax_mapping.edit_tax_mapping.tax_title_form.select',
				'constraints' => array(
						new NotBlank(array('message'=>'admin.tax_mapping.edit_tax_mapping.tax_title_form.validation')),
				),
		));
		
		if($this->confi['state']['status']== 1 )
		{
			$builder->add ('countryId' , 'choice', array(
					'choices' => $this->country , 'multiple'=>false,
					'required'  => false,
					'data' => $countryId,
					'placeholder'=>'admin.tax_mapping.edit_tax_mapping.tax_country_form.select',
					/*'constraints' => array(
							new NotBlank(array('message'=>'admin.tax_mapping.edit_tax_mapping.tax_country_form.validation')),
					),*/
			))
			->add ('stateId' , 'choice', array(
					'choices' => $this->state , 'multiple'=>false,
					'required'  => false,
					//'data' => $this->state,
					'data' => $stateId,
					'placeholder'=>'admin.tax_mapping.edit_tax_mapping.tax_state_form.select',
					/*'constraints' => array(
							new NotBlank(array('message'=>'admin.tax_mapping.edit_tax_mapping.tax_state_form.validation')),
					),*/
			))
			;
		}
		else
		{
			$builder->add ('countryId' , 'hidden')
			->add ('stateId' , 'hidden');
		}
		
		if($this->confi['category']['status']== 1 )
		{
			$builder->add ('categoryId' , 'choice', array(
					'choices' => $this->cat , 'multiple'=>false,
					'required'  => false,
					//'data' => $this->cat,
					'data' => $categoryId,
					'placeholder'=>'admin.tax_mapping.edit_tax_mapping.tax_category_form.select',
					/*'constraints' => array(
							new NotBlank(array('message'=>'admin.tax_mapping.edit_tax_mapping.tax_category_form.validation')),
					),*/
			));
		}
		else
		{
			$builder->add ('categoryId' , 'hidden');
		}
		
		if($this->confi['product']['status']== 1 )
		{
			$builder->add ('productSku' , 'text', array(
							'required'  => false,
							/*'constraints' => array(
									new NotBlank(array('message'=>'admin.tax_mapping.edit_tax_mapping.tax_sku_form.validation')),
							)*/
	                   ));
		}
		else
		{
			$builder->add ('productId' , 'hidden');
		}
		if($this->action == 'edit'){
			$builder->add ( 'id' , 'hidden', array('data' => $this->taxMapId));
		}
	}
	
	/**
	 * @param OptionsResolverInterface $resolver
	 */
	public function setDefaultOptions(OptionsResolverInterface $resolver)
	{
		$resolver->setDefaults(array(
				'data_class' => 'OfficeBrain\Bundle\TaxBundle\Entity\TaxMapping',
				'allow_extra_fields' => true
		));
	}
	
	/**
	 *
	 * @return string
	 */
	public function getName() {
		return 'edit_tax_mapping';
	}
	public function setForm($form){
    	$this->form = $form;
    }

}