<?php

class Sinistro2_Form_LaudoFinal extends Agro_Form {
    private $_readOnly = null;
	private $_readOnlyAll = null;
	private $_disabled = null;
	private $_disabledAll = null;
    public function init($idLaudoFinal = null) {

		$dados = $this->getDados();
		
		
		if($idLaudoFinal) {

			$Configuracoes = $this->getConfiguracoes($idLaudoFinal, $dados);

			switch ($Configuracoes['readonly']) {
				case 'readOnly':
					$this->setReadOnly(true);
					$this->setReadOnlyAll(true);
					$this->setDisabled(true);
					$this->setDisabledAll(true);
					break;
				case 'readOnlyAll':
					$this->setReadOnly(true);
					$this->setReadOnlyAll(true);
					$this->setDisabled(true);
					$this->setDisabledAll(true);
					break;
				default:
				break;
			}
		}

		$Configuracoes2['fl_exibe_botao_enviar'] = $Configuracoes['fl_exibe_botao_enviar'];
		$Configuracoes2['fl_exibe_botao_salvar'] = $Configuracoes['fl_exibe_botao_salvar'];
		$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']
        ));

        $listagemLaudosCoordenadas = ['lf010', 'lf006', 'lf003', 'lf013', 'lf034', 'lf042'];
        if(!$dados["fl_preliminar"] || in_array($this->_request['controller'], $listagemLaudosCoordenadas)){

	        $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(),
        		'disabled' => $this->getDisabled()
	        ));

			$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(),
				'disabled' => $this->getDisabled()
			));

        }
        if(REGULACAO_ATIVA == 'N') {

        	$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(),
        			'disabled' => $this->getDisabled(),
        			'validators'=>array(new Agro_Validate_AreaParcial),
        			'class' => 'short'
        	));
        }

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

			$this->addElement('multiCheckbox', 'auxiliares', array(
			'MultiOptions' => $Model_Funcionarios->getArrayAuxiliaresByEmpresa($dados["id_empresa_vistoriadora"]),
				'label' => 'Auxiliares',
				'decorators' => $this->fieldDecorator,
				'separator' => '',
				'ignore' => $this->getReadOnly(),
				'readonly' => $this->getReadOnly()
			));

			$listagemLaudosDataOcorrencia = ['lf010', 'lf006', 'lf003', 'lf023', 'lf034', 'lf042'];
			if(!$dados["fl_preliminar"] || in_array($this->_request['controller'], $listagemLaudosDataOcorrencia)) {
		        
				if(!in_array($this->_request['controller'], ['lf003', 'lf034'])){
					$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(),
						'disabled' => $this->getDisabled()
					));
				}	

				$useAreaCultivada = array('lf010','lf017','lf019','lf020','lf006','lf003','lf023','lf028','lf034','lf037', 'lf042');
		        if(in_array($this->_request['controller'],$useAreaCultivada)){

					$label = "As Áreas declaradas nas unidades seguradas da proposta representam toda a Área cultivada na propriedade?";
					$options = [1 => 'Sim', 0 => 'Não. Justifique nas observações'];

					if (in_array($this->_request['controller'], ['lf003', 'lf034'])) {
						$label = "As áreas seguradas representam toda a área desta cultura na propriedade?";
						$options = [1 => 'Sim', 0 => 'Não. Informe o tamanho da área não declarada na proposta'];
					}

			        $this->addElement('radio', 'fl_area_declarada_representa_area_cultivada', array(
			            'MultiOptions' => $options,
			            'decorators' => $this->multiElementDecorator,
			            'disableLoadDefaultDecorators' => true,
			            'separator'=>'',
			            'label' => $label,
			            'required' => true,
			    		'order' => 103,
					    //'ignore' => $this->getReadOnly(),
						'readonly' => $this->getReadOnly(),
			        	'disabled' => $this->getDisabled()
			        ));

			        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(),
				        	'disabled' => $this->getDisabled()
				        ));
			        }
		        }
			}

			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(),
		        	'disabled' => $this->getDisabled()
		        ));
			}
        }

		$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(),
        		'disabled' => $this->getDisabled(),
				'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(),
	        	'disabled' => $this->getDisabled(),
				'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->_request['controller'] == 'lf034'){
	        $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(),
	        	'disabled' => $this->getDisabled()
			));
		//}

        $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' => !in_array($this->_request['controller'], ['lf003', 'lf034']),
    		'order' => 2,
		    //'ignore' => $this->getReadOnly(),
			'readonly' => $this->getReadOnly(),
        	'disabled' => $this->getDisabled()
        ));

        $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' => !in_array($this->_request['controller'], ['lf003', 'lf034']),
    		'order' => 4,
		    //'ignore' => $this->getReadOnly(),
			'readonly' => $this->getReadOnly(),
        	'disabled' => $this->getDisabled()
        ));

        $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() ||
			$Configuracoes2['fl_exibe_botao_salvar'] === false
		) {
	    	$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'		=> 'Devolver',
	            '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'),
            '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) {

        }
        */

		if (in_array($this->_request['controller'], ['lf003', 'lf034'])) {
			
			$this->addElement('hidden', 'ds_tipo_responsavel_aux', array(
				'decorators' => $this->hiddendecorator,
				'order' => 10
			));

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

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

			$vistoriadorSecundario = (new Sistema_Model_AgendamentosFuncionarios())
				->getVistoriadorByIdVistoria((int) $this->_request['id_vistoria'], false);

			$assinaturaSegundoPerito = '';
			if (!empty($vistoriadorSecundario)) {
				$assinaturaSegundoPerito = "{$vistoriadorSecundario['ds_nome_fantasia']} - {$vistoriadorSecundario['ds_nome_funcionario']}";
			}
			
			$this->addElement('text', 'ds_vistoriador_secundario', array(
				'decorators' => $this->elementDecorator,
				'label' => 'Assinatura Segundo Perito',
				'value' => $assinaturaSegundoPerito,
				'ignore' => true,
				'readonly' => true,
				'class' => 'readonly',
				'style' => 'min-width: 995px;',
				'disabled' => 'disabled'
			));

			$isDigital = !empty($idLaudoFinal) && (new Sinistro2_Model_LaudosFinais())->isMetodoDigital($idLaudoFinal);
			
			if ($isDigital) {
				
				$comboAgronomos = (new Sinistro2_Model_LaudosFinaisAgronomosAgro())->getComboByLaudoFinal($idLaudoFinal);
				$agronomos = !empty($comboAgronomos) ? implode(" | ", $comboAgronomos) : "";
				$this->addElement('text', 'id_agronomos', array(
					'decorators' => $this->elementDecorator,
					'label' => 'Assinatura(s) do(s) Agrônomo(s) AgroBrasil',
					'value' => $agronomos,
					'ignore' => true,
					'readonly' => true,
					'class' => 'readonly',
					'style' => 'min-width: 995px;',
					'disabled' => 'disabled'
				));
			} else {
				$modelFuncionarios = new Sistema_Model_Funcionarios();
				$this->addElement('select', 'id_agronomos', array(
					'decorators' => $this->elementDecorator,
					'MultiOptions' => $modelFuncionarios->getComboFuncionariosAgronomos(),
					'label' => 'Assinatura do Agrônomo AgroBrasil',
					'class' => 'multiple',
					'multiple' => true,
					'style' => 'min-width: 503px;',
					'RegisterInArrayValidator' => false
				));
			}

			$this->addElement('hidden', 'fl_bloquear_form', array(
				'decorators' => $this->elementDecorator,
				'value' => $isDigital,
			));

			$this->addElement('hidden', 'id_tipo_laudo_final', array(
				'decorators' => $this->hiddendecorator,
				'order' => 13,
				'value' => $this->_request['controller'] == 'lf003' ? Agro_APIs_VSD_Vistoria::ID_LF003 : Agro_APIs_VSD_Vistoria::ID_LF034
			));
		}
    }

    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 setDisabled($value){
    	$this->_disabled = ($value ? $value : null);
    }

    public function getDisabled(){
    	return $this->_disabled;
    }

    public function setDisabledAll($value){
    	$this->_disabledAll = ($value ? $value : null);
    }

    public function getDisabledAll(){
    	return $this->_disabledAll;
    }

	public function validaDadosAssinatura(array $values): string
	{
		$mensagemErro = '';
		
		if (
			in_array($values['controller'], ['lf003', 'lf034'])
			&& $values['fl_evento_nao_constatado'] == 0
		) {
			if (in_array("", $values['fl_assinado'])) {
				return "Todos os Itens Segurados devem ser informados se foram ou não assinados.";
			}

			if (empty($values['ds_tipo_responsavel'])) {
				$mensagemErro = 'Campo Assinatura em Informações Complementares não foi preenchido.';
			}

			if ($values['ds_tipo_responsavel'] === 'autorizado') {
				if (empty($values['pessoa_autorizada'])) {
					$mensagemErro = 'Campo Autorizado não foi preenchido.';
				}
			} elseif(in_array($values['ds_tipo_responsavel'], ['outro', 'outro_pendente_autorizacao'])) {
				$camposNaoPreenchidos = '';

				if (empty($values['ds_responsavel'])) {
					$camposNaoPreenchidos = 'Acompanhante';
				}

				if (empty($values['cpf_responsavel'])) {
					$separator = empty($camposNaoPreenchidos) ? '' : ', ';
					$camposNaoPreenchidos .= $separator . 'CPF';
				}

				if (!empty($camposNaoPreenchidos)) {
					$mensagemErro ="Os seguintes campos da assinatura não foram preenchidos: {$camposNaoPreenchidos}.";
				}
			}
		}

		return $mensagemErro;
	}

    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 $fieldDecorator = array(
		'ViewHelper',
		array(
			'Description',
			array(
				'class'=>'help'
			)
		),
		'Errors',
		array(
			'Label',
			array(
				'tag'=>'div',
				'style'=>'width:100%'
			)
		),
		array(
			array(
				'field-div'=>'HtmlTag'
			),
			array(
				'tag'=>'div',
				'class'=>'row'
			)
		),
		array(
			array(
				'br'=>'HtmlTag'
			),
			array(
				'tag'=>'br'
			)
		),
		array(
			array(
				'div'=>'HtmlTag'
			),
			array(
				'tag'=>'div',
				'class'=>'clear'
			)
		),
		array(
			array(
				'br'=>'HtmlTag'
			),
			array(
				'tag'=>'br'
			)
		)
	);

	public function getConfiguracoes($idLaudoFinal, $dados = array()) {
		$Model_LaudosFinais = new Sinistro2_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'] && $LaudoFinal['ds_chave_status'] == 'ENVIADO') {
			$Configuracoes['readonly'] = 'readOnly';
		}

		if ($LaudoFinal['ds_chave_status'] != 'ENVIADO' && $Model_LaudosFinais->isComplete($idLaudoFinal, $dados['fl_preliminar'])) {
			$Configuracoes['fl_exibe_botao_enviar'] = true;
		}
		
		if (
			in_array($LaudoFinal['ds_chave_status'] , ['ENVIADO_NAO_ASSINADO', 'ENVIADO'])
			&& (Agro_Util::checkPermission('sinistro2:laudofinal:devolvelaudofinal'))
		) {
			$Configuracoes['fl_exibe_botao_devolver'] = true;
			$Configuracoes['readonly'] = 'readOnly';
		}
		
		$Configuracoes['fl_exibe_botao_salvar'] = true;

		if ($LaudoFinal['fl_transmitido_tablet'] == true) {
			$Configuracoes['fl_exibe_botao_salvar'] = false;
			$Configuracoes['fl_exibe_botao_devolver'] = false;
			$Configuracoes['fl_exibe_botao_enviar'] = false;
		}

		if ($LaudoFinal['ds_chave_status'] != 'PENDENTE_ENVIO'){
			$Configuracoes['fl_exibe_botao_enviar'] = false;
		}

		
		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',
	);
}
