<head>
<head>
<meta charset="UTF-8">
<title><?php echo $pageTitle; ?></title> <!-- NOME DA PÁGINA -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="Guilherme Bathist" name="author" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<!-- App favicon -->
<link rel="shortcut icon" href="../../assets/images/logo-sm.png">

<!-- App css -->
<link href="../../assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="../../assets/css/icons.min.css" rel="stylesheet" type="text/css" />
<link href="../../assets/css/app.min.css" rel="stylesheet" type="text/css" />
<link href="../../assets/css/style.css" rel="stylesheet" type="text/css" />

<!--Material Icon -->
<link rel="stylesheet" href="https://cdn.materialdesignicons.com/5.4.55/css/materialdesignicons.min.css">

<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.7/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.7/css/dataTables.bootstrap5.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/responsive/2.4.1/css/responsive.bootstrap5.min.css">

<!-- DataTables JS -->
<script type="text/javascript" src="https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.13.7/js/dataTables.bootstrap5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.4.1/js/dataTables.responsive.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/responsive/2.4.1/js/responsive.bootstrap5.min.js"></script>

<script>
function exportTableToExcel(tableId, fileName = '') {
    // Pega a tabela pelo ID
    var table = document.getElementById(tableId);
    if (!table) {
        console.error('Tabela não encontrada');
        return;
    }

    // Se fileName não foi fornecido, usa um nome padrão
    if (!fileName) {
        fileName = 'exportacao_' + new Date().toISOString().slice(0,10);
    }

    // Cria uma string CSV
    var csvContent = [];
    
    // Pega todos os cabeçalhos
    var headers = [];
    var headerCells = table.querySelectorAll('thead th');
    headerCells.forEach(function(cell) {
        headers.push(cell.textContent.trim());
    });
    csvContent.push(headers.join(';'));
    
    // Pega todas as linhas de dados
    var rows = table.querySelectorAll('tbody tr');
    rows.forEach(function(row) {
        var rowData = [];
        row.querySelectorAll('td').forEach(function(cell) {
            var cellContent = cell.textContent.trim();
            // Remove qualquer ponto e vírgula do conteúdo para não quebrar o CSV
            cellContent = cellContent.replace(/;/g, ',');
            rowData.push(cellContent);
        });
        csvContent.push(rowData.join(';'));
    });
    
    // Cria o conteúdo final do CSV
    var csvString = csvContent.join('\n');
    
    // Adiciona BOM para suporte a caracteres especiais no Excel
    var blob = new Blob(['\ufeff' + csvString], { 
        type: 'text/csv;charset=utf-8;' 
    });
    
    // Cria um link para download
    var link = document.createElement("a");
    var url = URL.createObjectURL(blob);
    link.setAttribute("href", url);
    link.setAttribute("download", fileName + '.csv');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

// Função auxiliar para adicionar botão de exportação
function addExportButton(tableId, containerId, fileName = '') {
    var container = document.getElementById(containerId);
    if (container) {
        var exportButton = document.createElement('button');
        exportButton.className = 'btn btn-success mb-3';
        exportButton.innerHTML = '<i class="mdi mdi-file-excel"></i> Exportar para Excel';
        exportButton.onclick = function() {
            exportTableToExcel(tableId, fileName);
        };
        container.appendChild(exportButton);
    }
}
</script>

<style>
    .dataTables_wrapper .dataTables_paginate ul.pagination li {
        padding: 0 !important;
    }
</style>

</head>