<?php

class Zend_View_Helper_MontaUnidades extends Zend_View_Helper_Abstract {
	
	public function montaUnidades($atributos, $respostas=false) {
		$Session = new Zend_Session_Namespace('carrega_proposta');
		$Proposta = new Zend_Session_Namespace('proposta');
		
		if ($Session->premio['propriedades']['PROD_PRODUCAO_GARANTIDA']['ds_valor']) {
			$Model = new Comercializacao_Model_TaxasAgrupamentoMunicipios();
			$produtividadeEstimada = $Model->getProdutividadeEstimada($Session->premio['agrupamento'], $Proposta->segurado['id_municipio_propriedade']);
		}
		
		if (is_array($respostas)):
			foreach ($respostas as $nro => $resposta):
				?>
				<tr id="tr_unidade_<?=$nro?>">
					<td align="center">
						<input type="checkbox" id="ckeck_unidade_<?=$nro?>" class="checkbox_unidade" value="<?=$nro?>" />
					</td>
					<td align="center"><?=$nro?></td>
					<? foreach ($atributos as $atributo): ?>
						<?
						if ($atributo['ds_chave_rn'] == 'IS_CICLO' or $atributo['ds_chave_rn'] == 'IS_FRANQUIA_PARCIAL' or $atributo['ds_chave_rn'] == 'IS_PREMIO_PARCIAL' or $atributo['ds_chave_rn'] == 'IS_FRANQUIA_PARCIAL') {
							continue;
						}
						
						if ($Proposta->segurado['questionario']['QUEST_PRODUTIVIDADE_DECLARADA'] == 2) {
							if ($atributo['ds_chave_rn'] == 'IS_AREA' or $atributo['ds_chave_rn'] == 'IS_PRODUTIVIDADE') {
								continue;
							}
						} else {
							if ($atributo['ds_chave_rn'] == 'IS_PRODUCAO_PLANTA' or $atributo['ds_chave_rn'] == 'IS_NRO_PLANTAS') {
								continue;
							}
						}
						?>
						<td align="center">
							<?
							if ($atributo['ds_chave_rn'] == 'IS_PRODUCAO_GARANTIDA') {
								$resposta[$atributo['ds_chave_rn']] = $produtividadeEstimada;
							}
							?>
							<?=$this->getElement($atributo, $resposta, $nro);?>
						</td>
					<? endforeach; ?>
				</tr>
				<?
			endforeach;
		else:
			?>
			<tr id="tr_unidade_{0}">
				<td align="center">
					<input type="hidden" name="unidade[{0}][ID]" value="" />
					<input type="checkbox" id="ckeck_unidade_{0}" class="checkbox_unidade" value="{0}" />
				</td>
				<td align="center">{0}</td>
				<? foreach ($atributos as $atributo):
					if ($atributo['ds_chave_rn'] == 'IS_CICLO' or $atributo['ds_chave_rn'] == 'IS_FRANQUIA_PARCIAL' or $atributo['ds_chave_rn'] == 'IS_PREMIO_PARCIAL' or $atributo['ds_chave_rn'] == 'IS_FRANQUIA_PARCIAL') {
						continue;
					}
					
					if ($Proposta->segurado['questionario']['QUEST_PRODUTIVIDADE_DECLARADA'] == 2) {
						if ($atributo['ds_chave_rn'] == 'IS_AREA' or $atributo['ds_chave_rn'] == 'IS_PRODUTIVIDADE') {
							continue;
						}
					} else {
						if ($atributo['ds_chave_rn'] == 'IS_PRODUCAO_PLANTA' or $atributo['ds_chave_rn'] == 'IS_NRO_PLANTAS') {
							continue;
						}
					}
					
					$resposta = false;
					
					if ($atributo['ds_chave_rn'] == 'IS_PRODUCAO_GARANTIDA') {
						$resposta[$atributo['ds_chave_rn']] = $produtividadeEstimada;
					}
					?>
					<td align="center"><?=$this->getElement($atributo, $resposta);?></td>
				<? endforeach; ?>
			</tr>
			<?
		endif;
	}
	
	public function getElement($atributo, $resposta=false, $item=false) {
		$Element = new Agro_Element();
		$Element->setType($atributo['ds_chave_tipo_atributo']);
		$Element->setValue($resposta[$atributo['ds_chave_rn']]);
		
		if ($atributo['fl_required']) {
			$Element->setClass($atributo['ds_chave_tipo_atributo'] . ' element_required');
		} else {
			$Element->setClass($atributo['ds_chave_tipo_atributo']);
		}

		if ($atributo['ds_chave_tipo_atributo'] == 'data') {
			$Element->setSize(11);
		}

		if ($item) {
			$Element->setName("unidade[$item][{$atributo['ds_chave_rn']}]");
		} else {
			$Element->setName("unidade[{0}][{$atributo['ds_chave_rn']}]");
		}
		
		switch ($atributo['ds_chave_tipo_atributo']) {
			case 'lookup':
				$Element->setOptions($this->getOptions($atributo));
				break;
				
			default:
				$Element->setOptions($atributo['ds_valores_opcoes']);
				break;
		}
		
		return $Element->get();
	}
	
	public function getOptions($atributo) {
		$Db = Agro_Db_Table_Abstract::getDefaultAdapter();
		
		$select = $Db->select();
		$select->distinct(true);
		$select->from("{$atributo['ds_schema']}.{$atributo['ds_table']}", array($atributo['ds_option_value'], $atributo['ds_option_text']));
		$select->order($atributo['ds_option_text']);
		
		
		if ($atributo['ds_option_filter']) {
			$session = new Zend_Session_Namespace($atributo['ds_option_filter']);
			$select->where("{$atributo['ds_option_filter']} = ?", $session->value);
		}
		
		$Rs = $Db->fetchAll($select);
		
		if ($Rs) {
			$options = array();
			foreach ($Rs as $Row) {
				$options[$Row[$atributo['ds_option_value']]] = $Row[$atributo['ds_option_text']];
			}
		}
		
		return $options;
	}
}