<?php
ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
session_start();

$isAdmin = $_SESSION['admin'];

if(!$isAdmin){
    header("Location: login.php");
    exit();
}

error_reporting(32767);

if (isset($_GET["delete"])) {
    $db = new SQLite3("./a/.anspanel.db");

    $delete_id = $_GET['delete'];
    print_r($_GET);

    echo $delete_id;
    $db->exec("DELETE FROM USERS WHERE id= '$delete_id'");
    $db->close();

    $db = new SQLite3("a/.ansdb.db");
    $db->exec("DELETE FROM ibo WHERE id_user = '$delete_id'");
    $db->close();

    header("Location: stores.php");
}

include "includes/header.php";
?>


<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v6.0.0-beta3/css/all.css">


<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h2>Confirmar</h2>
            </div>
            <div class="modal-body">
                Tem certeza de que deseja excluir?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <a class="btn btn-danger btn-ok">Excluir</a>
            </div>
        </div>
    </div>
</div>

<main role="main" class="col-15 pt-4 px-5">
    <div class="row justify-text-center">
        <div id="main">
            <br>
            <h2>Painel de Revendas</h2>
            <div class="input-group ">
                <a button class="btn btn-success btn-icon-split" id="button" href="stores_create.php">
                    <span class="icon text-white-50"><i class="fas fa-check"></i></span><span class="text">Criar</span>
                </a>&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      
                <div class="input-group-prepend">
                    <span class="input-group-text" style="font-size:24px;color:#1cc88a">
                        <i class="fas fa-search"></i>
                    </span>
                </div>
                <input class="form-control" type="text" id="search" placeholder="Pesquisar"  name="search_value"/>
            </div>
        </div>

        <div class="table-responsive">
            <table id="myTable" class="table table-striped table-sm">
                <thead class='text-danger'>
                    <tr>
                        <th>ID</th>
                        <th>Nome</th>
                        <th>Nome de Usuário</th>
                        <th>MACs</th>
                        <th>MACs Disponíveis</th>
                        <th>Editar</th>
                        <th>Excluir</th>
                        <th>Tipo</th>
                    </tr>
                </thead>

                <?php
                $db = new SQLite3("./a/.anspanel.db");
                $db2 = new SQLite3("./a/.ansdb.db");
                $res = $db->query("SELECT * FROM USERS WHERE (ADMIN != 1 OR ADMIN IS NULL) AND store_type = 1");

                while ($row = $res->fetchArray()) {
                    $id = $row["id"];
                    $name = $row["NAME"];
                    $username = $row["USERNAME"];
                    $macs = $row["mac_amount"];

                    $macsUsed = $db2->query("SELECT COUNT(*) as count FROM ibo WHERE id_user = '$id' AND active = 1 AND expire_date > date('now')");
                    $macsUsed = $macsUsed->fetchArray()['count'];

                    $macsAvailable = $macs - $macsUsed;

                    echo "<tbody class='text-danger'>
                            <tr>
                                <td>{$id}</td>
                                <td>{$name}</td>
                                <td>{$username}</td>
                                <td>{$macs}</td>
                                <td>{$macsAvailable}</td>
                                <td><a class='btn btn-success' style='padding: 0.25rem 0.5rem; font-size: 1rem;' href='./stores_update.php?update={$id}'><span class='icon text-white-50'><i class='fa fa-pencil' style='font-size:15px;color:blue'></i></span></a></td>
                                <td><a class='btn btn-danger' style='padding: 0.25rem 0.5rem; font-size: 1rem;' href='#' data-href='./stores.php?delete={$id}' data-toggle='modal' data-target='#confirm-delete'><span class='icon text-white-50'><i class='fa fa-trash' style='font-size:15px;color:white'></i></span></a></td>";

                    // Adiciona uma coluna para mostrar o tipo (Administrador ou Usuário comum)
                    echo "<td>";
                    if ($row['ADMIN'] == 1) {
                        echo "Administrador";
                    } else {
                        echo "Usuário Revenda";
                    }
                    echo "</td>";

                    echo "</tr>
                        </tbody>";
                }
                ?>
            </table>
        </div>
    </main>
    <br><br><br>
    <?php include "includes/footer.php"; ?>

    <script>
    $("#search").keyup(function () {
        var value = this.value.toLowerCase().trim();

        $("table tr").each(function (index) {
            if (!index) return;
            $(this).find("td").each(function () {
                var id = $(this).text().toLowerCase().trim();
                var not_found = (id.indexOf(value) == -1);
                $(this).closest('tr').toggle(!not_found);
                return not_found;
            });
        });
    });

    $('#confirm-delete').on('show.bs.modal', function(e) {
        $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
    });
    </script>
</body>
</html>
