<?php

class Sistema_Form_Empresa extends Agro_Form {
	public function init() {
		$this->addElement('hidden', 'id', array('decorators' => $this->elementDecorator));
		$this->addElement('hidden', 'tp_pessoa', array('decorators' => $this->elementDecorator, 'value' => 'J'));

		$this->addElement('text', 'ds_nome_fantasia', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Nome Fantasia',
			'required' => true,
			'class' => 'upper',		
			'maxlength' => 100,
			'size' => 50,
		));
		
		$this->addElement('text', 'id_empresa', array(
				'decorators' => $this->elementDecorator,
				'label' => 'Usuário de Acesso: ',
				'size' => 4,
				'readonly' => true
		));		
		
		$this->addElement('text', 'cod_acesso_mobile', array(
				'decorators' => $this->elementDecorator,
				'label' => 'Código de Acesso: ',
				'size' => 4,
				'readonly' => true
		));		
		
		$this->addElement('text', 'ds_razao_social', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Razão Social',
			'required' => true,
			'class' => 'upper',
			'maxlength' => 100,
			'size' => 50,
		));
		
		$this->addElement('text', 'cpf_cnpj', array(
			'decorators' => $this->elementDecorator,
			'label' => 'CNPJ',
			'class' => 'cpf_cnpj',
			'required' => true,
			'maxlength' => 18,
			'size' => 20,
		));

		$this->addElement('text', 'cnpj_search', array(
			'decorators' => $this->elementDecorator,
			'label' => 'CNPJ',
			'class' => 'cpf_cnpj',
			'required' => false,
			'maxlength' => 18,
			'size' => 20,
		));
		
		$Categoria = new Sistema_Model_Categorias();
		$this->addElement('select', 'id_categoria', array(
			'MultiOptions' => $Categoria->fetchPairs('id', 'ds_nome_categoria'),
			'decorators' => $this->elementDecorator,
			'label' => 'Categoria',
			'required' => true
		));
		
		$this->addElement('text', 'ds_endereco_cep', array(
			'decorators' => $this->elementDecorator,
			'label' => 'CEP',
			'class' => 'cep'
		));
		
		$this->addElement('hidden', 'id_estado', array('required' => true, 'decorators' => $this->elementDecorator));
		$this->addElement('text', 'ds_nome_estado', array(
			'decorators' => $this->elementDecorator,
			'label' => 'UF',
			'required' => true,
			'readonly' => true,
			'maxlength' => 2,
			'size' => 2
		));
		
		$Estados = new Sistema_Model_Estados();
		$this->addElement('select', 'id_estado_pesquisa', array(
			'MultiOptions' => $Estados->getCombo(),
			'decorators' => $this->elementDecorator,
			'label' => 'Estado',
			'required' => false
		));
		
		$this->addElement('hidden', 'id_municipio', array('required' => true, 'decorators' => $this->elementDecorator));
		$this->addElement('text', 'ds_nome_municipio', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Município',
			'required' => true,
			'readonly' => true,
			'maxlength' => 30,
			'size' => 78,
		));
		
		$this->addElement('text', 'ds_endereco_bairro', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Bairro',
			'required' => true,
			'maxlength' => 30,
			'size' => 30,
		));
		
		$this->addElement('text', 'ds_endereco_rua', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Endereço',
			'required' => true,
			'maxlength' => 100,
			'size' => 50,
		));
		
		$this->addElement('text', 'ds_endereco_nro', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Número',
			'required' => true,
			'maxlength' => 8,
			'size' => 10,
		));
		
		$this->addElement('text', 'ds_endereco_complemento', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Complemento',
			'maxlength' => 100,
			'size' => 70,
		));
		
		$this->addElement('text', 'ds_telefone', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Telefone',
			'class' => 'telefone'
		));
		
		$this->addElement('text', 'ds_telefone_fax', array(
			'decorators' => $this->elementDecorator,
			'label' => 'FAX',
			'class' => 'telefone'
		));
		
		$this->addElement('text', 'ds_telefone_adicional', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Telefone Adicional',
			'class' => 'telefone'
		));
		
		$this->addElement('textarea', 'ds_observacao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Observações',
			'cols' => 100,
			'rows' => 10
		));
		
		$this->addElement('text', 'ds_email', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Email',
			'maxlength' => 100,
			'size' => 70,		
			'required' => true
		));

		$this->addElement('text', 'ds_email_responsavel', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Email Responsável',
			'maxlength' => 100,
			'size' => 70,		
			'required' => false
		));

		$this->addElement('text', 'ds_nome_responsavel', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Nome Responsável',
			'maxlength' => 100,
			'size' => 70,		
			'required' => false
		));

		$Tipo = new Sistema_Model_TipoArquivoEmpresa();
		$this->addElement('select', 'id_tipo_arquivo', array(
			'MultiOptions' => $Tipo->getCombo(),
			'decorators' => $this->elementDecorator,
			'label' => 'Tipo de Documento'
		));

        $this->addElement('text', 'id_codigo_ibge', array(
            'decorators' => $this->elementDecorator,
            'label' => 'Código Município IBGE',
            'maxlength' => 7,
            'size' => 7,
            'class' => 'integer',
            'required' => true
        ));

        $modelFuncionarios = new Sistema_Model_Funcionarios();
        $this->addElement('select', 'id_agronomo', array(
            'decorators' => $this->elementDecorator,
            'MultiOptions' => $modelFuncionarios->getComboFuncionariosAgronomos(),
            'label' => 'Agrônomo'
        ));
	}
	
	public function populate(array $values) {
		if ($values['id_municipio']) {
			$Municipios = new Sistema_Model_Municipios();
			$Municipio = $Municipios->get($values['id_municipio']);
			$values['id_estado'] = $Municipio['id_estado'];
			
			$this->ds_nome_municipio->setValue($Municipio['ds_nome_municipio']);
			$this->ds_nome_estado->setValue($Municipio['ds_sigla']);
		}
		
		if($values['cod_acesso_mobile']){
			$this->id_empresa->setValue($values['id']);
		}

		parent::populate($values);
	}
}