<?php

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

    public function init() {

		$dados = $this->getDados();
		$Model_LaudosFinais = new Sinistro2_Model_LaudosFinais();
		$laudoFinal = $Model_LaudosFinais->find($dados['id_laudo_final'])->current();


		if ($dados['readonly']) {
			$this->setReadOnly(true);
		} elseif ($dados['id_laudo_final']) {
			$this->setReadOnly($Model_LaudosFinais->isReadonly($dados['id_laudo_final']));
		}

    	$this->setMethod('post');
        $this->setAttrib('class', 'form-laudo');

        $this->setDisableLoadDefaultDecorators(true);
        $this->setDecorators($this->decorators);

		$Model_MotivosQuadraNaoVistoriada = new Sinistro2_Model_MotivosQuadraNaoVistoriada();

		$item = (new Sinistro2_Model_LaudosFinaisItensSegurados())->find((int)$dados['id_laudo_final'], (int)$dados['id_item_segurado']);
	
		$labelFlNaoVistoriada = !in_array($this->_request['controller'], ['lf003itemsegurado', 'lf034itemsegurado'])
			? 'Item não vistoriado'
			: 'Amostra não coletada';

		$configFlNaoVistoriada = [
			'decorators' => $this->elementNaoVistoriadaDecorator,
			'label' => $labelFlNaoVistoriada,
			'order' => 10,
			'class' => 'ctrl-nao-vistoriada'
		];

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

        $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_proposta', array(
            'decorators' => $this->hiddendecorator,
    		'order' => 3
        ));

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

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

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

        $this->addElement('hidden', 'module_name', array(
            'decorators' => $this->hiddendecorator,
        	'value' => $this->_request['module'],
    		'order' => 7
        ));

        $this->addElement('hidden', 'controller_name', array(
            'decorators' => $this->hiddendecorator,
        	'value' => $this->_request['controller'],
    		'order' => 8
        ));

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

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

		$this->addElement('hidden', 'fl_assinado', array(
    		'decorators' => $this->hiddendecorator,
    	));

		$elementoIdMotivo = 'select';
		$idsMotivos = [];

		if (!in_array($this->_request['controller'], ['lf003itemsegurado', 'lf034itemsegurado'])) {
			$idsMotivos = [12, 11, 6, 8, 9, 5, 10, 4, 2, 1, 3, 7];
		}
		$configIdMotivo = [
			'MultiOptions' => $Model_MotivosQuadraNaoVistoriada->getOptions($idsMotivos),
			'decorators' => $this->elementDecorator,
			'label' => 'Motivo:',
			'class' => 'ctrl-nao-vistoriada'
		];
		
		if ($item['id_motivo_quadra_nao_vistoriada'] == Sinistro2_Model_MotivosQuadraNaoVistoriadaTipos::VISTORIA_NAO_CONCLUIDA) {
			$elementoIdMotivo = 'hidden';
			$configIdMotivo = [
				'decorators' => $this->elementDecorator,
				'value' => $item['id_motivo_quadra_nao_vistoriada']
			];

			$this->addElement('text', 'ds_motivo_quadra_nao_vistoriada', [
				'decorators' => $this->elementDecorator,
				'label' => 'Motivo:',
				'value' => 'Vistoria não concluída',
				'ignore' => true,
				'readonly' => true
			]);

			$configFlNaoVistoriada = [
				'decorators' => $this->elementNaoVistoriadaDecorator,
				'label'      => $labelFlNaoVistoriada,
				'order' => 10,
				'ignore' => true,
				'readonly' => true
			];
		}

		$this->addElement($elementoIdMotivo, 'id_motivo_quadra_nao_vistoriada', $configIdMotivo);

		$this->addElement('checkbox', 'fl_nao_vistoriada', $configFlNaoVistoriada);

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

		$this->addElement('text', 'dt_vistoria_data', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Data',
			'class' => 'data inline-element ctrl-nao-vistoriada',
			'required' => true,
			'ignore' => $this->getReadOnly(),
			'readonly' => $this->getReadOnly(),
			'validators'=>array(new Agro_Validate_DataVistoria)
		));


		if ($laudoFinal['fl_transmitido_tablet'] && in_array($this->_request['controller'], ['lf003itemsegurado', 'lf034itemsegurado'])) {
			$this->addElement('text', 'dt_vistoria_hora', array(
				'decorators' => $this->elementDecorator,
				'label' => 'Hora',
				'class' => 'inline-element ctrl-nao-vistoriada',
				'required' => true,
				'ignore' => $this->getReadOnly(),
				'readonly' => $this->getReadOnly(),
				'style' => 'max-width: 15px;',
			));

			$this->addElement('text', 'dt_vistoria_minutos', array(
				'decorators' => $this->elementDecorator,
				'label' => ':',
				'class' => 'inline-element ctrl-nao-vistoriada',
				'ignore' => $this->getReadOnly(),
				'readonly' => $this->getReadOnly(),
				'style' => 'max-width: 15px;',
			));
		} else {

			$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 ctrl-nao-vistoriada',
				'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 ctrl-nao-vistoriada',
				'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']),
		    'decorators' => $this->fieldDecorator,
			'label' => 'Auxiliares',
			'separator' => '',
			'required' => false,
			'ignore' => $this->getReadOnly(),
			'readonly' => $this->getReadOnly()
		));

		$this->addElement('text', 'ds_coordenadas_geograficas', array(
			'decorators' => $this->elementLabellessDecorator,
			'class' => 'coordenadas',
			'label' => 'Coordenadas Geográficas',
			'size' => 26,
			'required' => true,
			'ignore' => $this->getReadOnly(),
			'readonly' => $this->getReadOnly()
		));

		$this->addElement('text', 'vl_area_sinistrada', array(
			'decorators' => $this->elementDecorator,
			'class' => 'decimal',
			//'required' => true,
			'ignore' => $this->getReadOnly(),
			'readonly' => $this->getReadOnly(),
			'validators'=>array(new Agro_Validate_AreaSinistrada)
		));

		$this->addElement('text', 'vl_percent_area_afetada', array(
			'decorators' => $this->elementDecorator,
			'class' => 'decimal',
			//'required' => true,
			'ignore' => $this->getReadOnly(),
			'readonly' => $this->getReadOnly()
		));

		if ($dados['fl_area_sinistrada_readonly']) {
		    $Model_ItensSegurados = new Seguro_Model_ItensSegurados();
		    $vl_area = $Model_ItensSegurados->getArea($this->_request['id_item_segurado']);

			$this->vl_area_sinistrada->setAttrib('readonly', true);
			$this->vl_area_sinistrada->setValue(($vl_area ? $vl_area : 0));
		}

		switch ($this->_request['controller']){
			case 'lf007itemsegurado':
			case 'lf009itemsegurado':
				$this->addElement('textarea', 'ds_aspectos_gerais_cultura', array(
					'decorators' => $this->elementDecorator,
					'label' => 'Aspectos Gerais da Cultura (Nutrição, Sanidade e outros)',
					'maxlength' => 3000,
					'rows' => 7,
					'cols' => 80,
		    		'order' => 1093,
				    'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));

				$this->addElement('text', 'vl_percent_colhido', array(
					'decorators' => $this->elementLabellessDecorator,
					'class' => 'decimal',
					'label' => 'Percentual Colhido',
					'required' => true,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));

			break;
			case 'lf001itemsegurado':
			case 'lf014itemsegurado':
				$this->addElement('select', 'tp_plantio', array(
					'MultiOptions' => array(0=>'',1=>'Transplante',2=>'Semeadura Direta'),
					'decorators' => $this->elementDecorator,
					'class' => 'inline-element ctrl-nao-vistoriada',
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly(),
					'required' => true
				));

			break;

			case 'lf018itemsegurado':
		        //apenas para milho
		        $Estadio = new Sinistro2_Model_EstadiosFenologicos();
				$this->addElement('select', 'id_estadio_fenologico', array(
					'MultiOptions' => $Estadio->fetchPairs('id', 'ds_nome_estadio_fenologico', false, false, 1, 'id_grupo_estadio_fenologico = 9', true, 'ASC'),
					'decorators' => $this->elementDecorator,
					//'label' => 'Estádio Fenológico',
					'required' => false
				));

				$this->addElement('text', 'vl_percent_colhido', array(
					'decorators' => $this->elementLabellessDecorator,
					'class' => 'decimal',
					'label' => 'Percentual Colhido',
					'required' => true,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));

				break;
			case 'lf019itemseguradoch':
				$this->addElement('text', 'vl_percent_colhido_chocho', array(
					'decorators' => $this->elementLabellessDecorator,
					'class' => 'decimal',
					'label' => 'Percentual Colhido Chocho',
					'required' => true,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));
				break;

			case 'lf023itemsegurado':
			case 'lf020itemsegurado':
				break;


			case 'lf003itemsegurado':
			case 'lf034itemsegurado':
				$this->addElement('select', 'fl_zoneamento_valido', array(
					'MultiOptions' => ['' => 'Selecione...', 1 => 'Sim', 0 => 'Não'],
					'decorators' => $this->elementLabellessDecorator,
					'label' => 'Zoneamento válido?',
					'required' => true,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));

				$this->addElement('text', 'vl_area_vistoriada', array(
					'decorators' => $this->elementLabellessDecorator,
					'class' => 'decimal',
					'label' => '',
					'required' => true,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));

				$this->addElement('text', 'vl_percent_colhido', array(
					'decorators' => $this->elementLabellessDecorator,
					'class' => 'decimal',
					'label' => 'Área Vistoriada (ha)',
					'required' => true,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));

				$this->addElement('text', 'vl_area_declarada', array(
					'decorators' => $this->elementLabellessDecorator,
					'class' => 'decimal',
					'label' => 'Área Declarada Atualizada (ha)',
					'required' => false,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));
				
				break;

			default:
				$this->addElement('text', 'vl_percent_colhido', array(
					'decorators' => $this->elementLabellessDecorator,
					'class' => 'decimal',
					'label' => 'Percentual Colhido',
					'required' => true,
					'ignore' => $this->getReadOnly(),
					'readonly' => $this->getReadOnly()
				));
				break;

		}

		$this->addElement('textarea', 'ds_observacao', array(
			'decorators' => $this->elementDecorator,
			'label' => 'Observações',
			'maxlength' => 3000,
			'rows' => 7,
			'cols' => 80,
    		'order' => 1004,
		    //'ignore' => $this->getReadOnly(),
			//'readonly' => $this->getReadOnly()
		));

		$this->addElement('textarea', 'ds_observacao_assinatura_regulador', array(
			'decorators' => $this->elementDecorator,
			'label' => '',
			'maxlength' => 3000,
			'rows' => 3,
			'cols' => 80,
    		'order' => 1093,
		    //'ignore' => $this->getReadOnly(),
			//'readonly' => $this->getReadOnly(),
			'class' => 'hide'
		));

		$this->addElement('textarea', 'ds_observacao_assinatura_segurado', array(
			'decorators' => $this->elementDecorator,
			'label' => '',
			'maxlength' => 3000,
			'rows' => 3,
			'cols' => 80,
    		'order' => 1093,
		    //'ignore' => $this->getReadOnly(),
			//'readonly' => $this->getReadOnly(),
			'class' => 'hide'
		));

		if ($this->_request['controller'] != 'lf034itemsegurado' && $this->_request['controller'] != 'lf003itemsegurado') {
			$this->addElement('radio', 'fl_assinatura_segurado', array(
				'MultiOptions' => array(1=>'Sim',0=>'Não. Justifique abaixo.'),
				'decorators' => $this->multiElementDecorator,
				'label' => 'Assinatura do segurado coletada?',
				'required' => true,
				'order' => 1005,
				'separator' => '',
				'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,
				'label' => 'Assinatura do regulador coletada?',
				'required' => true,
				'order' => 1006,
				'separator' => '',
				'ignore' => $this->getReadOnly(),
				'readonly' => $this->getReadOnly()
			));
		}
		if ($this->getReadOnly() || $laudoFinal['fl_transmitido_tablet'] == true) {
	    	$this->addElement('hidden', 'Salvar', array(
	    		'decorators' => $this->hiddendecorator,
	    		'order' => 1008
	    	));

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

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

			if ($laudoFinal['fl_transmitido_tablet'] == true) {

				$this->addElement('button', 'historico_alteracoes', array(
					'decorators' => $this->buttonDecorator,
					'order' => 1009,
					'type' => 'button',
					'value' => 'Histórico Alterações',
					'ignore' => true,
					'label' => 'Histórico Alterações'
				));
			}
		} else {
	        $this->addElement('button', 'Salvar', array(
	        	'decorators' => $this->buttonDecorator,
	        	'order' => 1008,
	        	'type' => 'submit',
	            'value' => 'Salvar',
	            'ignore' => true
	        ));

			$this->addElement('button', 'salvar_nao_vistoriada', array(
	        	'decorators' => $this->buttonDecorator,
	        	'order' => 1010,
	        	'type' => 'button',
	            'value' => 'Salvar',
	            'ignore' => true,
	            'label' => 'Salvar',
				'style' => 'display:none;'
	        ));

			$this->addElement('button', 'voltar_nao_vistoriada', array(
	        	'decorators' => $this->buttonDecorator,
	        	'order' => 1011,
	        	'type' => 'button',
	            'value' => 'Voltar',
	            'ignore' => true,
	            'label' => 'Voltar',
	        ));

			$this->addElement('hidden', 'historico_alteracoes', array(
	    		'decorators' => $this->hiddendecorator,
	    		'order' => 1009
	    	));
		}
		/*
        $this->addElement('button', 'Cancelar', array(
        	'decorators' => $this->buttonDecorator,
        	'order' 	=> 1011,
        	'type' 		=> 'button',
            'value'		=> 'Cancelar',
            'ignore'	=> true
        ));

        $this->addElement('button', 'Voltar', array(
        	'decorators' => $this->buttonDecorator,
        	'order' 	=> 1012,
        	'type' 		=> 'button',
            'value'		=> 'Voltar',
            'ignore'	=> true
        ));
        */

        $this->addDisplayGroup(
			array('Salvar', 'historico_alteracoes', 'salvar_nao_vistoriada', 'voltar_nao_vistoriada'),
			'buttons',
			array(
				'order'=>1014,
				'disableLoadDefaultDecorators' => true,
				'decorators'=>$this->buttonsDecorator
			)
        );

        $this->addElement('radio', 'fl_indenizacao_fitossanitaria', array(
        		'MultiOptions' => array(1=>' Sim',0=>' Não'),
        		'decorators' => $this->multiElementDecorator,
        		'separator'=>'',
        		'required' => false,
        		'ignore' => $this->getReadOnly(),
        		'readonly' => $this->getReadOnly()
        ));

    }

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

    public function getReadOnly(){
        return $this->_readOnly;
    }

    public function populate(array $values) {
        parent::populate($values);
    }

    public $decorators = array (
        'FormElements',
        'Form',
    );

    public $elementDecorator = array (
        'ViewHelper',
        array(
            'Description',
            array(
                'class'=>'help'
            )
        ),
        'Label'
	);
	
	public $fieldDecorator = array(
		'ViewHelper',
		array(
			'Description',
			array(
				'class'=>'help'
			)
		),
		array(
			'Label',
			array(
				'tag'=>'div'
			)
		),
	);

    public $elementLabellessDecorator = array (
        'ViewHelper',
        array(
            'Description',
            array(
                'class'=>'help'
            )
        ),
    );

    public $fieldsetDecorator = array(
        'FormElements',
        'Fieldset'
    );

	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', 'id'=>'botoes-laudo'))
	);

	public $hiddendecorator = array(
		'ViewHelper',
	);

    public $multiElementDecorator = array (
		'ViewHelper',
		array(
			'Description',
			array(
				'tag'=>'div',
				'class'=>'help'
			)
		),
		array('Label'),
		array(
			'HtmlTag',
			array(
				'tag'=>'div',
				'class'=>'field'
			)
		)
    );

    public $elementNaoVistoriadaDecorator = array (
        'ViewHelper',
        array('Label', array('placement' => 'append'))
    );
}
