<!-- Students Management -->
<div class="admin-section">
    <?php if ($showDetail ?? false): ?>
    <!-- Student Detail View -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Student Details</h2>
            <div class="admin-card-actions">
                <a href="<?= url('/admin/students/' . (int) $viewStudent['id'] . '/edit') ?>" class="admin-btn admin-btn-sm admin-btn-primary">Edit</a>
                <a href="<?= url('/admin/students') ?>" class="admin-btn admin-btn-sm admin-btn-secondary">Back to List</a>
            </div>
        </div>
        <div class="admin-card-body">
            <div class="admin-detail-grid">
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Name</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Email</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['student_email'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Enrollment No</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['enrollment_no'] ?? '—', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Course</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Branch</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['branch_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Status</span>
                    <span class="admin-detail-value">
                        <?php
                        $statusClass = match($viewStudent['status'] ?? 'active') {
                            'active' => 'admin-badge-success',
                            'completed' => 'admin-badge-info',
                            'dropped' => 'admin-badge-danger',
                            default => '',
                        };
                        ?>
                        <span class="admin-badge <?= $statusClass ?>"><?= htmlspecialchars(ucfirst($viewStudent['status'] ?? 'active'), ENT_QUOTES, 'UTF-8') ?></span>
                    </span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Date of Birth</span>
                    <span class="admin-detail-value"><?= $viewStudent['dob'] ? date('M j, Y', strtotime($viewStudent['dob'])) : '—' ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Phone</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['student_phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Address</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['address'] ?? '—', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="admin-detail-item">
                    <span class="admin-detail-label">Guardian</span>
                    <span class="admin-detail-value"><?= htmlspecialchars($viewStudent['guardian_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></span>
                </div>
            </div>

            <!-- Exam Results -->
            <?php if (!empty($examResults)): ?>
            <h3 style="margin-top: 24px; margin-bottom: 12px;">Exam Results</h3>
            <div class="admin-table-responsive">
                <table class="admin-table">
                    <thead>
                        <tr>
                            <th>Exam</th>
                            <th>Score</th>
                            <th>Percentage</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($examResults as $result): ?>
                        <tr>
                            <td><?= htmlspecialchars($result['exam_title'] ?? 'Unknown', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= (int) ($result['total_obtained'] ?? 0) ?> / <?= (int) ($result['total_marks'] ?? 0) ?></td>
                            <td><?= number_format((float) ($result['percentage'] ?? 0), 1) ?>%</td>
                            <td>
                                <?php if (($result['status'] ?? '') === 'pass'): ?>
                                <span class="admin-badge admin-badge-success">Pass</span>
                                <?php else: ?>
                                <span class="admin-badge admin-badge-danger">Fail</span>
                                <?php endif; ?>
                            </td>
                            <td><?= date('M j, Y', strtotime($result['completed_at'] ?? $result['created_at'] ?? 'now')) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <?php elseif ($showEdit ?? false): ?>
    <!-- Edit Student Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Edit Student</h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= url('/admin/students/' . (int)$editStudent['id']) ?>" class="admin-form" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label class="admin-form-label">Student Name</label>
                        <input type="text" class="admin-form-input" disabled
                               value="<?= htmlspecialchars($editStudent['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label class="admin-form-label">Email</label>
                        <input type="email" class="admin-form-input" disabled
                               value="<?= htmlspecialchars($editStudent['student_email'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="course_id" class="admin-form-label">Course</label>
                        <select id="course_id" name="course_id" class="admin-form-select">
                            <?php foreach ($courses ?? [] as $course): ?>
                            <option value="<?= (int) $course['id'] ?>" <?= ($editStudent['course_id'] == $course['id']) ? 'selected' : '' ?>>
                                <?= htmlspecialchars($course['name'], ENT_QUOTES, 'UTF-8') ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="admin-form-group">
                        <label for="branch_id" class="admin-form-label">Branch</label>
                        <select id="branch_id" name="branch_id" class="admin-form-select">
                            <?php foreach ($branches ?? [] as $branch): ?>
                            <option value="<?= (int) $branch['id'] ?>" <?= ($editStudent['branch_id'] == $branch['id']) ? 'selected' : '' ?>>
                                <?= htmlspecialchars($branch['name'] . ' (' . $branch['city'] . ')', ENT_QUOTES, 'UTF-8') ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="admin-form-group">
                    <label for="status" class="admin-form-label">Status</label>
                    <select id="status" name="status" class="admin-form-select">
                        <option value="active" <?= ($editStudent['status'] ?? 'active') === 'active' ? 'selected' : '' ?>>Active</option>
                        <option value="completed" <?= ($editStudent['status'] ?? '') === 'completed' ? 'selected' : '' ?>>Completed</option>
                        <option value="dropped" <?= ($editStudent['status'] ?? '') === 'dropped' ? 'selected' : '' ?>>Dropped</option>
                    </select>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary">Update Student</button>
                    <a href="<?= url('/admin/students') ?>" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>

    <?php else: ?>
    <!-- Students List Table -->
    <div class="admin-card">
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Enrollment No</th>
                            <th>Name</th>
                            <th>Course</th>
                            <th>Branch</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($students as $student): ?>
                        <tr>
                            <td><code><?= htmlspecialchars($student['enrollment_no'] ?? '—', ENT_QUOTES, 'UTF-8') ?></code></td>
                            <td><strong><?= htmlspecialchars($student['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong></td>
                            <td><?= htmlspecialchars($student['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= htmlspecialchars($student['branch_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td>
                                <?php
                                $statusClass = match($student['status'] ?? 'active') {
                                    'active' => 'admin-badge-success',
                                    'completed' => 'admin-badge-info',
                                    'dropped' => 'admin-badge-danger',
                                    default => '',
                                };
                                ?>
                                <span class="admin-badge <?= $statusClass ?>"><?= htmlspecialchars(ucfirst($student['status'] ?? 'active'), ENT_QUOTES, 'UTF-8') ?></span>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <a href="<?= url('/admin/students/' . (int) $student['id']) ?>" class="admin-btn-icon" title="View">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>
                                    </a>
                                    <a href="<?= url('/admin/students/' . (int) $student['id'] . '/edit') ?>" class="admin-btn-icon" title="Edit">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                                    </a>
                                    <button type="button" class="admin-btn-icon admin-btn-delete" title="Delete"
                                            data-delete-url="<?= url('/admin/students/' . (int) $student['id'] . '/delete') ?>">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>
