<?php
header('Content-Type: text/html; charset=utf-8');
require 'db.php';
require 'auth.php';

require_login();
$user = current_user();

// sadece admin görsün istersen:
if ($user['role'] != 1) {
    die("Bu sayfaya erişim yetkiniz yok.");
}

// Filtreler
$userId = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 0;
$action = trim($_GET['action'] ?? '');
$date   = trim($_GET['date']   ?? '');

$conditions = [];
$params     = [];

$sql = "
    SELECT s.*, u.name AS user_name
    FROM system_logs s
    LEFT JOIN users u ON s.user_id = u.id
";

if ($userId > 0) {
    $conditions[] = "s.user_id = ?";
    $params[] = $userId;
}
if ($action !== '') {
    $conditions[] = "s.action = ?";
    $params[] = $action;
}
if ($date !== '') {
    $conditions[] = "DATE(s.created_at) = ?";
    $params[] = $date;
}

if (!empty($conditions)) {
    $sql .= " WHERE ".implode(" AND ", $conditions);
}

$sql .= " ORDER BY s.id DESC LIMIT 1000";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$logs = $stmt->fetchAll(PDO::FETCH_ASSOC);

// filtre için kullanıcı listesi
$usersStmt = $pdo->query("SELECT id, name FROM users ORDER BY name");
$usersAll  = $usersStmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Sistem Logları</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
    <div class="top-bar">
        <div><strong>Hoş geldin, <?php echo htmlspecialchars($user['name']); ?></strong></div>
        <div>
            <a href="jobs.php" class="btn">İşler</a>
            <a href="system_logs.php" class="btn">Sistem Logları</a>
            <a href="logout.php" class="btn">Çıkış</a>
        </div>
    </div>

    <div class="card">
        <h1>Sistem Logları (Full)</h1>

        <form method="get" style="display:flex;flex-wrap:wrap;gap:10px;margin-bottom:10px;">
            <label>
                Kullanıcı:
                <select name="user_id">
                    <option value="0">Tümü</option>
                    <?php foreach ($usersAll as $u): ?>
                        <option value="<?php echo $u['id']; ?>"
                            <?php echo ($userId == $u['id']) ? 'selected' : ''; ?>>
                            <?php echo htmlspecialchars($u['name']); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </label>

            <label>
                İşlem:
                <input type="text" name="action"
                       value="<?php echo htmlspecialchars($action); ?>"
                       placeholder="login_success, page_view...">
            </label>

            <label>
                Tarih:
                <input type="date" name="date"
                       value="<?php echo htmlspecialchars($date); ?>">
            </label>

            <button type="submit" class="btn">Filtrele</button>
            <?php if ($userId || $action || $date): ?>
                <a href="system_logs.php" class="btn">Temizle</a>
            <?php endif; ?>
        </form>

        <div class="table-wrapper">
            <table>
                <tr>
                    <th>ID</th>
                    <th>Kullanıcı</th>
                    <th>Action</th>
                    <th>Detay</th>
                    <th>IP</th>
                    <th>Tarayıcı</th>
                    <th>Tarih</th>
                </tr>
                <?php if (empty($logs)): ?>
                    <tr><td colspan="7" style="text-align:center;">Log yok.</td></tr>
                <?php else: ?>
                    <?php foreach ($logs as $l): ?>
                        <tr>
                            <td><?php echo $l['id']; ?></td>
                            <td><?php echo htmlspecialchars($l['user_name'] ?? '-'); ?></td>
                            <td><?php echo htmlspecialchars($l['action']); ?></td>
                            <td><?php echo nl2br(htmlspecialchars($l['details'] ?? '')); ?></td>
                            <td><?php echo htmlspecialchars($l['ip_address']); ?></td>
                            <td style="max-width:250px;"><?php echo htmlspecialchars($l['user_agent']); ?></td>
                            <td><?php echo htmlspecialchars($l['created_at']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>
</body>
</html>
