<?

class Produto_Form_Atributo extends Agro_Form {
	
	public function init() {
		$this->id = $this->createElement('hidden', 'id');
		$this->id->setDecorators($this->buttonDecorator);
		$this->addElement($this->id);
		
		$AtributoModulo = new Produto_Model_AtributosModulos();
		$this->id_atributo_modulo = $this->createElement('select', 'id_atributo_modulo');
		$this->id_atributo_modulo->setDecorators($this->elementDecorator);
		$this->id_atributo_modulo->addMultiOptions($AtributoModulo->getCombo(0));
		$this->id_atributo_modulo->setRequired(true);
		$this->id_atributo_modulo->setLabel('Módulo');
		$this->addElement($this->id_atributo_modulo);
		
		$Atributos = new Produto_Model_Atributos();
		$this->id_parent = $this->createElement('select', 'id_parent');
		$this->id_parent->setDecorators($this->elementDecorator);
		$this->id_parent->addMultiOptions($Atributos->fetchPairs('id', 'ds_descricao_atributo', 'id_parent', 0, 1, 'id_atributo_modulo in (2,6)'));
		$this->id_parent->setLabel('Depende de:');
		$this->addElement($this->id_parent);
		
		$AtributoTipo = new Produto_Model_AtributosTipos();
		$this->id_tipo_atributo = $this->createElement('select', 'id_tipo_atributo');
		$this->id_tipo_atributo->setDecorators($this->elementDecorator);
		$this->id_tipo_atributo->addMultiOptions($AtributoTipo->getCombo());
		$this->id_tipo_atributo->setRequired(true);
		$this->id_tipo_atributo->setLabel('Tipo');
		$this->addElement($this->id_tipo_atributo);
		
		$this->ds_parent_value = $this->createElement('text', 'ds_parent_value');
		$this->ds_parent_value->setDecorators($this->elementDecorator);
		$this->ds_parent_value->setAttrib('size', '5');
		$this->ds_parent_value->setAttrib('maxlength', '255');
		$this->ds_parent_value->setLabel('Valor');
		$this->addElement($this->ds_parent_value);
		
		$this->ds_chave_rn = $this->createElement('text', 'ds_chave_rn');
		$this->ds_chave_rn->setDecorators($this->elementDecorator);
		$this->ds_chave_rn->setRequired(true);
		$this->ds_chave_rn->setAttrib('size', '50');
		$this->ds_chave_rn->setAttrib('maxlength', '100');
		$this->ds_chave_rn->setLabel('Chave');
		$this->addElement($this->ds_chave_rn);

		$this->ds_validar_campo = $this->createElement('text', 'ds_validar_campo');
		$this->ds_validar_campo->setDecorators($this->elementDecorator);
		$this->ds_validar_campo->setAttrib('size', '50');
		$this->ds_validar_campo->setAttrib('maxlength', '100');
		$this->ds_validar_campo->setLabel('Validar campo:');
		$this->addElement($this->ds_validar_campo);
		
		$this->ds_validar_campo_valor = $this->createElement('text', 'ds_validar_campo_valor');
		$this->ds_validar_campo_valor->setDecorators($this->elementDecorator);
		$this->ds_validar_campo_valor->setAttrib('size', '50');
		$this->ds_validar_campo_valor->setAttrib('maxlength', '100');
		$this->ds_validar_campo_valor->setLabel('Quando o valor for: ');
		$this->addElement($this->ds_validar_campo_valor);
		
		$this->fl_atendimento = $this->createElement('select', 'fl_atendimento');
		$this->fl_atendimento->setDecorators($this->elementDecorator);
		$this->fl_atendimento->addMultiOptions(array('0' => 'Não', '1' => 'Sim'));
		$this->fl_atendimento->setLabel('Exclusivo Atendimento');
		$this->addElement($this->fl_atendimento);
		
		$this->fl_permite_exclusao = $this->createElement('select', 'fl_permite_exclusao');
		$this->fl_permite_exclusao->setDecorators($this->elementDecorator);
		$this->fl_permite_exclusao->addMultiOptions(array('0' => 'Não', '1' => 'Sim'));
		$this->fl_permite_exclusao->setLabel('Permitir Exclusão');
		$this->addElement($this->fl_permite_exclusao);
				
		$this->fl_required = $this->createElement('select', 'fl_required');
		$this->fl_required->setDecorators($this->elementDecorator);
		$this->fl_required->addMultiOptions(array('0' => 'Não', '1' => 'Sim'));
		$this->fl_required->setLabel('Obrigatório');
		$this->addElement($this->fl_required);
		
		$this->ds_valores_opcoes = $this->createElement('text', 'ds_valores_opcoes');
		$this->ds_valores_opcoes->setDecorators($this->elementDecorator);
		$this->ds_valores_opcoes->setAttrib('size', '41');
		$this->ds_valores_opcoes->setAttrib('maxlength', '1000');
		$this->ds_valores_opcoes->setLabel('Valores');
		$this->addElement($this->ds_valores_opcoes);
		
		$this->ds_descricao_atributo = $this->createElement('textarea', 'ds_descricao_atributo');
		$this->ds_descricao_atributo->setDecorators($this->elementDecorator);
		$this->ds_descricao_atributo->setAttrib('rows', '5');
		$this->ds_descricao_atributo->setAttrib('cols', '48');
		$this->ds_descricao_atributo->setLabel('Descrição');
		$this->addElement($this->ds_descricao_atributo);
		
		$this->ds_schema = $this->createElement('select', 'ds_schema');
		$this->ds_schema->setDecorators($this->elementDecorator);
		$this->ds_schema->setRegisterInArrayValidator(false);
		$this->ds_schema->addMultiOptions(Agro_Util::getSchemas());
		$this->ds_schema->setLabel('Schema');
		$this->addElement($this->ds_schema);
		
		$this->ds_table = $this->createElement('select', 'ds_table');
		$this->ds_table->setDecorators($this->elementDecorator);
		$this->ds_table->setRegisterInArrayValidator(false);
		$this->ds_table->setLabel('Tabela');
		$this->addElement($this->ds_table);
		
		$this->ds_option_value = $this->createElement('select', 'ds_option_value');
		$this->ds_option_value->setDecorators($this->elementDecorator);
		$this->ds_option_value->setRegisterInArrayValidator(false);
		$this->ds_option_value->setLabel('Valor');
		$this->addElement($this->ds_option_value);
		
		$this->ds_option_text = $this->createElement('select', 'ds_option_text');
		$this->ds_option_text->setDecorators($this->elementDecorator);
		$this->ds_option_text->setRegisterInArrayValidator(false);
		$this->ds_option_text->setLabel('Texto');
		$this->addElement($this->ds_option_text);
		
		$this->ds_option_filter = $this->createElement('text', 'ds_option_filter');
		$this->ds_option_filter->setDecorators($this->elementDecorator);
		$this->ds_option_filter->setLabel('Filtro');
		$this->addElement($this->ds_option_filter);
		
		$this->save = $this->createElement('button', 'salvar', array('type'=>'submit'));
		$this->save->setDecorators($this->buttonDecorator);
		$this->save->setDecorators($this->buttonDecorator);
		$this->save->setLabel('Salvar');
		$this->addElement($this->save);
		
		$this->cancel = $this->createElement('button', 'cancelar', array('onclick'=>"location.href='/produto/atributo'"));
		$this->cancel->setDecorators($this->buttonDecorator);
		$this->cancel->setLabel('Cancelar');
		$this->addElement($this->cancel);
	}
	
	public function populate(array $data) {
		
		if (array_key_exists("ds_schema", $data)) {
			if ($data['ds_schema']) {
				$this->ds_table->addMultiOptions(Agro_Util::getTables($data['ds_schema']));
			}
		}

		if (array_key_exists("ds_table", $data)) {
			if ($data['ds_table']) {
				$this->ds_option_value->addMultiOptions(Agro_Util::getColumns($data['ds_table']));
				$this->ds_option_text->addMultiOptions(Agro_Util::getColumns($data['ds_table']));
			}
		}
		
		parent::populate($data);
	}
}