<?php

class Sinistro_Form_LaudoFinal extends Agro_Form {  
    private $_readOnly = null;
	private $_readOnlyAll = null;

    public function init($idLaudoFinal = null) {
   	
		$dados = $this->getDados();

		$Configuracoes = array(
			'fl_exibe_botao_enviar'=>false,
			'fl_exibe_botao_devolver'=>false
		);
    	
		if($idLaudoFinal) {
			
			$Configuracoes = $this->getConfiguracoes($idLaudoFinal, $dados);
			
			switch ($Configuracoes['readonly']) {
				case 'readOnly':
					$this->setReadOnly(true);
					break;
				case 'readOnlyAll':
					$this->setReadOnly(true);
					$this->setReadOnlyAll(true);
					break;
				default:
				break;
			}

		}
		
		$Configuracoes2['fl_exibe_botao_enviar'] = $Configuracoes['fl_exibe_botao_enviar'];
		$Configuracoes2['fl_exibe_botao_devolver'] = $Configuracoes['fl_exibe_botao_devolver'];
				
    	$this->setMethod('post');
        $this->setAttrib('id', 'form-laudo');

        $this->setDisableLoadDefaultDecorators(true);
        $this->setDecorators($this->decorators);
        
    	$this->addElement('hidden', 'id_laudo_final', array(
    		'decorators' => $this->hiddendecorator,
    		'order' => 1
    	));
    	
        $this->addElement('hidden', 'id_processo', array(
            'decorators' => $this->hiddendecorator,
    		'order' => 2
        ));
        
        $this->addElement('hidden', 'id_cobertura', array(
            'decorators' => $this->hiddendecorator,
    		'order' => 3
        ));
        
        $this->addElement('hidden', 'id_vistoria', array(
            'decorators' => $this->hiddendecorator,
    		'order' => 4
        ));
        
        $this->addElement('hidden', 'id_proposta', array(
            'decorators' => $this->hiddendecorator,
    		'order' => 5
        ));

        $this->addElement('hidden', 'module_name', array(
            'decorators' => $this->hiddendecorator,
        	'value' => $this->_request['module'],
    		'order' => 60
        ));
        
        $this->addElement('hidden', 'controller_name', array(
            'decorators' => $this->hiddendecorator,
        	'value' => $this->_request['controller'],
    		'order' => 7
        ));
        
        $this->addElement('hidden', 'fl_vistoria_preliminar', array(
            'decorators' => $this->hiddendecorator,
            'order' => 8,
            'value' => $dados['fl_preliminar']
        ));
		
        if(!$dados["fl_preliminar"] || $this->_request['controller'] == 'lf010' || $this->_request['controller'] == 'lf006' || $this->_request['controller'] == 'lf003' || $this->_request['controller'] == 'lf013'){
	        
	        $this->addElement('radio', 'fl_evento_nao_constatado', array(
	            'MultiOptions' => array(0=>'Sim',1=>'Não'),
	            'decorators' => $this->multiElementDecorator,
	            'disableLoadDefaultDecorators' => true,
	            'separator'=>'',
	            'label' => 'Evento constatado?',
	            'required' => true,
	    		'order' => 102,
			    //'ignore' => $this->getReadOnly(),
				//'readonly' => $this->getReadOnly()
	        )); 	        

		
            $this->addElement('radio', 'fl_area_atingida', array(
                'MultiOptions' => array('T'=>'Total','P'=>'Parcial'),
                'decorators' => $this->elementDecorator,
                'disableLoadDefaultDecorators' => true,
                'separator'=>'',
                'label' => 'Área atingida',
                'required' => true,
                'order' => 1000,
                //'ignore' => $this->getReadOnly(),
                //'readonly' => $this->getReadOnly(),
                'validators'=>array(new Agro_Validate_AreaParcial),
            	'class' => 'short'
            ));	        
	        
			$this->addElement('text', 'ds_coordenadas', array(
				'decorators' => $this->elementDecorator,
				'disableLoadDefaultDecorators' => true,
				'label' => 'Coordenadas Geográficas da Sede',
				'class' => 'coordenadas',
				'size' => 26,
	    		'order' => 14,
				'required' => true,
				//'ignore' => $this->getReadOnly(),
				//'readonly' => $this->getReadOnly()
			));        

        }
        
		$this->addElement('file', 'ds_nome_laudo_fisico', array(
			'decorators' => $this->fileDecorator,
			'label' => '',
			'size'	=> 50,
			'class' => 'hide',
			'order' => 103,
			'MaxFileSize' => 4194304
			
		));
        
         if($this->_request['module'] != "safra13"){
        	
			$this->addElement('text', 'dt_vistoria_data', array(
				'decorators' => $this->elementDecorator,
				'label' => 'Data',
				'class' => 'data inline-element fieldset-informacoes-vistoria',
				'required' => true,
				'validators'=>array(new Agro_Validate_DataVistoria)
			));        
	        
			$this->addElement('select', 'dt_vistoria_hora', array(
				'MultiOptions' => array(
			        0=>'00',
			        1=>'01',
			        2=>'02',
			        3=>'03',
			        4=>'04',
			        5=>'05',
			        6=>'06',
			        7=>'07',
			        8=>'08',
			        9=>'09',
			        10=>'10',
			        11=>'11',
			        12=>'12',
			        13=>'13',
			        14=>'14',
			        15=>'15',
			        16=>'16',
			        17=>'17',
			        18=>'18',
			        19=>'19',
			        20=>'20',
			        21=>'21',
			        22=>'22',
			        23=>'23'
			    ),
				'decorators' => $this->elementDecorator,
				'label' => 'Hora',
				'class' => 'inline-element fieldset-informacoes-vistoria',
				'required' => true,
				'ignore' => $this->getReadOnly(),
				'readonly' => $this->getReadOnly()
			));
	
			$this->addElement('select', 'dt_vistoria_minutos', array(
				'MultiOptions' => array(
			        0=>'00',
			        5=>'05',
			        10=>'10',
			        15=>'15',
			        20=>'20',
			        25=>'25',
			        30=>'30',
			        35=>'35',
			        40=>'40',
			        45=>'45',
			        50=>'50',
			        55=>'55'
			    ),
				'decorators' => $this->elementDecorator,
				'label' => ':',
				'class' => 'inline-element fieldset-informacoes-vistoria',
				'ignore' => $this->getReadOnly(),
				'readonly' => $this->getReadOnly()
			));

			$Model_Funcionarios = new Sistema_Model_Funcionarios();
			$this->addElement('multiCheckbox', 'vistoriadores', array(
			    'MultiOptions' => $Model_Funcionarios->getArrayVistoriadoresByEmpresa($dados["id_empresa_vistoriadora"]),
			    'decorators' => $this->elementDecorator,
				'label' => 'Vistoriadores',
				'separator' => '',
				'required' => true,
				'class' => 'vistoriador',
				'ignore' => $this->getReadOnly(),
				'readonly' => $this->getReadOnly()
			));         	
         	
			if(!$dados["fl_preliminar"] || $this->_request['controller'] == 'lf010' || $this->_request['controller'] == 'lf006' || $this->_request['controller'] == 'lf003' || $this->_request['controller'] == 'lf023'){
		        $this->addElement('radio', 'fl_dt_evento_em_cobertura', array(
		            'MultiOptions' => array(1=>'Sim',0=>'Não. Justifique nas observações'),
		            'decorators' => $this->multiElementDecorator,
		            'disableLoadDefaultDecorators' => true,
		            'separator'=>'',
		            'label' => 'A data de ocorrência do evento está dentro do período de cobertura?',
		            'required' => true,
		    		'order' => 102,
				    //'ignore' => $this->getReadOnly(),
					//'readonly' => $this->getReadOnly()
		        )); 

		        if($this->_request['controller'] == 'lf010' || $this->_request['controller'] == 'lf017' || $this->_request['controller'] == 'lf019' || $this->_request['controller'] == 'lf020' || $this->_request['controller'] == 'lf006' || $this->_request['controller'] == 'lf003' || $this->_request['controller'] == 'lf023' || $this->_request['controller'] == 'lf028'){
			        $this->addElement('radio', 'fl_area_declarada_representa_area_cultivada', array(
			            'MultiOptions' => array(1=>'Sim',0=>'Não. Justifique nas observações'),
			            'decorators' => $this->multiElementDecorator,
			            'disableLoadDefaultDecorators' => true,
			            'separator'=>'',
			            'label' => 'As áreas declaradas nas unidades seguradas da proposta representam toda a área cultivada na propriedade?',
			            'required' => true,
			    		'order' => 103,
					    //'ignore' => $this->getReadOnly(),
						//'readonly' => $this->getReadOnly()
			        ));
	

			        if(!$this->_request['controller'] == 'lf010'){
				        $this->addElement('text', 'vl_tamanho_planta', array(
				            'disableLoadDefaultDecorators' => false,
				            'label' => 'Tamanho da planta (média em cm de pelo menos 70% das plantas):',
				            'required' => true,
				    		'order' => 104,
				        	'class' => 'integer'
						    //'ignore' => $this->getReadOnly(),
							//'readonly' => $this->getReadOnly()
				        ));	
			        }
		        }	        
		        
			}
			
			if($this->_request['controller'] == 'lf018' || $this->_request['controller'] == 'lf019' || $this->_request['controller'] == 'lf023'){
		        $this->addElement('text', 'vl_tamanho_planta', array(
		            'disableLoadDefaultDecorators' => false,
		            'label' => 'Tamanho da planta (média em cm de pelo menos 70% das plantas):',
		            'required' => true,
		    		'order' => 104,
		        	'class' => 'integer'
				    //'ignore' => $this->getReadOnly(),
					//'readonly' => $this->getReadOnly()
		        ));
			}
         }		
		
			$this->addElement('textarea', 'ds_observacao_assinatura_segurado', array(
				'decorators' => $this->elementDecorator,
				'label' => '',
				'maxlength' => 3000,
				'rows' => 3,
				'cols' => 80,
				'label' => ' ',
	    		'order' => 3,
			    //'ignore' => $this->getReadOnly(),
				//'readonly' => $this->getReadOnly(),
				'class' => 'hide'
			));		        
	        
			$this->addElement('textarea', 'ds_observacao_assinatura_regulador', array(
				'decorators' => $this->elementDecorator,
				'label' => '',
				'maxlength' => 3000,
				'rows' => 3,
				'cols' => 80,
				'label' => ' ',
	    		'order' => 105,
			    //'ignore' => $this->getReadOnly(),
				//'readonly' => $this->getReadOnly(),
				'class' => 'hide'
			));	         

			
        $this->addElement('hidden', 'input_itens_segurados_parcial', array(
            'decorators' => $this->hiddendecorator,
        	'order'=>1002
        ));

        $this->addDisplayGroup(
	        array('input_itens_segurados_parcial'),
	        'itens_segurados_parcial',
	        array(
	        	'legend' => 'Itens Segurados',
	        	'order'=>1001,
	            'class'=>'hide',
				'disableLoadDefaultDecorators' => true,
				'decorators'=>$this->fieldsetDecorator
	        )
		);
		
		
		if(!$dados["fl_preliminar"] || $this->_request['controller'] == 'lf010' || $this->_request['controller'] == 'lf006' || $this->_request['controller'] == 'lf003' || $this->_request['controller'] == 'lf013'){
	        $this->addDisplayGroup(
	            array( 
	            	'fl_evento_nao_constatado',
	            	'ds_nome_laudo_fisico'
	            ),
	            'informacoes-iniciais',
	            array(
	                'legend' => 'Informação Inicial',
	                'order' => 40,
	                'disableLoadDefaultDecorators' => true,
	                'decorators' => $this->fieldsetDecorator
	            )
	        );		
		}

		
		
        $this->addElement('hidden', 'input_itens_segurados_laudo_final', array(
            'decorators' => $this->buttonDecorator,
        	'order'=>1004
        ));
        
        $this->addDisplayGroup(
        	array('input_itens_segurados_laudo_final'),
        	'itens_segurados_laudo_final',
        	array(
        		'legend' => 'Itens Segurados',
        		'order'=>1003,
        	    'class'=>'hide',
        	    'disableLoadDefaultDecorators' => true,
        	    'decorators'=>$this->fieldsetDecorator
        	)
		);		
        
		//if (!in_array("ds_observacao", ($dados["ignorar"] ? $dados["ignorar"] : array()))) {
			$this->addElement('textarea', 'ds_observacao', array(
				'decorators' => $this->elementDecorator,
				'disableLoadDefaultDecorators' => true,
				'label' => 'Observações',
				'maxlength' => 3000,
				'rows' => 7,
				'cols' => 80,
	    		'order' => 1,
			    //'ignore' => $this->getReadOnly(),
				//'readonly' => $this->getReadOnly()
			));
		//}
		
        $this->addElement('radio', 'fl_assinatura_segurado', array(
            'MultiOptions' => array(1=>'Sim',0=>'Não. Justifique abaixo.'),
            'decorators' => $this->multiElementDecorator,
            'disableLoadDefaultDecorators' => true,
            'separator'=>'',
            'label' => 'Assinatura do segurado coletada?',
            'required' => true,
    		'order' => 2,
		    //'ignore' => $this->getReadOnly(),
			//'readonly' => $this->getReadOnly()
        ));
        
        $this->addElement('radio', 'fl_assinatura_regulador', array(
            'MultiOptions' => array(1=>'Sim',0=>'Não. Justifique abaixo.'),
            'decorators' => $this->multiElementDecorator,
            'disableLoadDefaultDecorators' => true,
            'separator'=>'',
            'label' => 'Assinatura do regulador coletada?',
            'required' => true,
    		'order' => 4,
		    //'ignore' => $this->getReadOnly(),
			//'readonly' => $this->getReadOnly()
        ));   
        
        $this->addDisplayGroup(
			array(
				'ds_observacao', 
				'fl_assinatura_segurado',
				'ds_observacao_assinatura_segurado', 
				'fl_assinatura_regulador',
				'ds_observacao_assinatura_regulador'
			),
			'informacoes_complementares',
            array(
				'legend' => 'Informações Complementares',
                'order'=>1005,
                'disableLoadDefaultDecorators' => true,
                'decorators'=>$this->fieldsetDecorator
            )
        );
        
		if ($this->getReadOnlyAll()) {
	    	$this->addElement('hidden', 'Salvar', array(
	    		'decorators' => $this->hiddendecorator,
	    		'order' => 1008
	    	));			
		} else {
	        $this->addElement('button', 'Salvar', array(
	        	'decorators' => $this->buttonDecorator,
	        	'order' 	=> 1008,
	        	'type' 		=> 'submit',
	            'value'		=> 'Salvar',
	            'ignore'	=> true
	        ));
		}
		
		if ($Configuracoes2['fl_exibe_botao_enviar']) {
	        $this->addElement('button', 'Enviar', array(
	        	'decorators' => $this->buttonDecorator,
	        	'order' 	=> 1009,
	        	'type' 		=> 'button',
	            'value'		=> 'Enviar',
	            'ignore'	=> true
	        ));
	    } else {
	    	$this->addElement('hidden', 'Enviar', array(
	    		'decorators' => $this->hiddendecorator,
	    		'order' => 1009
	    	));
		}
		
		if ($Configuracoes2['fl_exibe_botao_devolver']) {
	        $this->addElement('button', 'Devolver', array(
	        	'decorators' => $this->buttonDecorator,
	        	'order' 	=> 1010,
	        	'type' 		=> 'button',
	            'value'		=> 'Enviar',
	            'ignore'	=> true
	        ));
	    } else {
	    	$this->addElement('hidden', 'Devolver', array(
	    		'decorators' => $this->hiddendecorator,
	    		'order' => 1010
	    	));
		}
				
        $this->addElement('button', 'Cancelar', array(
        	'decorators' => $this->buttonDecorator,
        	'order' 	=> 1011,
        	'type' 		=> 'button',
            'value'		=> 'Cancelar',
            'ignore'	=> true
        ));		
		
        $this->addDisplayGroup(
			array('Salvar', 'Enviar', 'Devolver', 'Cancelar'),
            'buttons',
            array(
            	'order'=>1012,
                'disableLoadDefaultDecorators' => true,
                'decorators'=>$this->buttonsDecorator
			)
        );
        
        try {
        	$this->addDisplayGroup(
            array(
            	'fl_area_atingida'
            ), 
            'area_atingida',
            array(
                'legend'=>'Informações da Área',
                'order'=>506,
                'disableLoadDefaultDecorators' => true,
                'decorators'=>$this->fieldsetDecorator
            )
        );	
        } catch (Exception $e) {

        }
    }
    
    public function setReadOnly($value){
        $this->_readOnly = ($value ? $value : null);
    }
	
    public function getReadOnly(){
        return $this->_readOnly;
    }
    
    public function setReadOnlyAll($value){
        $this->_readOnlyAll = ($value ? $value : null);
    }
	
    public function getReadOnlyAll(){
        return $this->_readOnlyAll;
    }		
	
    public function populate(array $values) {
        parent::populate($values);
    }
 
    public $decorators = array (
        'FormElements',
        'Form',
    );
    
    public $elementDecorator = array (
        'ViewHelper',
        array(
            'Description',
            array(
                'class'=>'help'
            )
        ),
        'Errors',
        'Label'
    );
    
    public $fieldsetDecorator = array(
        'FormElements',
        'Fieldset'
    );
	
	public function getConfiguracoes($idLaudoFinal, $dados = array()) {
		$Model_LaudosFinais = new Sinistro_Model_LaudosFinais();
		$LaudoFinal = $Model_LaudosFinais->getLaudoFinal($idLaudoFinal);
		
		$Configuracoes = array();

		$User = Zend_Auth::getInstance()->getIdentity(); 
		
		$Funcionario = new Sistema_Model_Funcionarios();
		$f = $Funcionario->fetchRow('id_usuario = '.$User->id);
		$idEmpresaUserLogado = $f['id_empresa'];

		$VistoriaEmpresa = new Sistema_Model_VistoriasEmpresas();
		$idEmpresaVistoriadora = $VistoriaEmpresa->getIdEmpresaByLaudoFinal($idLaudoFinal);		
		
		if ($LaudoFinal['ds_chave_status'] == 'ENVIADO' || ($idEmpresaUserLogado != $idEmpresaVistoriadora && $User->tp_usuario != 'F')) {
			$Configuracoes['readonly'] = 'readOnlyAll';

		} elseif ($LaudoFinal['fl_evento_nao_constatado']) {
			
			$Configuracoes['readonly'] = 'readOnly';
			$Configuracoes['fl_exibe_botao_enviar'] = true;
		}

		if ($LaudoFinal['ds_chave_status'] != 'ENVIADO' && $Model_LaudosFinais->isComplete($idLaudoFinal, $dados['fl_preliminar'])) {
			$Configuracoes['fl_exibe_botao_enviar'] = true;
		}
		
		if ($LaudoFinal['ds_chave_status'] == 'ENVIADO' && (Agro_Util::checkPermission('sinistro:laudofinal:devolvelaudofinal'))) {
			$Configuracoes['fl_exibe_botao_devolver'] = true;
			$Configuracoes['readonly'] = 'readOnly';
		}
		
		return $Configuracoes;
	}
	
	public $buttonDecorator = array(
		'ViewHelper',
		array(array('elementLi'=>'HtmlTag'), array('tag' => 'li'))
	);
	
	public $buttonsDecorator = array(
		'FormElements',	
		array(array('elementUl'=>'HtmlTag'), array('tag' => 'ul','class'=>'ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all'))
	);
	
	public $hiddendecorator = array(
		'ViewHelper',
	);
}