<?php
// pages/activity_logs.php
requireRole('admin');

// Pagination / Limits
$limit = 50;
$page = isset($_GET['p']) ? (int)$_GET['p'] : 1;
$offset = ($page - 1) * $limit;

// Total Count
$total = $pdo->query("SELECT COUNT(*) FROM activity_logs")->fetchColumn();
$total_pages = ceil($total / $limit);

// Fetch Logs
$stmt = $pdo->prepare("SELECT * FROM activity_logs ORDER BY created_at DESC LIMIT :limit OFFSET :offset");
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$logs = $stmt->fetchAll();
?>

<h1>System Activity Logs</h1>

<div class="card">
    <div style="margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center;">
        <span>Total Records: <strong><?php echo number_format($total); ?></strong></span>
        <div>
            <?php if ($page > 1): ?>
                <a href="index.php?page=activity_logs&p=<?php echo $page - 1; ?>" class="btn">Previous</a>
            <?php endif; ?>
            
            <span style="margin: 0 10px;">Page <?php echo $page; ?> of <?php echo $total_pages; ?></span>
            
            <?php if ($page < $total_pages): ?>
                <a href="index.php?page=activity_logs&p=<?php echo $page + 1; ?>" class="btn">Next</a>
            <?php endif; ?>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>Time</th>
                    <th>User</th>
                    <th>Action</th>
                    <th>Description</th>
                    <th>IP Address</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($logs)): ?>
                    <tr><td colspan="5" style="text-align: center;">No activity recorded yet.</td></tr>
                <?php else: ?>
                    <?php foreach ($logs as $log): ?>
                    <tr>
                        <td style="white-space: nowrap;"><?php echo date('Y-m-d H:i:s', strtotime($log['created_at'])); ?></td>
                        <td>
                            <?php echo htmlspecialchars($log['username'] ?? 'Unknown'); ?>
                            <?php if ($log['user_id']) echo " <small class='text-muted'>(ID: {$log['user_id']})</small>"; ?>
                        </td>
                        <td>
                            <?php 
                                $badgeColor = '#666'; 
                                if ($log['action'] == 'LOGIN') $badgeColor = 'var(--success-color)';
                                elseif ($log['action'] == 'LOGOUT') $badgeColor = '#ff9800';
                                elseif ($log['action'] == 'DELETE') $badgeColor = 'var(--danger-color)';
                                elseif ($log['action'] == 'CREATE') $badgeColor = 'var(--primary-color)';
                            ?>
                            <span style="background: <?php echo $badgeColor; ?>; padding: 2px 6px; border-radius: 4px; font-size: 0.8rem; color: white;">
                                <?php echo htmlspecialchars($log['action']); ?>
                            </span>
                        </td>
                        <td><?php echo htmlspecialchars($log['description']); ?></td>
                        <td><?php echo htmlspecialchars($log['ip_address']); ?></td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
