<?php

class Seguro_Form_Beneficiario extends Agro_Form {

	public function init() {
	    $usuarioAgro = Sistema_Model_Usuarios::usuarioLogadoFuncionarioAgro();

		$this->addElement('hidden', 'id_beneficiario', array('decorators' => $this->buttonDecorator));
		$this->addElement('hidden', 'id', array('decorators' => $this->buttonDecorator));

        $this->addElement('text', 'idBeneficiario', array(
            'decorators' => $this->elementDecorator,
            'label' => 'ID Beneficiário',
            'class' => 'integer',
            'maxlength' => 10,
            'size' => 10
        ));

		$this->addElement('select', 'tp_pessoa', array(
			'MultiOptions' => array('' => '', 'F'=>'FÍSICA','J'=>'JURÍDICA'),
			'decorators' => $this->elementDecorator,
			'label' => 'Tipo Pessoa',
			'required' => true
		));

		$this->addElement('text', 'cpf_cnpj', array(
			'validators' => array(array('validator'=> new Agro_Validate_CpfCnpj())),
			'decorators' => $this->elementDecorator,
			'label' => 'CPF/CNPJ',
			'class' => 'cpf_cnpj',
			'maxlength' => 18,
			'size' => 19,
			'required' => true
		));

		$this->addElement('text', 'ds_nome_beneficiario', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Nome Beneficiário',
			'required' => true,
			'maxlength' => 300,
			'size' => 70
		));

		$Status = new Sistema_Model_Status();
		$arStatus = array(0 => 'Todos') + $Status->getComboTipo('ARQUIVOS_PROCESSO_SINISTRO'); 

		$this->addElement('select', 'id_status', array(
			'MultiOptions' => $arStatus,
			'decorators' => $this->elementDecorator,
			'label' => 'Status'
		));

		$classTipoBeneficiario = !$usuarioAgro ? 'element_disabled readonly_background_disabled' : '';

        $this->addElement('select', 'id_tipo_beneficiario', array(
          'MultiOptions' => (new Seguro_Model_TiposBeneficiarios())->getCombo(),
          'decorators' => $this->elementDecorator,
          'label' => 'Tipo de Beneficiário',
          'class' => $classTipoBeneficiario,
          'required' => true
        ));

		$Funcionarios = new Sistema_Model_Funcionarios();
		$funcs[0] = '';
		$this->addElement('select', 'id_funcionario_agronomo', array(
			'MultiOptions' => $funcs + $Funcionarios->getCombofuncionarios(23),
			'decorators' => $this->elementDecorator,
			'label' => 'Eng. Agrônomo'
		));

		$this->addElement('select', 'id_funcionario_perito', array(
			'MultiOptions' => $Funcionarios->getComboVistoriadores(),
			'decorators' => $this->elementDecorator,
			'label' => 'Vistoriador'
		));

		$Corretores = new Sistema_Model_Corretores();
		$this->addElement('select', 'id_corretor', array(
			'MultiOptions' => $Corretores->fetchPairs("id", "ds_nome_abreviado", false, false, 1, "fl_del = false and fl_ativo = true", true),
			'decorators' => $this->elementDecorator,
			'label' => 'Corretor',
			'required' => true
		));

		/**Inputs de endereço */
		$this->addElement('hidden', 'id_estado', array('decorators' => $this->elementDecorator));
		$this->addElement('text', 'ds_nome_estado', array(
			'decorators' => $this->elementDecorator,
			'label' => 'UF',
			'disabled' => true,
			'maxlength' => 2,
			'size' => 2
		));
		$this->addElement('hidden', 'id_municipio', array('decorators' => $this->elementDecorator));
		$this->addElement('text', 'ds_nome_municipio', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Município',
			'disabled' => true,
			'maxlength' => 30,
			'size' => 78
		));
		$this->addElement('text', 'ds_endereco_logradouro', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Logradouro',
			'maxlength' => 100,
			'size' => 40,
			
		));
		
		$this->addElement('text', 'ds_endereco_nro', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Número',
			'maxlength' => 100,
			'size' => 10
		));
		
		$this->addElement('text', 'ds_endereco_bairro', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Bairro',
			'maxlength' => 100,
			'size' => 40
		));
		
		$this->addElement('text', 'ds_endereco_cep', array(
			'decorators' => $this->elementDecorator,
			'label' => 'CEP',
			'class' => 'cep',
			'maxlength' => 9,
			'size' => 9
		));
		
		$this->addElement('text', 'ds_endereco_complem', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Complemento',
			'maxlength' => 100,
			'size' => 25
		));

		$this->addElement('text', 'ds_tel', array(
		    'decorators' => $this->elementDecorator,
		    'label' => 'Telefone',
    		'class' => 'telefone',
    		'maxlength' => 13,
    		'size' => 14
		));

		$this->addElement('text', 'ds_email', array(
			'decorators' => $this->elementDecorator,
			'label' => 'E-mail',
			'maxlength' => 100,
			'size' => 40,
			'class' => 'validarEmail'
		));
	
		/**Inputs de endereço */
	
	}

	public function populate(array $values) {
		$values['id_beneficiario'] = $values['id'];

		if ($values['id_municipio']) {
			$Municipios = new Sistema_Model_Municipios();
			$Estados = new Sistema_Model_Estados();

			$Municipio = $Municipios->find($values['id_municipio'])->current();
			$Estado = $Estados->find($Municipio->id_estado)->current();

			$values['ds_nome_municipio'] = $Municipio->ds_nome_municipio;
			$values['ds_nome_estado'] = $Estado->ds_sigla;
			$values['id_estado'] = $Municipio->id_estado;
			$values['id_municipio'] = $Municipio->id;
		}

		parent::populate($values);
	}
	
	public function isValid($params){
		return parent::isValid($params);
	}
}

