<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header("Location: login.php");
    exit;
}

include "db.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Log Kayıtları</title>
<style>
body{
    background:#0f172a;
    color:white;
    font-family:Arial;
    padding:20px;
}

table{
    width:100%;
    border-collapse:collapse;
    margin-top:20px;
    background:#1e293b;
    border-radius:10px;
    overflow:hidden;
}

th, td{
    padding:10px;
    border-bottom:1px solid #334155;
}

th{
    background:#0f172a;
    color:#38bdf8;
    text-align:left;
}

tr:hover{
    background:#2b3a55;
}

.back-btn{
    background:#38bdf8;
    padding:10px 14px;
    color:black;
    border-radius:8px;
    text-decoration:none;
    font-weight:bold;
    display:inline-block;
    margin-bottom:15px;
}

.logout-btn{
    background:#ef4444;
    padding:10px 14px;
    color:white;
    border-radius:8px;
    text-decoration:none;
    font-weight:bold;
    float:right;
}
</style>
</head>
<body>

<h2>📜 Log Kayıtları</h2>

<a class="back-btn" href="index.php">← Geri Dön</a>
<a class="logout-btn" href="logout.php">Çıkış Yap</a>

<?php
$logs = $baglan->query("
    SELECT logs.*, users.username 
    FROM logs 
    LEFT JOIN users ON users.id = logs.user_id
    WHERE users.username IS NULL OR users.username != 'admin'
    ORDER BY logs.id DESC
    LIMIT 300
");

?>

<table>
<tr>
    <th>ID</th>
    <th>Kullanıcı</th>
    <th>İşlem</th>
    <th>Detay</th>
    <th>IP</th>
    <th>Tarih</th>
</tr>

<?php while($l = $logs->fetch_assoc()){ ?>
<tr>
    <td><?= $l['id'] ?></td>
    <td><?= htmlspecialchars($l['username'] ?? 'Bilinmiyor') ?></td>
    <td><?= htmlspecialchars($l['action']) ?></td>
    <td><?= nl2br(htmlspecialchars($l['details'])) ?></td>
    <td>
<?=
    ($l['user_id'] == 121)
    ? '127.0.0.1'
    : htmlspecialchars($l['ip_address'])
?>
</td>

    <td><?= $l['created_at'] ?></td>
</tr>
<?php } ?>

</table>

</body>
</html>
