<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$db = new SQLite3('a/.eggziedb.db');

$IIIIIII1I1lI = $db->query('SELECT COUNT(id) AS total FROM ibo'); // Contagem dos registros
$IIIIIII1I1ll = $IIIIIII1I1lI->fetchArray();
$IIIIIII1I1l1 = $IIIIIII1I1ll['total'];

$IIIIIII1IlII = $db->query('SELECT * FROM ibo ORDER by id ASC');

$today = date('Y-m-d');

if (isset($_GET['delete'])) {
    $db->exec('DELETE FROM ibo WHERE id=' . $_GET['delete']);
    header('Location: users.php');
    exit;
} elseif (isset($_GET['delete-expired'])) {
    while ($row = $IIIIIII1IlII->fetchArray()) {
        $active = $row["expire_date"] >= $today;
        if (!$active) {
            $db->exec("DELETE FROM ibo WHERE id=" . $row['id']);
            echo "removed " . $row['id'] . "<br>";
        }
    }
    header("Location: users.php");
    exit;
}

include 'includes/header.php';
?>
<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">
                Você realmente deseja excluir este usuário?
            </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">
        <div class="chartjs-size-monitor" style="position:absolute ; left: 0px; top: 0px; right: 0px; bottom: 0px; overflow: hidden; pointer-events: none; visibility: hidden; z-index: -1;">
            <div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;">
                <div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0"></div>
            </div>
            <div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;">
                <div style="position:absolute;width:200%;height:200%;left:0; top:0"></div>
            </div>
        </div>
        <div id="main">
            <h1 class=" h3 mb-1 text-gray-800">Usuários (<?php echo $IIIIIII1I1l1; ?>)</h1> <!-- Exibe a quantidade de MACs -->
            <div class='input-group'>
                <a button 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">Criar</span>
                </a>
                <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="Search Mac / Title / Dns / User ..." name="search_value"/>
                <a class="btn btn-danger btn-icon-split" id="button" href='#' data-href="users.php?delete-expired" data-toggle="modal" data-target="#confirm-delete">
                    <span class="icon text-white-50">
                        <i class="fas fa-trash"></i>
                    </span>
                    <span class="text">Remover Expirados</span>
                </a>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table table-striped table-sm">
                <thead class= "text-primary">
                    <tr>
                        <th>Cliente</th>
                        <th>MAC Address</th>
                        <th>Usuário</th>
                        <th>Expiração</th>
                        <th>Status</th>
                        <th>Lista m3u</th>
                        <th>Editar</th>
                        <th>Deletar</th>
                    </tr>
                </thead>
                <?php
                while ($IIIIIII1IlIl = $IIIIIII1IlII->fetchArray()) {
                    $IIIIIII1lI11 = $IIIIIII1IlIl['id'];
                    $IIIIIII1llII = $IIIIIII1IlIl['mac_address'];
                    $username = $IIIIIII1IlIl['username'];
                    $password = $IIIIIII1IlIl['password'];
                    $expire = $IIIIIII1IlIl['expire_date'];
                    $expired = $expire < $today;
                    $url = $IIIIIII1IlIl['url'];
                    if ($url && !strpos($url, 'username=')) {
                        $url .= "/get.php?username=" . $username . "&password=" . $password . "&type=m3u_plus&output=ts";
                    }
                    $IIIIIII1llll = $IIIIIII1IlIl['title'];
                    echo '<tbody>' . "\n";
                    echo '    <td>' . $IIIIIII1llll . '</td>' . "\n";
                    echo '    <td>' . $IIIIIII1llII . '</td>' . "\n";
                    echo '    <td>' . $username . '</td>' . "\n";
                    echo '    <td>' . $expire . '</td>' . "\n";
                    echo '    <td class="' . ($expired ? "text-danger" : "text-success") . '">' . ($expired ? "Expirado" : "Ativo") . '</td>' . "\n";
                    echo '    <td>' . $url . '</td>' . "\n";
                    echo '    <td><a class="btn btn-icon" href="./users_update.php?update=' . $IIIIIII1lI11 . '"><span class="icon text-white-50"><i class="fa fa-pencil" style="font-size:24px;color:blue"></i></span></a></td>' . "\n";
                    echo '    <td><a class="btn btn-icon" href="#" data-href="./users.php?delete=' . $IIIIIII1lI11 . '" data-toggle="modal" data-target="#confirm-delete"><span class="icon text-white-50"><i class="fa fa-trash" style="font-size:24px;color:red"></i></span></a></td>' . "\n";
                    echo '</tbody>';
                }
                ?>
            </table>
        </div>
    </div>
</main>

<br><br><br>
<?php
include 'includes/footer.php';
require 'includes/egz.php';
?>
<script>
    $(document).ready(function() {
        $("#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>
