<?php
include_once 'db.php';
// AJAX
if (isset($_GET['action']) && $_GET['action'] == 'get_documentos') {
    header('Content-Type: application/json; charset=utf-8');
    $stmt = $pdo->query("SELECT cadastro, descricao, idDocumentos, url, tipo, documento FROM documentos WHERE id_categoria='CONSULTA PUBLICA'");
    echo json_encode($stmt->fetchAll()); exit;
}
?>
<?php include 'header1.php'; ?>
<body>

<?php include 'header2.php'; ?>
<div class="container-fluid position-relative p-0">
  <?php include 'header.php'; ?>
  <div class="page-banner">
    <div class="container">
      <h1 class="animated zoomIn">Consultas Públicas</h1>
      <nav aria-label="breadcrumb">
        <ol class="breadcrumb justify-content-center mb-0">
          <li class="breadcrumb-item"><a href="index.php" class="text-white-50">Início</a></li>
          <li class="breadcrumb-item active">Lista de Consultas Públicas</li>
        </ol>
      </nav>
    </div>
  </div>
</div>

<div class="container-fluid py-5 wow fadeInUp">
  <div class="container">
    <div class="text-center mb-5">
      <span class="section-label">Documentos</span>
      <h2 class="section-heading">Lista de Consultas Públicas</h2>
      <div class="section-divider"></div>
    </div>
    <div class="content-card">
      <div class="content-card-header">
        <h5><i class="bi bi-table me-2"></i>Lista de Consultas Públicas</h5>
      </div>
      <div class="p-3 p-md-4">
        <div class="table-responsive">
          <table class="table table-hover w-100" id="documentosTable">
            <thead><tr>
              <th>#</th><th>Documento</th><th>Descrição</th><th>Anexo</th><th>Tipo</th><th>Data</th>
            </tr></thead>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<?php include 'footer.php'; ?>
<script>
$(document).ready(function () {
    $('#documentosTable').DataTable({
        ajax: { url: 'consulta_publica.php?action=get_documentos', dataSrc: '' },
        columns: [
            { data: null, render: function(d,t,r,m){ return m.row+1; } },
            { data: 'documento' },
            { data: 'descricao' },
            { data: 'url', render: function(d){ return d ? `<a class="btn btn-sgla btn-sm" href="${d}" target="_blank"><i class="bi bi-download me-1"></i>Download</a>` : '<span class="text-muted small">Sem anexo</span>'; } },
            { data: 'tipo' },
            { data: 'cadastro', render: function(d){ return d ? new Date(d).toLocaleDateString('pt-MZ') : '—'; } }
        ],
        order: [[5,'desc']],
        "language": {
            "search": "Pesquisar:",
            "lengthMenu": "_MENU_ por página",
            "zeroRecords": "Nenhum resultado encontrado",
            "info": "A mostrar _START_ a _END_ de _TOTAL_ registos",
            "infoEmpty": "Sem registos",
            "paginate": {"first":"Primeiro","last":"Último","next":"Próximo","previous":"Anterior"}
        }
    });
});
</script>
</body>
</html>
