<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Exams Management -->
<div class="admin-section">
    <?php if ($showQuestions ?? false): ?>
    <!-- Questions Management for a specific Exam -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Questions — <?= htmlspecialchars($editExam['title'] ?? '', ENT_QUOTES, 'UTF-8') ?></h2>
            <div class="admin-card-actions">
                <a href="<?= $basePath ?>/admin/exams" class="admin-btn admin-btn-sm admin-btn-secondary">Back to Exams</a>
            </div>
        </div>
        <div class="admin-card-body">
            <!-- Add Question Form -->
            <div class="admin-form-section">
                <h3 class="admin-form-section-title">Add New Question</h3>
                <form method="POST" action="<?= $basePath ?>/admin/exams/<?= (int) $editExam['id'] ?>/questions" class="admin-form" novalidate>
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

                    <div class="admin-form-group">
                        <label for="question_text" class="admin-form-label">Question Text <span class="admin-required">*</span></label>
                        <textarea id="question_text" name="question_text" class="admin-form-textarea" rows="3" required></textarea>
                    </div>

                    <div class="admin-form-row">
                        <div class="admin-form-group">
                            <label for="option_a" class="admin-form-label">Option A <span class="admin-required">*</span></label>
                            <input type="text" id="option_a" name="option_a" class="admin-form-input" required>
                        </div>
                        <div class="admin-form-group">
                            <label for="option_b" class="admin-form-label">Option B <span class="admin-required">*</span></label>
                            <input type="text" id="option_b" name="option_b" class="admin-form-input" required>
                        </div>
                    </div>

                    <div class="admin-form-row">
                        <div class="admin-form-group">
                            <label for="option_c" class="admin-form-label">Option C</label>
                            <input type="text" id="option_c" name="option_c" class="admin-form-input">
                        </div>
                        <div class="admin-form-group">
                            <label for="option_d" class="admin-form-label">Option D</label>
                            <input type="text" id="option_d" name="option_d" class="admin-form-input">
                        </div>
                    </div>

                    <div class="admin-form-row">
                        <div class="admin-form-group">
                            <label for="correct_answer" class="admin-form-label">Correct Answer</label>
                            <select id="correct_answer" name="correct_answer" class="admin-form-select">
                                <option value="a">A</option>
                                <option value="b">B</option>
                                <option value="c">C</option>
                                <option value="d">D</option>
                            </select>
                        </div>
                        <div class="admin-form-group">
                            <label for="marks" class="admin-form-label">Marks</label>
                            <input type="number" id="marks" name="marks" class="admin-form-input" min="1" value="5">
                        </div>
                        <div class="admin-form-group">
                            <label for="sort_order" class="admin-form-label">Sort Order</label>
                            <input type="number" id="sort_order" name="sort_order" class="admin-form-input" min="0" value="<?= count($examQuestions ?? []) + 1 ?>">
                        </div>
                    </div>

                    <div class="admin-form-actions">
                        <button type="submit" class="admin-btn admin-btn-primary">Add Question</button>
                    </div>
                </form>
            </div>

            <!-- Questions List -->
            <?php if (!empty($examQuestions)): ?>
            <h3 style="margin-top: 24px; margin-bottom: 12px;">Current Questions (<?= count($examQuestions) ?>)</h3>
            <div class="admin-table-responsive">
                <table class="admin-table">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Question</th>
                            <th>Options</th>
                            <th>Correct</th>
                            <th>Marks</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($examQuestions as $idx => $q): ?>
                        <tr>
                            <td><?= (int) ($idx + 1) ?></td>
                            <td><?= htmlspecialchars($q['question_text'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td>
                                <small>
                                    A: <?= htmlspecialchars($q['option_a'], ENT_QUOTES, 'UTF-8') ?><br>
                                    B: <?= htmlspecialchars($q['option_b'], ENT_QUOTES, 'UTF-8') ?><br>
                                    C: <?= htmlspecialchars($q['option_c'], ENT_QUOTES, 'UTF-8') ?><br>
                                    D: <?= htmlspecialchars($q['option_d'], ENT_QUOTES, 'UTF-8') ?>
                                </small>
                            </td>
                            <td><span class="admin-badge admin-badge-success"><?= strtoupper($q['correct_answer']) ?></span></td>
                            <td><?= (int) $q['marks'] ?></td>
                            <td>
                                <form method="POST" action="<?= $basePath ?>/admin/exams/<?= (int) $editExam['id'] ?>/questions/<?= (int) $q['id'] ?>/delete" style="display:inline">
                                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                    <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" data-confirm-delete>Remove</button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <p class="admin-empty-text" style="margin-top: 16px;">No questions added yet. Use the form above to add questions.</p>
            <?php endif; ?>
        </div>
    </div>

    <?php elseif ($showForm ?? false): ?>
    <!-- Add/Edit Exam Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title"><?= $editExam ? 'Edit Exam' : 'Add New Exam' ?></h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $editExam ? $basePath . '/admin/exams/' . (int)$editExam['id'] : $basePath . '/admin/exams' ?>" 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 for="title" class="admin-form-label">Exam Title <span class="admin-required">*</span></label>
                        <input type="text" id="title" name="title" class="admin-form-input" required
                               value="<?= htmlspecialchars($editExam['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="course_id" class="admin-form-label">Course <span class="admin-required">*</span></label>
                        <select id="course_id" name="course_id" class="admin-form-select" required>
                            <option value="">Select Course</option>
                            <?php foreach ($courses ?? [] as $course): ?>
                            <option value="<?= (int) $course['id'] ?>" <?= ($editExam['course_id'] ?? '') == $course['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($course['name'], ENT_QUOTES, 'UTF-8') ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="duration_minutes" class="admin-form-label">Duration (minutes)</label>
                        <input type="number" id="duration_minutes" name="duration_minutes" class="admin-form-input" min="1"
                               value="<?= htmlspecialchars($editExam['duration_minutes'] ?? '60', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="total_marks" class="admin-form-label">Total Marks</label>
                        <input type="number" id="total_marks" name="total_marks" class="admin-form-input" min="1"
                               value="<?= htmlspecialchars($editExam['total_marks'] ?? '100', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="pass_marks" class="admin-form-label">Pass Marks</label>
                        <input type="number" id="pass_marks" name="pass_marks" class="admin-form-input" min="1"
                               value="<?= htmlspecialchars($editExam['pass_marks'] ?? '40', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-group">
                    <label class="admin-form-label">Options</label>
                    <label class="admin-toggle">
                        <input type="checkbox" name="is_published" value="1" <?= ($editExam['is_published'] ?? 0) ? 'checked' : '' ?>>
                        <span class="admin-toggle-slider"></span>
                        <span class="admin-toggle-label">Published</span>
                    </label>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary"><?= $editExam ? 'Update Exam' : 'Create Exam' ?></button>
                    <a href="<?= $basePath ?>/admin/exams" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>

    <?php else: ?>
    <!-- Exams List -->
    <div class="admin-section-header">
        <a href="<?= $basePath ?>/admin/exams/create" class="admin-btn admin-btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add New Exam
        </a>
    </div>

    <?php if (!empty($exams)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Title</th>
                            <th>Course</th>
                            <th>Duration</th>
                            <th>Total Marks</th>
                            <th>Published</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($exams as $exam): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($exam['title'], ENT_QUOTES, 'UTF-8') ?></strong></td>
                            <td><?= htmlspecialchars($exam['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                            <td><?= (int) $exam['duration_minutes'] ?> min</td>
                            <td><?= (int) $exam['total_marks'] ?></td>
                            <td>
                                <?php if ($exam['is_published']): ?>
                                <span class="admin-badge admin-badge-success">Yes</span>
                                <?php else: ?>
                                <span class="admin-badge admin-badge-warning">Draft</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <a href="<?= $basePath ?>/admin/exams/<?= (int) $exam['id'] ?>/questions" class="admin-btn-icon" title="Manage Questions">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>
                                    </a>
                                    <a href="<?= $basePath ?>/admin/exams/<?= (int) $exam['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="<?= $basePath ?>/admin/exams/<?= (int) $exam['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 else: ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <p class="admin-empty-text">No exams found. <a href="<?= $basePath ?>/admin/exams/create">Create your first exam</a>.</p>
        </div>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>
