<?php

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

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

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

			$this->setReadOnly($Model_LaudosFinais->isReadonly($dados['id_laudo_final']));
		}

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

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

    	
        $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('checkbox', 'fl_nao_vistoriada', array(
    		'decorators' => $this->elementNaoVistoriadaDecorator,
    		'label'      => 'Item não vistoriado',
    		'order' => 10,
    		'class' => 'ctrl-nao-vistoriada'
    	));

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

        $Model_MotivosQuadraNaoVistoriada = new Sinistro_Model_MotivosQuadraNaoVistoriada();
        $this->addElement('select', 'id_motivo_quadra_nao_vistoriada', array(
            'MultiOptions' => $Model_MotivosQuadraNaoVistoriada->getOptions(),
            'decorators' => $this->elementDecorator,
            'label' => 'Motivo:',
            'class' => 'ctrl-nao-vistoriada'
        ));

		$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('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 Sinistro_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;				
				
			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'
		));		
		
        $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()) {
	    	$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
	        ));
		}
        $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', 'Cancelar','Voltar'),
                                'buttons',
                                array(
                                    'order'=>1014,
                                    'disableLoadDefaultDecorators' => true,
                                    'decorators'=>$this->buttonsDecorator
                                )
        );
                
    }

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