<?php
// activity_log.php — Admin only.
require_once 'includes/auth.php';
require_role(['admin']);

require_once 'db.php';

$jobFilter = $_GET['job_id'] ?? '';

$sql = "SELECT al.*, j.Job_No, j.Category, u.Name AS UserName
        FROM tbl_activity_log al
        LEFT JOIN tbl_jobs j ON j.ID = al.JobID
        LEFT JOIN tbl_users u ON u.UserID = al.UserID
        WHERE 1=1";
$params = [];
if (!empty($jobFilter)) {
    $sql .= " AND al.JobID = ?";
    $params[] = $jobFilter;
}
$sql .= " ORDER BY al.Created_At DESC LIMIT 300";

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

$actionBadges = [
    'created' => 'bg-primary', 'edited' => 'bg-secondary',
    'approved' => 'bg-success', 'unlocked' => 'bg-warning text-dark',
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Activity Log — Liftek</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container my-4" style="max-width: 950px;">

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h3 class="fw-bold m-0"><i class="fa-solid fa-clock-rotate-left text-primary me-2"></i> Activity Log</h3>
        <a href="index.php" class="btn btn-outline-secondary btn-sm"><i class="fa-solid fa-arrow-left me-1"></i> Back to Register</a>
    </div>

    <?php if (!empty($jobFilter)): ?>
        <div class="alert alert-info py-2 d-flex justify-content-between align-items-center">
            Showing activity for Job ID #<?= (int) $jobFilter; ?> only.
            <a href="activity_log.php" class="btn btn-sm btn-outline-primary">Show All</a>
        </div>
    <?php endif; ?>

    <div class="card shadow-sm border-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle m-0">
                <thead class="table-dark">
                    <tr>
                        <th>When</th>
                        <th>Job No</th>
                        <th>Action</th>
                        <th>By</th>
                        <th>Details</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($logs)): ?>
                        <tr><td colspan="5" class="text-center text-muted py-4">No activity recorded yet.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($logs as $log): ?>
                        <tr>
                            <td class="small text-muted"><?= htmlspecialchars($log['Created_At']); ?></td>
                            <td class="fw-bold"><?= htmlspecialchars($log['Job_No'] ?? '#' . $log['JobID']); ?></td>
                            <td><span class="badge <?= $actionBadges[$log['Action']] ?? 'bg-secondary'; ?> text-capitalize"><?= htmlspecialchars($log['Action']); ?></span></td>
                            <td><?= htmlspecialchars($log['UserName'] ?? 'Unknown'); ?></td>
                            <td class="small text-muted"><?= htmlspecialchars($log['Details'] ?? ''); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
