<?php

class Sistema_Form_Funcionario extends Agro_Form {
	
	public function init() {

		$this->addElement('hidden', 'id', array(
			'decorators' => $this->elementDecorator
		));
		
		$this->addElement('hidden', 'id_usuario', array(
			'decorators' => $this->elementDecorator
		));
		
		$this->addElement('text', 'ds_nome_funcionario', array(
			'decorators' => $this->elementDecorator,
			'required' => true,
			'maxlength' => 100,
			'label' => 'Nome',
			'size' => 70
		));

		$this->addElement('text', 'ds_nome_guerra', array(
			'decorators' => $this->elementDecorator,
			'required' => false,
			'maxlength' => 100,
			'label' => 'Nome Social',
			'size' => 30
		));
		
		$this->addElement('text', 'cpf_cnpj', array(
			'decorators' => $this->elementDecorator,
			'label' => 'CPF',
			'class' => 'cpf_cnpj',
			'required' => true,
			'maxlength' => 15,
			'size' => 15
		));
		
		$this->addElement('text', 'dt_nascimento', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Dt. Nascimento',
			'required' => true,
			'maxlength' => 10,
			'class' => 'data',
			'size' => 11
		));
		
		$Empresa = new Sistema_Model_Empresas();
		$this->addElement('select', 'id_empresa', array(
			'MultiOptions' => $Empresa->getCombo(array('EMPRESA', 'SEGURADORA')),
			'decorators' => $this->elementDecorator,
			'label' => 'Empresa',
			'required' => true
		));
		
		$this->addElement('select', 'id_departamento', array(
			'decorators' => $this->elementDecorator,
			'RegisterInArrayValidator' => false,
			'label' => 'Departamento',
			'required' => true
		));
		
		$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_rua', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Endereço',
			'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_complemento', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Comp.',
			'maxlength' => 100,
			'size' => 10
		));
		
		$this->addElement('text', 'ds_email', array(
			'validators' => array(array('validator'=> new Zend_Validate_EmailAddress())),
			'decorators' => $this->elementDecorator,
			'label' => 'E-mail',
			'required' => true,
			'maxlength' => 100,
			'class' => 'email',
			'size' => 100
		));

		$Roles = new Sistema_Model_Roles();
		$this->addElement('select', 'id_role', array(
			'MultiOptions' => $Roles->getComboFuncionario(),
			'decorators' => $this->elementDecorator,
			'label' => 'Perfil',
			'required' => true
		));
		
		$this->addElement('select', 'tp_usuario', array(
			'MultiOptions' => array('F' => 'Funcionário'),
			'decorators' => $this->elementDecorator,
			'label' => 'Tipo',
			'required' => true
		));
			
		$this->addElement('text', 'ds_nome_usuario', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Login',
			'required' => true,
			'maxlength' => 100,
			'class' => 'lower',
			'size' => 40
		));
		
		$this->addElement('text', 'ds_senha', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Senha',
			'required' => false,
			'size' => 20
		));
		
		$this->addElement('text', 'ds_senha2', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Confirmar Senha',
			'required' => false,
			'maxlength' => 20,
			'size' => 20
		));
		
		$this->addElement('textarea', 'ds_observacao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Observações',
			'cols' => 50,
			'rows' => 5
		));
		
		$this->addElement('hidden', 'fl_responsavel_empresa', array(
			'decorators' => $this->elementDecorator
		));

        $this->addElement('checkbox', 'fl_liberado_tablet_previa', array(
            'decorators' => $this->elementDecorator,
            'label' => 'Ativar Prévia Digital?'
		));
		
		$this->addElement('checkbox', 'fl_usuario_tagh', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Incluir usuário na Tagh?'
		));

        $this->addElement('text', 'deviceid', array(
          'decorators' => $this->elementDecorator,
          'label' => 'Dispositivo Vinculado',
          'readonly' => 'readonly',
          'maxlength' => 60,
          'size' => 60
        ));

		$Categoria = new Sistema_Model_Categorias();
		$this->addElement('select', 'id_categoria', array(
			'MultiOptions' => $Categoria->fetchPairs(
				'id',
				'ds_nome_categoria',
				false,
				false,
				1,
				'id <> '. Sistema_Model_Categorias::ID_EMPRESA_VISTORIA
			),
			'decorators' => $this->elementDecorator,
			'label' => 'Categoria',
			'required' => false
		));

        $this->addElement('checkbox', 'fl_agronomo', array(
            'decorators' => $this->elementDecorator,
            'label' => 'Agrônomo'
        ));

		$this->addElement('checkbox', 'fl_liberado_tablet_sinistro', array(
            'decorators' => $this->elementDecorator,
            'label' => 'Ativar Sinistro Digital?'
        ));

        $this->addElement('text', 'deviceid_sinistro', array(
            'decorators' => $this->elementDecorator,
            'label' => 'Dispositivo Vinculado',
            'readonly' => 'readonly',
            'maxlength' => 60,
            'size' => 60
        ));
	}
	
	public function populate(array $values) {
		// Formata a data

		if ($values['dt_nascimento']) {
			$values['dt_nascimento'] = Agro_Util::formatDate($values['dt_nascimento'], Zend_Date::DATES);
		}
		
		// Preenche o campo de departamento
		if ($values['id_empresa']) {
			$Departamentos = new Sistema_Model_Departamentos();
			$Departamento = $Departamentos->find($values['id_empresa'])->current();
			$this->id_departamento->addMultiOptions($Departamentos->fetchPairs($values['id_empresa']));
			//print_r($Departamentos->fetchPairs($values['id_empresa']));
		}

		// Preenche os municipios e o estado
		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;
		}
		
		// Preenche os campos do usuario
		if ($values['id_usuario']) {
			$Usuarios = new Sistema_Model_Usuarios();
			$Usuario = $Usuarios->find($values['id_usuario'])->current();

			$values['ds_nome_usuario'] = $Usuario->ds_nome_usuario;
			$values['tp_usuario'] = $Usuario->tp_usuario;
			$values['ds_senha2'] = $Usuario->ds_senha;
			$values['ds_senha'] = $Usuario->ds_senha;
			$values['id_role'] = $Usuario->id_role;
			$values['fl_liberado_tablet_previa'] = $Usuario->fl_liberado_tablet_previa;
			$values['fl_liberado_tablet_sinistro'] = $Usuario->fl_liberado_tablet_sinistro;
			$values['deviceid'] = $Usuario->deviceid;
			$values['deviceid_sinistro'] = $Usuario->deviceid_sinistro;
		}
		
		$values['ds_senha'] = '';
		$values['ds_senha2'] = '';

		parent::populate($values);
	}
}