<?php include __DIR__ . '/../../layouts/header.php'; ?>
<?php include __DIR__ . '/../../layouts/sidebar.php'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="mb-0">System Audit Logs</h2>
    <span class="badge bg-light text-secondary rounded-pill px-3 py-2 border">Tracking 200 Latest Events</span>
</div>

<div class="card border-0 shadow-sm rounded-4 overflow-hidden">
    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0">
            <thead class="bg-light">
                <tr>
                    <th class="ps-4">Timestamp</th>
                    <th>Admin</th>
                    <th>Action</th>
                    <th>Target</th>
                    <th>IP Address</th>
                    <th class="pe-4">Details</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($logs)): ?>
                    <tr>
                        <td colspan="6" class="text-center py-5 text-muted">No audit logs found.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($logs as $log): ?>
                        <tr>
                            <td class="ps-4 text-secondary small">
                                <?= date('M d, Y', strtotime($log['created_at'])) ?><br>
                                <span class="fw-bold"><?= date('H:i:s', strtotime($log['created_at'])) ?></span>
                            </td>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <div style="width: 32px; height: 32px; border-radius: 8px; background: var(--primary-light); color: var(--primary); display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: bold;">
                                        <?= strtoupper(substr($log['admin_name'] ?? 'S', 0, 1)) ?>
                                    </div>
                                    <span class="fw-semibold"><?= htmlspecialchars($log['admin_name'] ?? 'System') ?></span>
                                </div>
                            </td>
                            <td>
                                <?php 
                                    $actionClass = 'bg-secondary';
                                    if (strpos(strtolower($log['action']), 'approve') !== false || strpos(strtolower($log['action']), 'active') !== false) $actionClass = 'bg-success';
                                    if (strpos(strtolower($log['action']), 'reject') !== false || strpos(strtolower($log['action']), 'block') !== false || strpos(strtolower($log['action']), 'suspend') !== false || strpos(strtolower($log['action']), 'delete') !== false) $actionClass = 'bg-danger';
                                ?>
                                <span class="badge <?= $actionClass ?> bg-opacity-10 text-<?= str_replace('bg-', '', $actionClass) ?> px-3 py-2" style="font-weight: 600;">
                                    <?= htmlspecialchars(ucwords(str_replace('_', ' ', $log['action']))) ?>
                                </span>
                            </td>
                            <td>
                                <span class="text-secondary"><?= htmlspecialchars(ucfirst($log['target_type'])) ?></span>
                                <?php if ($log['target_id']): ?>
                                    <span class="fw-bold text-dark ms-1">#<?= $log['target_id'] ?></span>
                                <?php endif; ?>
                            </td>
                            <td><code class="small text-muted"><?= htmlspecialchars($log['ip_address'] ?? '0.0.0.0') ?></code></td>
                            <td class="pe-4">
                                <?php if ($log['details']): ?>
                                    <button class="btn btn-sm btn-light rounded-pill px-3" type="button" data-bs-toggle="collapse" data-bs-target="#details-<?= $log['id'] ?>">
                                        View Data
                                    </button>
                                    <div class="collapse mt-2" id="details-<?= $log['id'] ?>">
                                        <div class="bg-light p-2 rounded small" style="max-width: 300px; word-break: break-all;">
                                            <pre class="mb-0" style="white-space: pre-wrap;"><code><?= htmlspecialchars($log['details']) ?></code></pre>
                                        </div>
                                    </div>
                                <?php else: ?>
                                    <span class="text-muted small">No extra data</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php include __DIR__ . '/../../layouts/footer.php'; ?>
