<?php
/**
 * Branch Admin — Exam Form View (Create / Edit)
 *
 * Shared form for creating and editing exams.
 * When $exam is set, the form is in edit mode; otherwise create mode.
 * Fields: Title, Course, Duration, Total Marks, Pass Marks, Instructions, Published.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$isEdit = !empty($exam);
$formAction = $isEdit
    ? $bp . '/branch/exams/' . (int) ($exam['id'] ?? 0)
    : $bp . '/branch/exams';
?>

<div class="admin-card">
    <div class="admin-card-header">
        <h2 class="admin-card-title"><?= $isEdit ? 'Edit Exam' : 'Create New Exam' ?></h2>
        <a href="<?= $bp ?>/branch/exams" class="admin-btn admin-btn-secondary">&larr; Back to Exams</a>
    </div>
    <div class="admin-card-body">
        <form method="POST" action="<?= htmlspecialchars($formAction, ENT_QUOTES, 'UTF-8') ?>" class="admin-form" id="examForm">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

            <div class="admin-form-group">
                <label class="admin-form-label" for="title">Exam Title <span style="color:#e74c3c;">*</span></label>
                <input type="text" name="title" id="title" class="admin-form-input" required
                       value="<?= htmlspecialchars($exam['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
                       placeholder="e.g., Mid-Term Examination 2025">
            </div>

            <div class="admin-form-row" style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div class="admin-form-group">
                    <label class="admin-form-label" for="course_id">Course <span style="color:#e74c3c;">*</span></label>
                    <select name="course_id" id="course_id" class="admin-form-input" required>
                        <option value="">-- Select Course --</option>
                        <?php if (!empty($courses) && is_array($courses)): ?>
                            <?php foreach ($courses as $course): ?>
                            <option value="<?= (int) ($course['id'] ?? 0) ?>"
                                <?= ((int) ($exam['course_id'] ?? 0) === (int) ($course['id'] ?? 0)) ? 'selected' : '' ?>>
                                <?= htmlspecialchars($course['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                            </option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label" for="duration_minutes">Duration (minutes) <span style="color:#e74c3c;">*</span></label>
                    <input type="number" name="duration_minutes" id="duration_minutes" class="admin-form-input"
                           required min="1"
                           value="<?= htmlspecialchars($exam['duration_minutes'] ?? '60', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="e.g., 60">
                </div>
            </div>

            <div class="admin-form-row" style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                <div class="admin-form-group">
                    <label class="admin-form-label" for="total_marks">Total Marks <span style="color:#e74c3c;">*</span></label>
                    <input type="number" name="total_marks" id="total_marks" class="admin-form-input"
                           required min="1" step="0.5"
                           value="<?= htmlspecialchars($exam['total_marks'] ?? '100', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="e.g., 100">
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label" for="pass_marks">Pass Marks</label>
                    <input type="number" name="pass_marks" id="pass_marks" class="admin-form-input"
                           min="0" step="0.5"
                           value="<?= htmlspecialchars($exam['pass_marks'] ?? '40', ENT_QUOTES, 'UTF-8') ?>"
                           placeholder="e.g., 40">
                </div>
            </div>

            <div class="admin-form-group">
                <label class="admin-form-label" for="instructions">Instructions</label>
                <textarea name="instructions" id="instructions" class="admin-form-input" rows="5"
                          placeholder="Enter exam instructions for students..."><?= htmlspecialchars($exam['instructions'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>

            <div class="admin-form-group">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;">
                    <input type="checkbox" name="is_published" id="is_published" value="1"
                           <?= !empty($exam['is_published']) ? 'checked' : '' ?>>
                    <span class="admin-form-label" style="margin:0;">Publish Exam</span>
                </label>
                <small style="color:#888;margin-left:28px;">Students will only see published exams.</small>
            </div>

            <div style="margin-top:20px;display:flex;gap:12px;">
                <button type="submit" class="admin-btn admin-btn-primary">
                    <?= $isEdit ? 'Update Exam' : 'Create Exam' ?>
                </button>
                <a href="<?= $bp ?>/branch/exams" class="admin-btn admin-btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
