<?php
include_once 'db.php';
if (isset($_GET['action']) && $_GET['action'] == 'get_consultores') {
    header('Content-Type: application/json; charset=utf-8');
    $stmt = $pdo->query("SELECT id_auditor, numero_processo, numero_certificado, cod, nome_empresa, nuit, area_consultoria, telefone, email, taxa_certificado FROM auditores WHERE forma_registo='Individual'");
    echo json_encode($stmt->fetchAll()); exit;
}
if (isset($_GET['action']) && $_GET['action'] == 'get_consultor_detalhes' && isset($_GET['id_auditor'])) {
    header('Content-Type: application/json; charset=utf-8');
    $stmt = $pdo->prepare("SELECT * FROM consultor WHERE id_auditor = ?");
    $stmt->execute([$_GET['id_auditor']]);
    echo json_encode($stmt->fetch()); exit;
}
?>
<?php include 'header1.php'; ?>
<body>

<!-- Modal -->
<div class="modal fade" id="consultorModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog modal-xl modal-dialog-scrollable">
    <div class="modal-content border-0 rounded-xl overflow-hidden">
      <div class="modal-header sgla-modal-header">
        <h5 class="modal-title"><i class="bi bi-shield-check me-2"></i>Detalhes do Auditor Individual</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body p-4">
        <div class="row g-4">
          <div class="col-lg-6">
            <h6 class="fw-bold text-green mb-3"><i class="bi bi-info-circle me-1"></i> Dados Básicos</h6>
            <ul class="list-group list-group-flush">
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Nº Processo</span><strong id="m_numero_processo">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Nº Certificado</span><strong id="m_numero_certificado">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Código Pasta</span><strong id="m_cod">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Nome</span><strong id="m_nome_empresa">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Telefone</span><strong id="m_telefone">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">E-mail</span><strong id="m_email">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Local de Trabalho</span><strong id="m_local_trabalho">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Forma de Registo</span><strong id="m_forma_registo">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Seguro Profissional</span><strong id="m_seguro">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Data Emissão Licença</span><strong id="m_data_emissao1">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Data Validade Licença</span><strong id="m_data_validade1">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Ano Registo</span><strong id="m_ano">—</strong></li>
            </ul>
          </div>
          <div class="col-lg-6">
            <h6 class="fw-bold text-green mb-3"><i class="bi bi-person me-1"></i> Identificação Pessoal</h6>
            <ul class="list-group list-group-flush">
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Tipo Documento</span><strong id="m_tipo_doc">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Nº Documento</span><strong id="m_num_doc">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Data Emissão</span><strong id="m_data_emissao">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Data Validade</span><strong id="m_data_validade">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Local Emissão</span><strong id="m_local_emissao">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Residência</span><strong id="m_residencia">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Nacionalidade</span><strong id="m_nacionalidade">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Profissão</span><strong id="m_profissao">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Data Nascimento</span><strong id="m_data_nasc">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">NUIT</span><strong id="m_nuit">—</strong></li>
              <li class="list-group-item d-flex justify-content-between"><span class="text-muted">Área Consultoria</span><strong id="m_area">—</strong></li>
            </ul>
          </div>
        </div>
      </div>
      <div class="modal-footer border-0">
        <button type="button" class="btn btn-outline-secondary rounded-pill px-4" data-bs-dismiss="modal">Fechar</button>
      </div>
    </div>
  </div>
</div>

<?php include 'header2.php'; ?>
<div class="container-fluid position-relative p-0">
  <?php include 'header.php'; ?>
  <div class="page-banner">
    <div class="container">
      <h1>Auditores Individuais</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">Auditores Individuais</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">Auditores Registados</span>
      <h2 class="section-heading">Lista de Auditores Individuais</h2>
      <div class="section-divider"></div>
    </div>
    <div class="content-card">
      <div class="content-card-header">
        <h5><i class="bi bi-people me-2"></i>Auditores Individuais</h5>
      </div>
      <div class="p-3 p-md-4">
        <div class="table-responsive">
          <table class="table table-hover w-100" id="myTable">
            <thead><tr>
              <th>Nº Processo</th><th>Nº Certificado</th><th>Código</th><th>Nome</th>
              <th>NUIT</th><th>Área Consultoria</th><th>Telefone</th><th>Email</th>
              <th>Taxa (MZN)</th><th>Detalhe</th>
            </tr></thead>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<?php include 'footer.php'; ?>

<script>
$(document).ready(function () {
    $('#myTable').DataTable({
        ajax: { url: 'auditor_individual.php?action=get_consultores', dataSrc: '' },
        columns: [
            { data: 'numero_processo' }, { data: 'numero_certificado' }, { data: 'cod' },
            { data: 'nome_empresa' }, { data: 'nuit' }, { data: 'area_consultoria' },
            { data: 'telefone' }, { data: 'email' },
            { data: 'taxa_certificado', render: $.fn.dataTable.render.number('.', ',', 2, 'MZN ') },
            { data: null, orderable: false, render: function(d,t,r){ return `<button class="btn btn-sgla btn-sm btnView" data-id="${r.id_auditor}"><i class="bi bi-eye"></i></button>`; } }
        ],
        order: [[0,'desc']],
        language: { search: 'Pesquisar:', lengthMenu: '_MENU_ por página',
            zeroRecords: 'Nenhum resultado', info: 'A mostrar _START_-_END_ de _TOTAL_',
            paginate: {first:'Primeiro',last:'Último',next:'Próximo',previous:'Anterior'} }
    });
});

$(document).on('click', '.btnView', function() {
    $.getJSON('auditor_individual.php', { action: 'get_consultor_detalhes', id_auditor: $(this).data('id') }, function(d) {
        $('#m_numero_processo').text(d.numero_processo||'—');
        $('#m_numero_certificado').text(d.numero_certificado||'—');
        $('#m_cod').text(d.cod||'—');
        $('#m_nome_empresa').text(d.nome_empresa||'—');
        $('#m_telefone').html((d.telefone_empresa||d.telefone||'—').replace(/\//g,'<br>'));
        $('#m_email').html((d.email_empresa||d.email||'—').replace(/\//g,'<br>'));
        $('#m_local_trabalho').text(d.local_trabalho||'—');
        $('#m_forma_registo').text(d.forma_registo||'—');
        $('#m_seguro').text(d.seguro_profissional||'—');
        $('#m_data_emissao1').text(d.data_emissao1||'—');
        $('#m_data_validade1').text(d.data_validade1||'—');
        $('#m_ano').text(d.ano||'—');
        $('#m_tipo_doc').text(d.tipo_documento||'—');
        $('#m_num_doc').text(d.numero_documento||'—');
        $('#m_data_emissao').text(d.data_emissao||'—');
        $('#m_data_validade').text(d.data_validade||'—');
        $('#m_local_emissao').text(d.local_emissao||'—');
        $('#m_residencia').text(d.residencia_responsavel||'—');
        $('#m_nacionalidade').text(d.nacionalidade_empresa||'—');
        $('#m_profissao').text(d.profissao||'—');
        $('#m_data_nasc').text(d.data_nasc||'—');
        $('#m_nuit').text(d.nuit||'—');
        $('#m_area').text(d.area_consultoria||'—');
        $('#consultorModal').modal('show');
    });
});
</script>
</body></html>
