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

// Proteção simples de sessão (opcional, adicione conforme sua necessidade)
if (!isset($_SESSION['id'])) {
    header("Location: login_mac.php");
    exit;
}

$db1 = new SQLite3("./api/.logs.db");

// Lógica de Exclusão
if (isset($_GET["delete"])) {
    if ($_GET["delete"] == "all") {
        $db1->exec("DELETE FROM logs");
        header("Location: snoop.php");
        exit;
    } else {
        $id_del = (int)$_GET["delete"];
        $db1->exec("DELETE FROM logs WHERE id=$id_del");
        header("Location: snoop.php");
        exit;
    }
}

$res1 = $db1->query("SELECT * FROM logs ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Snoop Logs - Painel Ultra</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;800&display=swap" rel="stylesheet">
    
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <style>
        :root {
            --bg-body: #020617;
            --card-bg: #0f172a;
            --accent: #3b82f6;
            --text-main: #f8fafc;
            --text-muted: #94a3b8;
            --border: #1e293b;
            --danger: #ef4444;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            background-color: var(--bg-body);
            color: var(--text-main);
            font-family: 'Poppins', sans-serif;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: flex-start; /* Alinhado ao topo para listas longas */
            padding: 40px 20px;
        }

        .container { width: 100%; max-width: 900px; }

        .ultra-card {
            background: var(--card-bg);
            border: 1px solid var(--border);
            border-radius: 24px;
            padding: 30px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            border: none;
            background: transparent;
        }

        .card-header h2 { font-weight: 800; font-size: 1.5rem; color: var(--text-main); }

        /* Tabela Ultra Dark */
        .table-responsive { border-radius: 15px; overflow: hidden; background: #070e1b; }
        .table { margin-bottom: 0; color: var(--text-main); }
        .table thead th {
            background: #1e293b;
            color: var(--accent);
            border: none;
            text-transform: uppercase;
            font-size: 0.75rem;
            letter-spacing: 1px;
            padding: 15px;
        }
        .table td {
            border-top: 1px solid var(--border);
            padding: 15px;
            vertical-align: middle;
            font-size: 0.9rem;
        }

        /* Botões e Badges */
        .btn-delete-all {
            background: rgba(239, 68, 68, 0.1);
            color: var(--danger);
            border: 1px solid var(--danger);
            padding: 8px 15px;
            border-radius: 10px;
            font-weight: 600;
            font-size: 0.8rem;
            text-decoration: none;
            transition: 0.3s;
        }
        .btn-delete-all:hover { background: var(--danger); color: white; }

        .btn-action-del {
            color: var(--danger);
            font-size: 1.1rem;
            transition: 0.3s;
        }
        .btn-action-del:hover { transform: scale(1.2); color: #ff5f5f; }

        .btn-back {
            display: inline-block;
            margin-top: 20px;
            color: var(--text-muted);
            text-decoration: none;
            font-size: 0.85rem;
        }

        /* Modal Customizado */
        .modal-content {
            background: var(--card-bg);
            border: 1px solid var(--border);
            border-radius: 20px;
            color: white;
        }
        .modal-header { border-bottom: 1px solid var(--border); }
        .modal-footer { border-top: 1px solid var(--border); }
    </style>
</head>
<body>

<div class="container">
    <div class="ultra-card">
        <div class="card-header">
            <h2><i class="fa-solid fa-eye" style="color: var(--accent);"></i> Snoop Logs</h2>
            <a href="#" data-href="snoop.php?delete=all" data-toggle="modal" data-target="#confirm-delete" class="btn-delete-all">
                <i class="fa-solid fa-trash-can"></i> LIMPAR TUDO
            </a>
        </div>

        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>Endereço IP</th>
                        <th>Data e Hora</th>
                        <th style="text-align: center;">Ação</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row1 = $res1->fetchArray()): ?>
                    <tr>
                        <td style="font-family: monospace; font-weight: bold; color: var(--accent);">
                            <?php echo $row1["ipaddress"]; ?>
                        </td>
                        <td style="color: var(--text-muted);">
                            <?php echo $row1["date"]; ?>
                        </td>
                        <td style="text-align: center;">
                            <a href="#" data-href="snoop.php?delete=<?php echo $row1["id"]; ?>" data-toggle="modal" data-target="#confirm-delete" class="btn-action-del">
                                <i class="fa-solid fa-trash-can"></i>
                            </a>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
        
        <a href="playlists_mac.php" class="btn-back">
            <i class="fa-solid fa-arrow-left"></i> Voltar ao Painel
        </a>
    </div>
</div>

<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Confirmar Exclusão</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true" style="color: white;">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Deseja realmente excluir este registro de log? Esta ação não pode ser desfeita.
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" style="border-radius: 10px;" data-dismiss="modal">Cancelar</button>
                <a class="btn btn-danger btn-ok" style="border-radius: 10px; font-weight: bold;">Sim, Excluir</a>
            </div>
        </div>
    </div>
</div>

<script>
    // Script para passar o link de exclusão para o botão do modal
    $('#confirm-delete').on('show.bs.modal', function(e) {
        $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
    });
</script>

</body>
</html>
<?php $db1->close(); ?>