<?php
require_once 'config.php';
if (!isset($_SESSION['logged_in'])) { header('Location: login.php'); exit; }
require_once 'db.php';
$db = getDB();

$page  = max(1, (int)($_GET['page'] ?? 1));
$limit = 50;
$offset= ($page-1)*$limit;
$total = $db->query("SELECT COUNT(*) FROM verify_logs")->fetchColumn();
$pages = ceil($total/$limit);
$logs  = $db->query("SELECT * FROM verify_logs ORDER BY created_at DESC LIMIT $limit OFFSET $offset")->fetchAll();
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Loglar — UA Changer Panel</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;background:#0A0E1A;color:#F0F4FF}
.sidebar{position:fixed;left:0;top:0;bottom:0;width:220px;background:#111827;border-right:1px solid #1E2D45;padding:24px 0}
.sidebar-logo{padding:0 20px 24px;border-bottom:1px solid #1E2D45}
.sidebar-logo .brand{color:#00D4FF;font-size:11px;font-weight:700;letter-spacing:2px}
.sidebar-logo h1{font-size:18px;font-weight:700;margin-top:2px}
.nav-item{display:flex;align-items:center;gap:10px;padding:10px 20px;color:#7B8DB0;text-decoration:none;font-size:14px}
.nav-item:hover,.nav-item.active{color:#F0F4FF;background:#1A2235}
.main{margin-left:220px;padding:32px}
.page-header{margin-bottom:24px}
.page-header h2{font-size:24px;font-weight:700}
.card{background:#111827;border:1px solid #1E2D45;border-radius:12px;padding:24px}
table{width:100%;border-collapse:collapse;font-size:13px}
th{text-align:left;padding:10px 12px;color:#7B8DB0;font-size:11px;font-weight:700;border-bottom:1px solid #1E2D45}
td{padding:10px 12px;border-bottom:1px solid #0F1929}
tr:hover td{background:#0F1929}
.badge{padding:2px 8px;border-radius:10px;font-size:11px;font-weight:700}
.success{background:#0D2B1A;color:#00E676}
.error,.device_limit{background:#2B0D0D;color:#FF4757}
.not_found,.invalid,.expired,.revoked{background:#2B1A00;color:#FFB300}
.pagination{display:flex;gap:8px;margin-top:16px;justify-content:center}
.pagination a,.pagination span{padding:6px 12px;border-radius:6px;text-decoration:none;font-size:13px;border:1px solid #1E2D45;color:#7B8DB0}
.pagination a:hover,.pagination .current{border-color:#00D4FF;color:#00D4FF}
</style>
</head>
<body>
<div class="sidebar">
    <div class="sidebar-logo">
        <div class="brand">⚡ UA CHANGER</div>
        <h1>Lisans Paneli</h1>
    </div>
    <nav style="padding:16px 0">
        <a href="index.php" class="nav-item"><span>🏠</span> Panel</a>
        <a href="logs.php" class="nav-item active"><span>📋</span> Loglar</a>
        <a href="https://t.me/enployer" target="_blank" class="nav-item"><span>✈</span> Telegram</a>
    </nav>
</div>
<div class="main">
    <div class="page-header">
        <h2>Doğrulama Logları</h2>
        <p style="color:#7B8DB0;font-size:14px;margin-top:4px">Toplam <?= $total ?> kayıt</p>
    </div>
    <div class="card">
        <table>
            <thead>
                <tr><th>TARİH</th><th>ANAHTAR</th><th>CİHAZ ID</th><th>IP</th><th>SONUÇ</th><th>MESAJ</th></tr>
            </thead>
            <tbody>
            <?php foreach ($logs as $log): ?>
            <tr>
                <td style="color:#7B8DB0;white-space:nowrap"><?= $log['created_at'] ?></td>
                <td style="font-family:monospace;color:#00D4FF"><?= htmlspecialchars($log['lic_key'] ?? '—') ?></td>
                <td style="color:#7B8DB0;font-size:11px"><?= htmlspecialchars(substr($log['device_id']??'',0,20)) ?>...</td>
                <td style="color:#7B8DB0;font-size:12px"><?= htmlspecialchars($log['ip']??'') ?></td>
                <td><span class="badge <?= $log['result'] ?>"><?= $log['result'] ?></span></td>
                <td style="color:#7B8DB0;font-size:12px"><?= htmlspecialchars($log['message']??'') ?></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php if ($pages > 1): ?>
        <div class="pagination">
            <?php for ($i=1; $i<=$pages; $i++): ?>
                <?php if ($i===$page): ?>
                    <span class="current"><?= $i ?></span>
                <?php else: ?>
                    <a href="?page=<?= $i ?>"><?= $i ?></a>
                <?php endif; ?>
            <?php endfor; ?>
        </div>
        <?php endif; ?>
    </div>
</div>
</body>
</html>
