<?php
require 'auth_check.php';
require '../config/db.php';

// Clear Logs (Optional: Only allow if you want to wipe history)
if (isset($_GET['clear'])) {
    $pdo->query("TRUNCATE TABLE system_logs");
    header("Location: logs.php"); exit();
}

// Fetch Logs
$logs = $pdo->query("SELECT * FROM system_logs ORDER BY created_at DESC LIMIT 200")->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>System Activity Logs</title>
    <link rel="stylesheet" href="admin_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .log-table { width: 100%; background: white; border-collapse: collapse; font-size: 13px; }
        .log-table th { background: #f8f9fa; padding: 10px; text-align: left; border-bottom: 2px solid #eee; }
        .log-table td { padding: 10px; border-bottom: 1px solid #eee; color: #555; }
        .badge { padding: 2px 6px; border-radius: 4px; font-size: 10px; font-weight: bold; text-transform: uppercase; }
        .b-admin { background: #8e44ad; color: white; }
        .b-user { background: #95a5a6; color: white; }
    </style>
</head>
<body>

    <div class="sidebar">
        <?php include 'sidebar.php'; ?>
    </div>

    <div class="content">
        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:20px;">
            <h1><i class="fas fa-history"></i> Activity Logs</h1>
            <a href="logs.php?clear=1" class="btn-view" style="background:red; color:white; padding:5px 10px; border-radius:4px; text-decoration:none;" onclick="return confirm('Clear all logs?')">Clear History</a>
        </div>

        <div style="background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 5px rgba(0,0,0,0.05);">
            <table class="log-table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>User</th>
                        <th>Action</th>
                        <th>Details</th>
                        <th>IP Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($logs as $log): ?>
                    <tr>
                        <td style="white-space:nowrap; color:#888;"><?php echo date('d M H:i', strtotime($log['created_at'])); ?></td>
                        <td>
                            <strong><?php echo htmlspecialchars($log['user_name']); ?></strong><br>
                            <span class="badge b-<?php echo $log['role']; ?>"><?php echo $log['role']; ?></span>
                        </td>
                        <td style="font-weight:bold; color:#2c3e50;"><?php echo htmlspecialchars($log['action']); ?></td>
                        <td><?php echo htmlspecialchars($log['details']); ?></td>
                        <td style="font-family:monospace;"><?php echo $log['ip_address']; ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</body>
</html>