<?php $bp = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<div class="admin-page-header">
    <h2>Dashboard</h2>
</div>

<!-- Stats Cards -->
<div class="admin-stats-grid">
    <div class="admin-stat-card">
        <div class="admin-stat-icon admin-stat-icon-blue">&#9782;</div>
        <div class="admin-stat-info">
            <span class="admin-stat-value"><?= htmlspecialchars((string)($totalStudents ?? 0), ENT_QUOTES, 'UTF-8') ?></span>
            <span class="admin-stat-label">Total Students</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon admin-stat-icon-purple">&#9998;</div>
        <div class="admin-stat-info">
            <span class="admin-stat-value"><?= htmlspecialchars((string)($totalExams ?? 0), ENT_QUOTES, 'UTF-8') ?></span>
            <span class="admin-stat-label">Total Exams</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon admin-stat-icon-green">&#8377;</div>
        <div class="admin-stat-info">
            <span class="admin-stat-value">&#8377; <?= htmlspecialchars(number_format((float)($totalFeeCollection ?? 0), 2), ENT_QUOTES, 'UTF-8') ?></span>
            <span class="admin-stat-label">Fee Collected</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon admin-stat-icon-red">&#8377;</div>
        <div class="admin-stat-info">
            <span class="admin-stat-value">&#8377; <?= htmlspecialchars(number_format((float)($totalFeeDue ?? 0), 2), ENT_QUOTES, 'UTF-8') ?></span>
            <span class="admin-stat-label">Fee Due</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon admin-stat-icon-orange">&#9638;</div>
        <div class="admin-stat-info">
            <span class="admin-stat-value">&#8377; <?= htmlspecialchars(number_format((float)($walletBalance ?? 0), 2), ENT_QUOTES, 'UTF-8') ?></span>
            <span class="admin-stat-label">Wallet Balance</span>
        </div>
    </div>
</div>

<!-- Recent Students -->
<div class="admin-card">
    <div class="admin-card-header">
        <h3>Recent Students</h3>
        <a href="<?= $bp ?>/branch/students" class="admin-btn admin-btn-sm admin-btn-outline">View All</a>
    </div>
    <div class="admin-card-body">
        <?php if (!empty($recentStudents)): ?>
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Enrollment No</th>
                        <th>Name</th>
                        <th>Course</th>
                        <th>Status</th>
                        <th>Fee Due</th>
                        <th>Joined</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recentStudents as $s): ?>
                    <tr>
                        <td><?= htmlspecialchars($s['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                        <td>
                            <a href="<?= $bp ?>/branch/students/<?= (int)($s['id'] ?? 0) ?>">
                                <?= htmlspecialchars($s['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                            </a>
                        </td>
                        <td><?= htmlspecialchars($s['course_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                        <td>
                            <span class="admin-badge admin-badge-<?= ($s['status'] ?? '') === 'active' ? 'success' : 'warning' ?>">
                                <?= htmlspecialchars(ucfirst($s['status'] ?? ''), ENT_QUOTES, 'UTF-8') ?>
                            </span>
                        </td>
                        <td>&#8377; <?= htmlspecialchars(number_format((float)($s['fee_due'] ?? 0), 2), ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= htmlspecialchars(date('d M Y', strtotime($s['created_at'] ?? 'now')), ENT_QUOTES, 'UTF-8') ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <p class="admin-empty-text">No students found.</p>
        <?php endif; ?>
    </div>
</div>

<!-- Recent Exams -->
<div class="admin-card">
    <div class="admin-card-header">
        <h3>Recent Exams</h3>
        <a href="<?= $bp ?>/branch/exams" class="admin-btn admin-btn-sm admin-btn-outline">View All</a>
    </div>
    <div class="admin-card-body">
        <?php if (!empty($recentExams)): ?>
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Course</th>
                        <th>Duration</th>
                        <th>Total Marks</th>
                        <th>Status</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recentExams as $e): ?>
                    <tr>
                        <td>
                            <a href="<?= $bp ?>/branch/exams/<?= (int)($e['id'] ?? 0) ?>/questions">
                                <?= htmlspecialchars($e['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                            </a>
                        </td>
                        <td><?= htmlspecialchars($e['course_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= (int)($e['duration_minutes'] ?? 0) ?> min</td>
                        <td><?= (int)($e['total_marks'] ?? 0) ?></td>
                        <td>
                            <span class="admin-badge admin-badge-<?= !empty($e['is_published']) ? 'success' : 'secondary' ?>">
                                <?= !empty($e['is_published']) ? 'Published' : 'Draft' ?>
                            </span>
                        </td>
                        <td><?= htmlspecialchars(date('d M Y', strtotime($e['created_at'] ?? 'now')), ENT_QUOTES, 'UTF-8') ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <p class="admin-empty-text">No exams found.</p>
        <?php endif; ?>
    </div>
</div>
