<?php

ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
session_start();

if ($_SESSION['store_type'] == 2) {
    header("Location: users_mac.php");
}

error_reporting(32767);
$db = new SQLite3("./a/.ansdb.db");

$db->exec("CREATE TABLE IF NOT EXISTS ibo(
    id INTEGER PRIMARY KEY NOT NULL,
    mac_address VARCHAR(100),
    key VARCHAR(100),
    username VARCHAR(100),
    password VARCHAR(100),
    expire_date VARCHAR(100),
    dns VARCHAR(100),
    epg_url VARCHAR(100),
    title VARCHAR(100),
    url VARCHAR(100),
    type VARCHAR(100),
    id_user INT
)");

$res = $db->query("SELECT * FROM ibo WHERE id_user = " . $_SESSION['id']);

if (isset($_GET["delete"])) {
    $db->exec("DELETE FROM ibo WHERE id=" . $_GET["delete"]);
    $db->close();
    header("Location: users.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>Confirm</h2>
            </div>
            <div class="modal-body">
                Do you really want to delete?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>

<main role="main" class="col-15 pt-4 px-5">
    <div class="row justify-text-center">
        <br>
        <h2>Painel do Usuário</h2>
        <div class="input-group">
            <a class="btn btn-success btn-icon-split" id="button" href="./users_create.php">
                <span class="icon text-white-50"><i class="fas fa-check"></i></span>
                <span class="text">Novo</span>
            </a>&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="Pesquise por MAC, Nome ou Usuário..." name="search_value"/>
        </div>
    </div>
    <div class="table-responsive">
        <table id="myTable" class="table table-striped table-sm">
            <thead class="text-black">
                <tr>
                    <th>ID</th>
                    <th>MAC</th>
                    <th>Usuário</th>
                    <th>Nome</th>
                    <th>Editar</th>
                    <th>Excluir</th>
                </tr>
            </thead>
            <tbody class="text-danger">
                <?php
                $db = new SQLite3("./a/.ansdb.db");
                $res = $db->query("SELECT * FROM ibo WHERE id_user = " . $_SESSION['id']);

                while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
                    $playlist_password = isset($row['playlistpassword']) ? $row['playlistpassword'] : null;
                    $iid = $row["id"];
                    $imac = isset($row["mac_address"]) ? $row["mac_address"] : 'N/A';
                    $ikey = isset($row["key"]) ? $row["key"] : 'N/A';
                    $iexpire_date = isset($row["expire_date"]) ? $row["expire_date"] : 'N/A';
                    $iusername = isset($row["dns"]) && $row["dns"] !== NULL ? $row["username"] : "listm3u-id" . $row["id"];
                    $idns = isset($row["dns"]) ? $row["dns"] : $row["url"];
                    $iepg = isset($row["epg_url"]) ? $row["epg_url"] : 'N/A';
                    $ititle = isset($row["title"]) ? $row["title"] : 'N/A';

                    if ($playlist_password) {
                        $idns = "****";
                        $iepg = "****";
                        $ititle = "****";
                    }

                    echo "<tr>
                            <td>{$iid}</td>
                            <td>{$imac}</td>
                            <td>{$iusername}</td>
                            <td>{$ititle}</td>
                            <td>
                                <a class='btn btn-success' style='padding: 0.25rem 0.5rem; font-size: 1rem;' href='./users_update.php?update={$iid}'>
                                    <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 btn-icon' style='padding: 0.25rem 0.5rem; font-size: 1rem;' href='#' data-href='./users.php?delete=" . htmlspecialchars($iid) . "' 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>
                          </tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
</main>
<br><br><br>

<?php
include "includes/footer.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">

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