<?php
require '../../conexao/conexao.php';
//require '../../sessao.php';
require '../../configuacoes.php';
require '../../conexao/acesso.php';
require '../../querys/query-filiais.php';
require '../../querys/query-cidades.php';
require '../../conexao/logs.php';

if (isset($_SESSION['CodUsuario']) && isset($_SESSION['NomeUsuario'])) {
    registrarLogAcesso(75, $_SESSION['CodUsuario'], $_SESSION['NomeUsuario']);
}

$cidades = new Cidades($connectionmysql);

$filiais = new Filiais($connectionmysql);

$listagemFiliais = $filiais->listarFiliais();

//print_r($listagemFiliais);

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

$filiais->deletaFiliais($connectionmysql);
?>


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

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

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="modalexcluir.css">
    <!--Material Icon -->
    <link rel="stylesheet" href="https://cdn.materialdesignicons.com/5.4.55/css/materialdesignicons.min.css">
</head>

<?php $pageTitle = 'Listar Filiais';

include('../header.php'); ?>

<!-- Navigation Bar-->
<?php
include('../menu.php');
?>
<!-- End Navigation Bar-->

<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">Lista Filiais</h4>
                        </div>
                        <div class="col-6 text-right">
                            <a href="add_filiais.php" class="btn btn-light" alt="Incluir Filiais" title="Incluir Filiais"><i class="mdi mdi-plus"></i> Adicionar Filial</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" width="100%">
                            <thead>
                                <tr>
                                    <th>Código Prime</th>
                                    <th>Filiais Cadastradas</th>
                                    <th>Cidade</th>
                                    <th>Status</th>
                                    <th style="text-align: center">Ações</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($listagemFiliais as $itens) {
                                    $listaCidades = $cidades->listarCidades($itens['id_cidade'])[0];
                                ?>
                                    <tr>
                                        <td><?php echo $itens['codigo_fc']; ?></td>
                                        <td><?php echo $itens['nome_filial']; ?></td>
                                        <td><?php echo $listaCidades['nome_cidade']; ?></td>
                                        <td><?php echo ($itens['situacao'] == 'S') ? 'Ativo' : 'Desativado'; ?></td>
                                        <td align="center">
                                            <a href="alteracao_filiais.php?id=<?php echo $itens['id']; ?>" class="table-actions link-view" title="Alterar Filiais">
                                                <i class="mdi mdi-account-edit"></i>
                                            </a>
                                            <button data-id="<?php echo $itens['id']; ?>" type="button" class="table-actions link-view excluir-btn abrir-modal-exclusao" title="Excluir Meta">

                                                <i class="mdi mdi-delete-forever"></i>
                                            </button>
                                        </td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>


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

    <div id="myModalPercentual" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">×</span>
            <h2>Deseja realmente excluir esta filial?</h2>
            <form id="formExclusao" method="get" action="index.php">
                <input type="hidden" id="id_filial" name="id" value="">
                <div class="modal-actions">
                    <button type="button" class="btn btn-secondary" onclick="closeModal()">Cancelar</button>
                    <button type="button" class="btn btn-danger ml-2" onclick="confirmarExclusao()">Excluir</button>
                </div>
            </form>
        </div>
    </div>

    <?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": [
                'excelHtml5',
                'csvHtml5',
            ],
            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']
            ],
        });

        setTimeout(function() {
            $("#minhatabela_filter").children("label").css("font-size", "15px");
        }, 150);
        setTimeout(function() {
            $("#minhatabela_length").children("label").css("font-size", "15px");
        }, 150);

        document.querySelectorAll('.abrir-modal-exclusao').forEach(function(button) {
            button.addEventListener('click', function() {
                const filialId = this.getAttribute('data-id');
                document.getElementById('id_filial').value = filialId;
                document.getElementById('myModalPercentual').style.display = 'block';
            });
        });
    });

    function closeModal() {
        document.getElementById('myModalPercentual').style.display = 'none';
    }

    function confirmarExclusao() {
        document.getElementById('formExclusao').submit();
    }
</script>

<style>
    .dataTables_length label,
    .dataTables_info {
        margin: 0 !important;
        padding: 0 !important;
        align-items: center;
    }
</style>