<?php
require '../../conexao/conexao.php';
require '../../sessao.php';
require '../../configuacoes.php';
require '../../conexao/acesso.php';
require '../../querys/query-metas.php';
require '../../logs/gera-log.php';
require '../../conexao/logs.php';

gerarLog(
    1, // tipo de log (acesso)
    $basePath, // módulo
    $_SESSION['CodUsuario'],
    $_SESSION['NomeUsuario']
);

// if (isset($_GET['status']) && $_GET['status'] === 'sucesso' && isset($_GET['acao']) && $_GET['acao'] === 'atualizar') {
//     gerarLog(
//         3, // tipo de log (atualização)
//         $basePath, // id do módulo
//         $_SESSION['CodUsuario'],
//         $_SESSION['NomeUsuario']
//     );
// }

// if (isset($_GET['status']) && $_GET['status'] === 'sucesso' && isset($_GET['acao']) && $_GET['acao'] === 'excluir') {
//     gerarLog(
//         4, // tipo de log (exclusão)
//         $basePath, // id do módulo
//         $_SESSION['CodUsuario'],
//         $_SESSION['NomeUsuario']
//     );
// }

// if (isset($_GET['status']) && $_GET['status'] === 'sucesso' && isset($_GET['acao']) && $_GET['acao'] === 'criar') {
//     gerarLog(
//         2, // tipo de log (exclusão)
//         $basePath, // id do módulo
//         $_SESSION['CodUsuario'],
//         $_SESSION['NomeUsuario']
//     );
// }

$Metas = new Metas($connectionmysql);

$listagemMetas = $Metas->listarMetas();

//print_r($listagemMetas);

if (isset($_SESSION['message'])) {
    echo "<script>alert('" . $_SESSION['message'] . "');</script>";
    unset($_SESSION['message']); // Limpa a mensagem após exibição
}

$Metas->desativaMeta($connectionmysql);
?>

<!DOCTYPE html>
<html lang="pt-BR">

<head>
    <link rel="icon" href="\KPI\pages/configuracoes/logo-sm.png">
    <link rel="shortcut icon" href="\KPI\pages/configuracoes/logo-sm.png">

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap CSS -->

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

<?php $pageTitle = 'Configuração de Metas - Manda Receita';

include('../header.php');
include('../menu.php');

?>


<body>

    <form action="" method="post" enctype="multipart/form-data">
        <div class="wrapper candidato">
            <div class="container-fluid">
                <div class="card-box mt-3">
                    <div class="row">
                        <div class="col-6 text-left mb-4">
                            <h4 class="page-title">Configura Metas</h4>

                        </div>
                        <div class="col-6 text-right">

                            <a href="add-metas.php" class="btn btn-light" alt="Incluir Metas" title="Incluir Metas"><i class="mdi mdi-plus"></i> Adicionar Metas</a>


                        </div>
                        <div class="col-12" style="margin-top: -15px;">
                            <hr>
                        </div>

                    </div>
                    <div class="col-12">
                        <table id="minhatabela" class="table table-striped table-bordered dt-responsive nowrap">
                            <thead>
                                <tr>
                                    <th valign="middle" nowrap="nowrap">Metas Cadastradas</th>
                                    <th valign="middle" nowrap="nowrap" style="text-align: center">Ações</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($listagemMetas as $itens) { ?>
                                    <tr>
                                        <td width="87%"><?php echo $itens['nome_meta']; ?></td>
                                        <td width="13%" align="center" nowrap="nowrap">
                                            <a href="alteracao_metas.php?id=<?php echo $itens['id']; ?>"
                                                class="table-actions link-view"
                                                title="Alterar Metas">
                                                <i class="mdi mdi mdi-account-edit"></i>
                                            </a>

                                            <button onclick="window.location.href='listagem-metas.php?id=<?php echo $itens['id']; ?>';"
                                                type="button"
                                                class="table-actions link-view excluir-btn"
                                                title="Excluir Meta">
                                                <i class="mdi mdi-delete-forever"></i>
                                            </button>
                                        </td>
                                    </tr>
                                <?php } ?>

                                <?php
                                // Aqui o log é gravado apenas 1x (após montar a tabela)
                                gerarLog(
                                    5, // tipo de log (acesso)
                                    $basePath, // módulo
                                    $_SESSION['CodUsuario'],
                                    $_SESSION['NomeUsuario']
                                );
                                ?>
                            </tbody>
                        </table>

                    </div>
                </div>
            </div>
        </div>
    </form>

    <?php include('../footer.php'); ?>
    <?php include('../mensagens_modais.php'); ?>

    <!-- Vendor js -->
    <script src="../../../assets/js/vendor.min.js"></script>

    <!-- knob plugin -->
    <script src="../../../assets/libs/jquery-knob/jquery.knob.min.js"></script>
    <!-- App js-->
    <script src="../../../assets/js/app.min.js"></script>
    <script src="../../../assets/js/funcoes.js"></script>



    <script src="../../geral/javascript.js"></script>

    <!-- third party js -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.3/js/dataTables.bootstrap5.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/dataTables.bootstrap5.min.css">

    <script src="https://cdn.datatables.net/buttons/2.0.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.66/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.66/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/2.0.1/js/buttons.html5.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.0.1/css/buttons.dataTables.min.css">
    <link rel="stylesheet" href="../../../assets/css/resetedatatable.css">

</body>

</html>

<script>
    $(document).ready(function() {
        var table = $('#minhatabela').DataTable({
            "language": {
                "url": "https://cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"
            },
            "lengthMenu": [
                [10, 25, 50, 100, -1],
                [10, 25, 50, 100, "Todos"]
            ],
            "buttons": [
                // 'copyHtml5',
                'excelHtml5',
                'csvHtml5',
                // 'pdfHtml5'
            ],
            dom: '<"d-flex justify-content-between align-items-center mb-2"Bf>t<"datatable-footer d-flex justify-content-between align-items-center mt-2 w-100"<"footer-left"l><"footer-center"i><"footer-right"p>>',
            "order": [
                [0, 'asc']
            ], // Ordenar pela primeira coluna em ordem ascendente

        });

        setTimeout(function() {
            $("#minhatabela_filter").children("label").css("font-size", "15px");
        }, 150);
        setTimeout(function() {
            $("#minhatabela_length").children("label").css("font-size", "15px");
        }, 150);
    });
</script>
<style>
    .dataTables_length label,
    .dataTables_info {
        margin: 0 !important;
        padding: 0 !important;
        align-items: center;
    }
</style>