<?php $pageTitle = 'Admin Dashboard'; ?>
<div class="row g-3 mb-4">
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon orange"><i class="fas fa-user-graduate"></i></div><div><div class="kpi-value"><?= $stats['total_students'] ?></div><div class="kpi-label">Total Students</div></div></div></div>
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon blue"><i class="fas fa-file-alt"></i></div><div><div class="kpi-value"><?= $stats['new_applications'] ?></div><div class="kpi-label">New Applications</div></div></div></div>
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon green"><i class="fas fa-check-circle"></i></div><div><div class="kpi-value"><?= $stats['active_students'] ?></div><div class="kpi-label">Active Students</div></div></div></div>
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon yellow"><i class="fas fa-credit-card"></i></div><div><div class="kpi-value"><?= $stats['pending_payments'] ?></div><div class="kpi-label">Pending Payments</div></div></div></div>
</div>
<div class="row g-3 mb-4">
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon orange"><i class="fas fa-book"></i></div><div><div class="kpi-value"><?= $stats['total_courses'] ?></div><div class="kpi-label">Courses</div></div></div></div>
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon blue"><i class="fas fa-users"></i></div><div><div class="kpi-value"><?= $stats['total_enrollments'] ?></div><div class="kpi-label">Enrollments</div></div></div></div>
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon green"><i class="fas fa-award"></i></div><div><div class="kpi-value"><?= $stats['certificates_issued'] ?></div><div class="kpi-label">Certificates</div></div></div></div>
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon yellow"><i class="fas fa-chart-line"></i></div><div><div class="kpi-value"><?= $stats['pass_rate'] ?>%</div><div class="kpi-label">Pass Rate</div></div></div></div>
</div>
<div class="row g-3 mb-4">
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon green"><i class="fas fa-rand-sign"></i></div><div><div class="kpi-value">R<?= number_format($stats['total_revenue'], 0) ?></div><div class="kpi-label">Revenue</div></div></div></div>
    <div class="col-md-3"><div class="kpi-card"><div class="kpi-icon blue"><i class="fas fa-laptop-code"></i></div><div><div class="kpi-value"><?= $stats['total_tests'] ?></div><div class="kpi-label">Tests Written</div></div></div></div>
</div>
<div class="row g-4">
    <div class="col-lg-6">
        <div class="card-cix">
            <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-file-alt text-orange me-2"></i> Recent Applications</h5><a href="<?= base_url('admin/applications') ?>" class="text-secondary small">View All</a></div>
            <?php if (!empty($recentApplications)): ?>
            <table class="table-cix"><thead><tr><th>Applicant</th><th>Email</th><th>Status</th><th>Date</th></tr></thead><tbody>
            <?php foreach ($recentApplications as $app): ?>
            <tr><td><?= e($app['first_name'] . ' ' . $app['last_name']) ?></td><td class="text-secondary small"><?= e($app['email']) ?></td><td><span class="badge-cix badge-<?= $app['status'] ?>"><?= $app['status'] ?></span></td><td class="text-secondary small"><?= time_elapsed($app['created_at']) ?></td></tr>
            <?php endforeach; ?>
            </tbody></table>
            <?php else: ?><p class="text-secondary text-center p-3">No applications yet</p><?php endif; ?>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="card-cix">
            <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-credit-card text-orange me-2"></i> Recent Payments</h5><a href="<?= base_url('admin/payments') ?>" class="text-secondary small">View All</a></div>
            <?php if (!empty($recentPayments)): ?>
            <table class="table-cix"><thead><tr><th>Student</th><th>Amount</th><th>Status</th><th>Date</th></tr></thead><tbody>
            <?php foreach ($recentPayments as $pay): ?>
            <tr><td><?= e($pay['first_name'] . ' ' . $pay['last_name']) ?> <small class="text-secondary">(<?= e($pay['student_number']) ?>)</small></td><td class="text-orange fw-bold">R<?= number_format($pay['amount'], 2) ?></td><td><span class="badge-cix badge-<?= $pay['status'] ?>"><?= $pay['status'] ?></span></td><td class="text-secondary small"><?= time_elapsed($pay['created_at']) ?></td></tr>
            <?php endforeach; ?>
            </tbody></table>
            <?php else: ?><p class="text-secondary text-center p-3">No payments yet</p><?php endif; ?>
        </div>
    </div>
</div>
<div class="row g-4 mt-2">
    <div class="col-lg-6">
        <div class="card-cix">
            <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-history text-orange me-2"></i> Recent Students</h5><a href="<?= base_url('admin/students') ?>" class="text-secondary small">View All</a></div>
            <?php if (!empty($recentStudents)): ?>
            <table class="table-cix"><thead><tr><th>Student #</th><th>Name</th><th>Email</th><th>Status</th></tr></thead><tbody>
            <?php foreach ($recentStudents as $s): ?>
            <tr><td class="text-orange fw-bold"><?= e($s['student_number']) ?></td><td><?= e($s['first_name'] . ' ' . $s['last_name']) ?></td><td class="text-secondary small"><?= e($s['email']) ?></td><td><span class="badge-cix badge-<?= $s['enrollment_status'] ?>"><?= $s['enrollment_status'] ?></span></td></tr>
            <?php endforeach; ?>
            </tbody></table>
            <?php else: ?><p class="text-secondary text-center p-3">No students yet</p><?php endif; ?>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="card-cix">
            <div class="card-cix-header"><h5 class="mb-0"><i class="fas fa-shield-alt text-orange me-2"></i> Recent Audit Logs</h5><a href="<?= base_url('admin/audit-logs') ?>" class="text-secondary small">View All</a></div>
            <?php if (!empty($recentAuditLogs)): ?>
            <table class="table-cix"><thead><tr><th>User</th><th>Action</th><th>Entity</th><th>When</th></tr></thead><tbody>
            <?php foreach ($recentAuditLogs as $log): ?>
            <tr><td><?= e(($log['first_name'] ?? '') . ' ' . ($log['last_name'] ?? '')) ?></td><td class="text-orange"><?= e($log['action']) ?></td><td class="text-secondary"><?= e($log['entity_type'] ?? '') ?></td><td class="text-secondary small"><?= time_elapsed($log['created_at']) ?></td></tr>
            <?php endforeach; ?>
            </tbody></table>
            <?php else: ?><p class="text-secondary text-center p-3">No audit logs yet</p><?php endif; ?>
        </div>
    </div>
</div>
