<?php $bp = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<div class="admin-page-header">
    <h2>Edit Exam</h2>
    <a href="<?= $bp ?>/branch/exams" class="admin-btn admin-btn-outline">&larr; Back to Exams</a>
</div>

<div class="admin-card">
    <div class="admin-card-body">
        <form method="POST" action="<?= $bp ?>/branch/exams/<?= (int)($exam['id'] ?? 0) ?>/update" 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"
                           placeholder="Enter exam title" required
                           value="<?= htmlspecialchars($exam['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 a course</option>
                        <?php foreach ($courses ?? [] as $c): ?>
                        <option value="<?= (int)($c['id'] ?? 0) ?>"
                                <?= ((string)($exam['course_id'] ?? '') == (string)($c['id'] ?? '')) ? 'selected' : '' ?>>
                            <?= htmlspecialchars($c['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) <span class="admin-required">*</span></label>
                    <input type="number" id="duration_minutes" name="duration_minutes" class="admin-form-input"
                           placeholder="e.g. 60" required min="1"
                           value="<?= htmlspecialchars($exam['duration_minutes'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="admin-form-group">
                    <label for="total_marks" class="admin-form-label">Total Marks <span class="admin-required">*</span></label>
                    <input type="number" id="total_marks" name="total_marks" class="admin-form-input"
                           placeholder="e.g. 100" required min="1"
                           value="<?= htmlspecialchars($exam['total_marks'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
            </div>

            <div class="admin-form-row">
                <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"
                           placeholder="e.g. 40" min="0"
                           value="<?= htmlspecialchars($exam['pass_marks'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="admin-form-group">
                    <label for="is_published" class="admin-form-label">Published</label>
                    <div class="admin-form-checkbox-wrap">
                        <input type="checkbox" id="is_published" name="is_published" value="1"
                               <?= !empty($exam['is_published']) ? 'checked' : '' ?>>
                        <label for="is_published" class="admin-form-checkbox-label">
                            <?= !empty($exam['is_published']) ? 'Published' : 'Draft' ?>
                        </label>
                    </div>
                </div>
            </div>

            <div class="admin-form-row">
                <div class="admin-form-group">
                    <label for="start_time" class="admin-form-label">Start Time</label>
                    <?php
                        $startVal = '';
                        if (!empty($exam['start_time']) && $exam['start_time'] !== '0000-00-00 00:00:00') {
                            $startVal = date('Y-m-d\TH:i', strtotime($exam['start_time']));
                        }
                    ?>
                    <input type="datetime-local" id="start_time" name="start_time" class="admin-form-input"
                           value="<?= htmlspecialchars($startVal, ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="admin-form-group">
                    <label for="end_time" class="admin-form-label">End Time</label>
                    <?php
                        $endVal = '';
                        if (!empty($exam['end_time']) && $exam['end_time'] !== '0000-00-00 00:00:00') {
                            $endVal = date('Y-m-d\TH:i', strtotime($exam['end_time']));
                        }
                    ?>
                    <input type="datetime-local" id="end_time" name="end_time" class="admin-form-input"
                           value="<?= htmlspecialchars($endVal, ENT_QUOTES, 'UTF-8') ?>">
                </div>
            </div>

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

            <div class="admin-form-actions">
                <button type="submit" class="admin-btn admin-btn-primary">Update Exam</button>
                <a href="<?= $bp ?>/branch/exams" class="admin-btn admin-btn-outline">Cancel</a>
            </div>
        </form>
    </div>
</div>
