<?php
/**
 * Admin Audit Logs View
 * Displays a chronological, searchable log of all significant system actions.
 */
$actionColors = [
    'CREATE'  => 'success',
    'UPDATE'  => 'warning',
    'DELETE'  => 'danger',
    'LOGIN'   => 'info',
    'LOGOUT'  => 'secondary',
    'APPROVE' => 'primary',
    'REJECT'  => 'danger',
    'TOGGLE'  => 'warning',
    'VIEW'    => 'light',
];
?>
<div class="container-fluid py-4 px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold font-outfit text-dark m-0"><i class="fa-solid fa-file-lines text-secondary me-2"></i>Audit Logs</h2>
            <p class="text-muted mb-0">Complete record of system activities and admin actions.</p>
        </div>
        <form action="<?= APP_URL ?>/admin/audit-logs/export" method="POST">
            <?= csrf_field() ?>
            <button class="btn btn-outline-secondary rounded-pill px-4 font-outfit fw-bold">
                <i class="fa-solid fa-file-export me-2"></i>Export CSV
            </button>
        </form>
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm p-4 mb-4">
        <form action="<?= APP_URL ?>/admin/audit-logs" method="GET" class="row g-3 align-items-end">
            <div class="col-md-3">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Search</label>
                <div class="input-group">
                    <span class="input-group-text bg-white border-end-0"><i class="fa-solid fa-search text-muted"></i></span>
                    <input type="text" name="search" class="form-control border-start-0 ps-0" placeholder="User name, action description..." value="<?= htmlspecialchars($search ?? '') ?>">
                </div>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Action Type</label>
                <select name="action" class="form-select">
                    <option value="">All Actions</option>
                    <?php foreach (array_keys($actionColors) as $action): ?>
                        <option value="<?= $action ?>" <?= ($selectedAction ?? '') === $action ? 'selected' : '' ?>><?= ucfirst(strtolower($action)) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">User Role</label>
                <select name="role" class="form-select">
                    <option value="">All Roles</option>
                    <?php foreach (['admin', 'mentor', 'tutor', 'student'] as $r): ?>
                        <option value="<?= $r ?>" <?= ($selectedRole ?? '') === $r ? 'selected' : '' ?>><?= ucfirst($r) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Date From</label>
                <input type="date" name="date_from" class="form-control" value="<?= htmlspecialchars($dateFrom ?? '') ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Date To</label>
                <input type="date" name="date_to" class="form-control" value="<?= htmlspecialchars($dateTo ?? '') ?>">
            </div>
            <div class="col-md-1">
                <button class="btn btn-secondary rounded-pill w-100 font-outfit fw-bold"><i class="fa-solid fa-filter"></i></button>
            </div>
        </form>
    </div>

    <!-- Logs Timeline -->
    <?php if (empty($logs)): ?>
        <div class="text-center py-5">
            <div class="text-5xl mb-3">📋</div>
            <h5 class="fw-bold font-outfit text-dark">No Audit Logs Found</h5>
            <p class="text-muted">Try adjusting your search filters.</p>
        </div>
    <?php else: ?>
        <div class="card border-0 shadow-sm">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover align-middle text-sm mb-0">
                        <thead class="table-light">
                            <tr>
                                <th class="ps-4 py-3 border-0 font-outfit fw-bold text-muted">#</th>
                                <th class="border-0 font-outfit fw-bold text-muted">User</th>
                                <th class="border-0 font-outfit fw-bold text-muted">Action</th>
                                <th class="border-0 font-outfit fw-bold text-muted">Description</th>
                                <th class="border-0 font-outfit fw-bold text-muted">Entity</th>
                                <th class="border-0 font-outfit fw-bold text-muted">IP Address</th>
                                <th class="border-0 font-outfit fw-bold text-muted">Timestamp</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($logs as $idx => $log): 
                                $actionType = strtoupper($log['action_type'] ?? 'VIEW');
                                $color = $actionColors[$actionType] ?? 'secondary';
                            ?>
                                <tr>
                                    <td class="ps-4 text-muted"><?= (($currentPage ?? 1) - 1) * ($perPage ?? 50) + $idx + 1 ?></td>
                                    <td>
                                        <div class="d-flex align-items-center gap-2">
                                            <div class="badge-icon-box bg-primary-subtle text-primary rounded-circle flex-shrink-0" style="width:30px;height:30px;display:flex;align-items:center;justify-content:center;font-size:11px;">
                                                <?= strtoupper(substr($log['user_name'] ?? 'U', 0, 1)) ?>
                                            </div>
                                            <div>
                                                <span class="fw-bold font-outfit text-dark d-block"><?= htmlspecialchars($log['user_name'] ?? 'System') ?></span>
                                                <small class="text-muted"><?= htmlspecialchars($log['user_role'] ?? '') ?></small>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-<?= $color ?>-subtle text-<?= $color ?> rounded-pill font-outfit fw-bold text-xs">
                                            <?= htmlspecialchars($actionType) ?>
                                        </span>
                                    </td>
                                    <td class="text-dark max-w-xs" style="max-width:280px;">
                                        <span title="<?= htmlspecialchars($log['description'] ?? '') ?>"><?= htmlspecialchars(mb_strimwidth($log['description'] ?? '', 0, 80, '...')) ?></span>
                                    </td>
                                    <td>
                                        <?php if (!empty($log['entity_type'])): ?>
                                            <span class="badge bg-light text-secondary rounded-pill text-xs font-outfit">
                                                <?= htmlspecialchars($log['entity_type']) ?> #<?= htmlspecialchars($log['entity_id'] ?? '') ?>
                                            </span>
                                        <?php else: ?>
                                            <span class="text-muted">—</span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="text-muted font-monospace text-xs"><?= htmlspecialchars($log['ip_address'] ?? '—') ?></td>
                                    <td class="text-muted text-xs">
                                        <?= date('d M Y', strtotime($log['created_at'])) ?>
                                        <br>
                                        <small><?= date('H:i:s', strtotime($log['created_at'])) ?></small>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- Pagination -->
        <?php if (($totalPages ?? 1) > 1): ?>
            <nav class="mt-4 d-flex justify-content-between align-items-center">
                <small class="text-muted">Showing <?= count($logs) ?> of <?= number_format($total ?? 0) ?> logs</small>
                <ul class="pagination pagination-sm gap-1 mb-0">
                    <?php for ($p = max(1, ($currentPage ?? 1) - 3); $p <= min($totalPages, ($currentPage ?? 1) + 3); $p++): ?>
                        <li class="page-item <?= $p == ($currentPage ?? 1) ? 'active' : '' ?>">
                            <a class="page-link rounded-circle border-0 shadow-sm" href="<?= APP_URL ?>/admin/audit-logs?page=<?= $p ?>&search=<?= urlencode($search ?? '') ?>&action=<?= urlencode($selectedAction ?? '') ?>"><?= $p ?></a>
                        </li>
                    <?php endfor; ?>
                </ul>
            </nav>
        <?php endif; ?>
    <?php endif; ?>
</div>
