<?php

class Sinistro2_View_Helper_RenderArquivosProcesso extends Zend_View_Helper_Abstract
{
    public function renderArquivosProcesso(array $data, $processo, $revisorArquivos = null, $styleArquivosProcesso = '')
    {
        if (!Agro_Util::checkPermission('sinistro2:abaarquivos:arquivoprocesso')) {
            return '';
        }

        $html = '';

        $html .= '<div ' . $styleArquivosProcesso . '>';
        $html .= '<fieldset>';
        $html .= '<legend><b>Arquivos do Processo</b></legend>';

       // $html .= '/sinistro2/processo/checkfiles';
        $html .= '<input type="hidden" name="id_proposta" value="' . $this->view->escape($processo->id_proposta) . '">';
        $html .= '<input type="hidden" name="id_processo" value="' . $this->view->escape($processo->id) . '">';

        /* ====== BOTÕES E STATUS ====== */
        if (Agro_Util::checkPermission('sinistro2:processo:checkfiles')) {

            if (!empty($data['arquivos'])) {
                $html .= '<button type="button" id="conferirArquivos">Conferidos</button>';
            }

            if ($processo->id_status != 35) {
                $html .= '<button type="button" id="newFile">Cadastrar Arquivo</button>';
            }

            $html .= '<button type="button" id="' . $processo->id_proposta . '" class="showListPendencias">Pendências</button>';
            $html .= '<label for="id_status_arquivos">Status dos arquivos:</label>';
            $html .= '<select name="id_status_arquivos" id="id_status_arquivos">';

            foreach ($data['comboStatusArquivos'] as $status) {
                $selected = ($processo->id_status_arquivos == $status['id_status']) ? ' selected' : '';
                $disabled = ($status['cd_status'] == 1) ? ' disabled' : '';

                $html .= '<option value="' . $status['id_status'] . '"' . $selected . $disabled . '>';
                $html .= $status['cd_status'] . ' - ' . $status['ds_status'];
                $html .= '</option>';
            }

            $html .= '</select>';

            if ($processo->id_status_arquivos == 108 && $revisorArquivos) {
                $html .= "<span id='revisorArquivos'>";
                $html .= " Revisado por <b>{$revisorArquivos['nomeRevisor']}</b>";
                $html .= " em <b>" . Agro_Util::formatDate($revisorArquivos['dataRevisao'], Zend_Date::DATES) . "</b>";
                $html .= '</span>';
            }
        }

        /* ====== TABELA DE ARQUIVOS ====== */
        if (!empty($data['arquivos'])) {
            $html .= '<table width="100%" class="grid" cellpadding="1" cellspacing="1">';
            $html .= '<thead>';
            $html .= '<tr>';
            $html .= '<th width="2%"><input type="checkbox" id="check_all_arquivos" /></th>';
            $html .= '<th>Tipo</th>';
            $html .= '<th>Arquivo</th>';
            $html .= '<th width="20%" nowrap>Data do cadastro</th>';
            $html .= '<th width="15%">Ações</th>';
            $html .= '</tr>';
            $html .= '</thead>';
            $html .= '<tbody>';

            foreach ($data['arquivos'] as $arquivo) {
                $bgColor = ($processo->id == $arquivo['id_processo']) ? '#66ffff' : '#ffffff';

                $filePath = APPLICATION_PATH . '/upload_files/sinistro_processo/' . $arquivo['ds_nome_arquivo'];
                if (!file_exists($filePath)) {
                    continue;
                }

                $checked = $arquivo['fl_conferido'] ? ' checked="checked"' : '';

                $html .= '<tr>';
                $html .= '<td bgcolor="' . $bgColor . '">';
                $html .= '<input data-tipo-arquivo="' . $arquivo['ds_tipo_arquivo'] . '" ';
                $html .= 'class="check_file" type="checkbox" name="ar_arquivoss_proposta[]" ';
                $html .= 'id="arquivo_proposta_' . $arquivo['id'] . '" ';
                $html .= 'value="' . $arquivo['id'] . '"' . $checked . '>';
                $html .= '</td>';

                $html .= '<td bgcolor="' . $bgColor . '">' . $arquivo['ds_tipo_arquivo'] . '</td>';
                $html .= '<td bgcolor="' . $bgColor . '">' . $arquivo['ds_nome_arquivo'] . '</td>';
                $html .= '<td align="center" bgcolor="' . $bgColor . '">';
                $html .= Agro_Util::formatDate($arquivo['dt_criacao'], Zend_Date::DATES);
                $html .= '</td>';

                /* ====== AÇÕES ====== */
                $html .= '<td align="center" nowrap bgcolor="' . $bgColor . '">';


                if (Agro_Util::checkPermission('sinistro2:processo:editfile')) {
                    $html .= '<a href="/sinistro2/processo/editararquivo/id_arquivo/' . $arquivo['id']
                    . '/id_tipo_arquivo/' . $arquivo['id_tipo_arquivo'] . '" '
                    . 'rel="' . $arquivo['id'] . '" title="Editar Arquivo" class="editFile">';
                    $html .= '<img src="/resources/imagens/mini/edit.png" alt="edit.png" title="Editar" border="0">';
                    $html .= '</a> ';
                }
                $html .= '<a href="/agronet/file/processoarquivo/id/' . $arquivo['id'] . '">';
                $html .= '<img src="/resources/imagens/mini/download.png" alt="download.png" title="Download" border="0">';
                $html .= '</a> ';

                if (Agro_Util::checkPermission('sinistro2:processo:deletefile')) {
                    $html .= '<a href="/sinistro2/processo/deletefile/id/' . $arquivo['id']
                    . '/id_processo/' . $processo->id . '" class="delete_arquivo_processo">';
                    $html .= '<img src="/resources/imagens/mini/delete.png" alt="delete.png" title="Excluir" border="0">';
                    $html .= '</a> ';
                }
                if (Agro_Util::checkPermission('sinistro2:processo:listarquivolog')) {
                    $html .= '<a href="/sinistro2/processo/listarquivolog/id/' . $arquivo['id']
                    . '/id_processo/' . $processo->id . '" class="show_log" rel="' . $arquivo['id'] . '">';
                    $html .= '<img src="/resources/imagens/mini/view.png" alt="view.png" title="Histórico" border="0">';
                    $html .= '</a> ';
                }
                $html .= '</td>';
                $html .= '</tr>';
            }

            $html .= '</tbody></table>';
        }

        $html .= '</form>';
        $html .= '</fieldset>';
        $html .= '</div>';

        return $html;
    }
}