<?php

// Página
$direct = 'tela';

$ac = Acesso::check($_GET['i']);

// Checa se o usuário possui acesso na página
Util::verificaAcesso('cms-' . $direct, 'consultar', 'index.php');

$param = array();
$where = array();

if (isset($_POST['buscar'])) {
    $param[] = Util::montaParametros(':buscar', '%' . $_POST['buscar'] . '%', 'STR');
    $where[] = '(nome LIKE :buscar)';
}

$where = implode(' AND ', $where);

$qtdRegistro = Tela::contador($where, $param);

// Paginacao
$objPag = new Paginacao(20, $qtdRegistro, 'cms-' . $direct);
$pagina = (isset($_GET['pagina'])) ? $_GET['pagina'] : 1;
$objPag->paginar($pagina);

// Mensagem de Retorno
require_once $GLOBALS['baseDir'] . 'includes/mensagens.php';

// Modal excluir registro
require_once $GLOBALS['baseDir'] . 'includes/modal/excluir-registro.php';
?>

<?php if ($ac->inserir()) : ?>
    <a href="<?= $GLOBALS['baseUrl']; ?>?i=cms-<?= $direct; ?>-inserir" class="adicionar">Adicionar</a>
<?php endif; ?>

<p style="font-size: 14px;">Total de registros: <?= number_format($qtdRegistro, 0, ',', '.'); ?></p>

<div class="tabelas ordenar" data-modulo="<?php echo $direct; ?>">
    <?php if ($ac->excluir()) : ?>
        <input type="button" value="Deletar" class="deletar mobile-hidden" />
    <?php endif; ?>

    <table id="grid" border="0" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <?php if ($ac->editar()) : ?>
                    <th scope="col" width="10px" class="primeiro"><input name="todos" type="checkbox" onClick="toggle(this)"></th>
                <?php endif; ?>

                <th scope="col">Nome</th>

                <?php if ($ac->editar()) : ?>
                    <th scope="col" width="10%">Status</th>
                <?php endif; ?>

                <?php if ($ac->editar()) : ?>
                    <th scope="col" width="10%">Editar</th>
                <?php endif; ?>

                <?php if ($ac->excluir()) : ?>
                    <th scope="col" width="10%" class="ultimo">Deletar</th>
                <?php endif; ?>
            </tr>
        </thead>
        <tbody id="list">
            <?php
                $editar = ($ac->editar()) ? $GLOBALS['baseUrl'] . '?i=cms-' . $direct . '-editar&id=' : false;
                $deletar = ($ac->excluir()) ? $GLOBALS['baseUrl'] . '?i=cms-' . $direct . '-processo&t=excluir&id=' : false;

                $objList = Tela::listar($where, $param, 'ordem ASC, id DESC', $objPag->getQtdPerPage(), $objPag->getOffset());
                if ($objList) :
                    foreach ($objList as $obj) :
                        $ativo = (1 == $obj->exibirMenu) ? 'pronto' : 'impedido';
                        $imagem = "javascript:alteraStatusModulo('" . $direct . "'," . $obj->id . ');';
            ?>
                <tr class="registro-<?= $obj->id; ?>">
                    <?php if ($ac->editar()) : ?>
                        <td class="mobile-hidden"><input name="marcar[]" type="checkbox" value="<?= $obj->id; ?>"></td>
                    <?php endif; ?>

                    <!-- -->

                    <?php if ($ac->editar()) : ?>
                        <td data-label="Nome"><a href="<?= $editar . $obj->id; ?>"><?= $obj->nome; ?></a></td>
                    <?php else : ?>
                        <td data-label="Nome"><?= $obj->nome; ?></td>
                    <?php endif; ?>

                    <!-- -->

                    <?php if ($ac->editar()) : ?>
                        <td data-label="Status"><a href="<?= $imagem; ?>" id="status<?= $obj->id; ?>" class="status <?= $ativo; ?>" title=""></a></td>
                    <?php endif; ?>

                    <?php if ($ac->editar()) : ?>
                        <td class="mobile-hidden"><a href="<?= $editar . $obj->id; ?>" class="status editar" title="Editar"></a></td>
                    <?php endif; ?>

                    <?php if ($ac->excluir()) : ?>
                        <td data-label="Excluir"><a href="javascript:;" data-modulo="<?= $direct; ?>" data-id="<?= $obj->id; ?>" class="status excluir" title="Excluir"></a></td>
                    <?php endif; ?>
                </tr>
            <?php
                    endforeach;
                else :
                    echo '<tr><td colspan="6">Nenhum registro encontrado</td></tr>';
                endif;
            ?>
        </tbody>
    </table>
</div>
<?= $objPag->links(); ?>
