<?php

class Seguro_Form_Proponente extends Agro_Form {

	public function init() {
		$OrgaoExpedidor = new Sistema_Model_OrgaosExpedidores();
		$EstadoCivil = new Sistema_Model_EstadosCivis();
		$Estado = new Sistema_Model_Estados();

		$this->addElement('hidden', 'id_proponente', array('decorators' => $this->buttonDecorator));

		$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(
			'decorators' => $this->elementDecorator,
			'validators' => array(array('validator'=> new Agro_Validate_CpfCnpj())),
			'label' => 'CPF/CNPJ',
			'class' => 'cpf_cnpj',
			'required' => true,
			'maxlength' => 18,
			'size' => 19
		));

		$this->addElement('select', 'tp_segurado', array(
			'MultiOptions' => array('', 'P'=>'PROPRIETÁRIO','A'=>'ARRENDATÁRIO', 'O'=>'OUTROS'),
			'decorators' => $this->elementDecorator,
			'label' => 'Tipo Segurado'
		));

		$this->addElement('text', 'ds_nome_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Nome Proponente',
			'required' => true,
			'maxlength' => 100,
			'size' => 70
		));

		$this->addElement('text', 'ds_email_proponente', array(
			//'validators' => array(array('validator'=> new Zend_Validate_EmailAddress())),
			'decorators' => $this->elementDecorator,
			'label' => 'E-mail',
			'maxlength' => 100,
			'class' => 'email',
			'size' => 50
		));

		$this->addElement('select', 'ds_sexo', array(
			'MultiOptions' => array('', 'F'=>'FEMININO','M'=>'MASCULINO'),
			'decorators' => $this->elementDecorator,
			'label' => 'Sexo',
		));

		$this->addElement('text', 'dt_nascimento', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Dt. Nascimento',
			'class' => 'data',
			'maxlength' => 10,
			'size' => 11
		));

		$this->addElement('select', 'id_estado_civil', array(
			'MultiOptions' => $EstadoCivil->getCombo(),
			'decorators' => $this->elementDecorator,
			'label' => 'Estado Civil'
		));

		$this->addElement('select', 'ds_documento_natureza', array(
			'MultiOptions' => array('RG' => 'RG', 'RNE' => 'RNE'),
			'decorators' => $this->elementDecorator,
			'label' => 'Doc. Identificação',
		));

		$this->addElement('text', 'ds_documento_numero', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Nº Documento',
			'class' => 'integer',
			'maxlength' => 12,
			'size' => 12
		));

		$this->addElement('select', 'id_orgao_expedidor', array(
			'MultiOptions' => $OrgaoExpedidor->getCombo(),
			'decorators' => $this->elementDecorator,
			'label' => 'Órgão Expedidor',
		));

		$this->addElement('text', 'dt_documento_exp', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Dt. Expedição',
			'class' => 'data',
			'maxlength' => 10,
			'size' => 11
		));

		$this->addElement('text', 'ds_endereco_cep_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'CEP',
			'maxlength' => 10,
			'required' => true,
			'class' => 'cep',
			'size' => 10
		));

		$this->addElement('text', 'ds_endereco_rua_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Endereço',
			'maxlength' => 100,
			'required' => true,
			'class' => 'upper',
			'size' => 32
		));

		$this->addElement('text', 'ds_endereco_nro_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Número',
			'class' => 'integer',
			'required' => true,
			'maxlength' => 8,
			'size' => 8
		));

		$this->addElement('text', 'ds_endereco_complem_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Complemento',
			'maxlength' => 100,
			'class' => 'upper',
			'size' => 30
		));

		$this->addElement('text', 'ds_endereco_bairro_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Bairro',
			'required' => true,
			'maxlength' => 30,
			'class' => 'upper',
			'size' => 30
		));

		$this->addElement('text', 'ds_nome_estado_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'UF',
			'disabled' => true,
			'maxlength' => 2,
			'class' => 'upper',
			'size' => 2
		));

		$this->addElement('text', 'ds_nome_municipio_proponente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Município',
			'disabled' => true,
			'maxlength' => 255,
			'class' => 'upper',
			'size' => 60
		));

		$this->addElement('hidden', 'id_estado_proponente', array(
			'decorators' => $this->buttonDecorator,
			'label' => 'Estado',
			'required' => true
		));

		$this->addElement('hidden', 'id_municipio_proponente', array(
			'decorators' => $this->buttonDecorator,
			'label' => 'Município',
			'required' => true
		));

		$this->addElement('text', 'ds_tel_residencia', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Residencial',
			'class' => 'telefone',
			'maxlength' => 20,
			'required' => false,
			'size' => 14
		));

		$this->addElement('text', 'ds_tel_propriedade', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Propriedade',
			'class' => 'telefone',
			'maxlength' => 20,
			'required' => false,
			'size' => 14
		));

		$this->addElement('text', 'ds_tel_celular', array(
			'decorators' => $this->elementDecorator,
			'class' => 'telefone',
			'label' => 'Celular',
			'maxlength' => 20,
			'required' => false,
			'size' => 14
		));

		$this->addElement('checkbox', 'fl_inadimplente', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Inadimplente'
		));

		$this->addElement('checkbox', 'fl_validacao_receita', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Validação Receita'
		));
		
		$this->addElement('checkbox', 'fl_restricao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Produtor com proposta em Moderação de Negócio'
		));

        $this->addElement('checkbox', 'fl_programa_abc', array(
            'decorators' => $this->elementDecorator,
            'label' => 'Produtor pertencente ao programa RENOVAGRO'
        ));

		$this->addElement('text', 'dt_vencimento_programa_abc', array(
            'decorators' => $this->elementDecorator,
            'label' => 'Dt. de vencimento RENOVAGRO',
			'class' => 'data',
			'maxlength' => 10,
			'size' => 11
        ));

		$this->addElement('text', 'id_proposta', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Proposta',
			'size' => 10,
			'class' => 'integer'
		));

		$this->addElement('textarea', 'ds_descricao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Relato',
			'style' => 'width:450px;height:150px;'
		));

		$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
		));
	}

	public function populate(array $values) {
		if ($values['id_municipio_proponente']) {
			$values['id_municipio'] = $values['id_municipio_proponente'];
		}

		if ($values['id_municipio']) {
			$Municipios = new Sistema_Model_Municipios();
			$Municipio = $Municipios->get($values['id_municipio']);

			$values['ds_endereco_rua_proponente'] = ($values['ds_endereco_rua'] ? $values['ds_endereco_rua'] : $values['ds_endereco_rua_proponente']);
			$values['ds_endereco_nro_proponente'] = ($values['ds_endereco_nro'] ? $values['ds_endereco_nro'] : $values['ds_endereco_nro_proponente']);
			$values['ds_endereco_complem_proponente'] = ($values['ds_endereco_complem'] ? $values['ds_endereco_complem'] : $values['ds_endereco_complem_proponente']);
			$values['ds_endereco_bairro_proponente'] = ($values['ds_endereco_bairro'] ? $values['ds_endereco_bairro'] : $values['ds_endereco_bairro_proponente']);
			$values['ds_endereco_cep_proponente'] = ($values['ds_endereco_cep'] ? $values['ds_endereco_cep'] : $values['ds_endereco_cep_proponente']);
			$values['id_municipio_proponente'] = ($values['id_municipio'] ? $values['id_municipio'] : $values['id_municipio_proponente']);
			$values['id_estado_proponente'] = $Municipio->id_estado;

			$values['ds_nome_estado_proponente'] = $Municipio->ds_sigla;
			$values['ds_nome_municipio_proponente'] = $Municipio->ds_nome_municipio;

		}

		$values['dt_documento_exp'] = Agro_Util::formatDate($values[dt_documento_exp], Zend_Date::DATES);
		$values['dt_nascimento'] = Agro_Util::formatDate($values[dt_nascimento], Zend_Date::DATES);

		if ($values['id']) {
			$values['id_proponente'] = $values['id'];
		}

		parent::populate($values);
	}

	public function isValid($data)
    {
		$isValid = parent::isValid($data);

		if ($isValid) {
			if ($data['fl_programa_abc'] && empty($data['dt_vencimento_programa_abc'])) {
				$this->dt_vencimento_programa_abc->addError("Deve ser informado uma data quando marcado que o Produtor pertence ao programa RENOVAGRO.");
				$isValid = false;
			}
		}

        return $isValid;
    }
}

