<?php
class Agro_View_Helper_PlanilhaTotalizacaoPerdas extends Zend_View_Helper_Abstract {
    public function PlanilhaTotalizacaoPerdas($Configuracoes, $Amostras = array(), $vlPercentPerdas = 0, $flDisable = false) {
?>
    <script type="text/javascript">
        function enableTotalizacaoPerdas() {
            $(".totalizacao-perdas").removeAttr("disabled");
        }
        
        function disableTotalizacaoPerdas() {
            $(".totalizacao-perdas").attr("disabled", "disabled");
        }

    </script>
    <table width="100%" class="grid">
        <thead>
            <tr>
                <th align="center">Amostra</th>
                <?foreach ($Configuracoes['colunas'] as $Coluna):?>
                <th align="center"><?=$Coluna['ds_nome_exibicao']?></th>
                <?endforeach?>
            </tr>
        </thead>
        <tbody>
        <?for ($x = 1; $x <= $Configuracoes['nr_amostras']; $x++):?>
            <tr>
                <th><?=$x?></th>
                <?
                foreach ($Configuracoes['colunas'] as $Coluna):
                    if(array_key_exists($x, $Amostras)) {
                        $value = $Amostras[$x][$Coluna['ds_nome_campo']];
                    } else {
                        $value = null;
                    }
                ?>
                <td align="center">
                    <?=$this->getInput($Coluna, $x, $value)?>
                </td>
                <?endforeach?>
            </tr>
        <?endfor?>
            <tr>
                <th colspan="<?=count($Configuracoes['colunas'])?>" align="right">Perda Total (%)</th>
                <th><?=$vlPercentPerdas?></th>
            </tr>        
        </tbody>
    </table>
<?
    }
    
    public function getInput($Coluna, $nrAmostra, $value = null) {
        $input = '';
        if ($Coluna['value']) {
            $value = $Coluna['value'];
        }
        switch ($Coluna['ds_tipo']) {
            case 'select':
                $input .= '<select name="'.$Coluna['ds_nome_campo'].'['.$nrAmostra.']">';
				$input .= '<option value=""></option>';
                foreach ($Coluna['ar_opcoes'] as $dsValue=>$dsCaption) {
                    $selected = (($value == $dsValue) ? ' selected' : '');
                    $input .= '<option value="'.$dsValue.'"'.$selected.'>'.$dsCaption.'</option>';
                }
				$input .= '</select>';
				break;
			case 'result':
				$input .= $value;
				break;
			case 'label-hidden':
				$input .= '<label for="'.$Coluna['ds_nome_campo'].'_'.$nrAmostra.'">'.$value.'</label>';
				$input .= '<input type="hidden" name="'.$Coluna['ds_nome_campo'].'['.$nrAmostra.']" id="'.$Coluna['ds_nome_campo'].'_'.$nrAmostra.'" value="'.$value.'" />';
				break;
            default:
                $input .= '<input type="text" name="'.$Coluna['ds_nome_campo'].'['.$nrAmostra.']" value="'.$value.'" class="'.$Coluna['ds_class'].'" size="5" maxlenght="6" '.$Coluna['readonly'].' />';
        }
        return $input;
    }
}