<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['title'])) {
    $title       = $_POST['title'];
    $class_id    = $_POST['class_id'];
    $subject_id  = $_POST['subject_id'];
    $duration    = $_POST['duration'];
    $exam_date   = $_POST['exam_date'];
    $start_time  = $_POST['start_time'];
    $end_time    = $_POST['end_time'];
    $total_marks = $_POST['total_marks'];

    // ✅ Step 1: Check for duplicate
    $check = $connection->prepare("SELECT id FROM cbt_exams 
                             WHERE class_id = ? AND subject_id = ? AND exam_date = ?");
    $check->bind_param("iis", $class_id, $subject_id, $exam_date);
    $check->execute();
    $check->store_result();

    if ($check->num_rows > 0) {
        $_SESSION['msg'] = "An exam already exists for this class, subject and date.";
        $_SESSION['type'] = "error";
        header("Location: create_exam");
        exit();
    }

    // ✅ Step 2: No duplicate — insert
    $stmt = $connection->prepare("INSERT INTO cbt_exams 
        (title, class_id, subject_id, duration, total_marks, exam_date, start_time, end_time) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
    );
    $stmt->bind_param("siiissss", 
        $title, $class_id, $subject_id, $duration, $total_marks, $exam_date, $start_time, $end_time
    );

    if ($stmt->execute()) {
        $_SESSION['msg'] = "Exam created successfully.";
        $_SESSION['type'] = "success";
    } else {
        $_SESSION['msg'] = "Failed to create exam. Error: " . $stmt->error;
        $_SESSION['type'] = "error";
    }

    header("Location: cbt_create_exam");
    exit();
}
?>


<div class='page-title'>
    <div>
        <h1><i class='icon-graduation icons'></i> Exam Management</h1>
        <p>Create CBT Exams</p>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <form method="POST" action="">
            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label">Exam Title:</label>
                    <input type="text" name="title" class="form-control" required>
                </div>

                <div class="col-md-6">
                    <label class="form-label">Class:</label>
                    <select name="class_id" class="form-control" required>
                        <option value="">-- Select Class --</option>
                        <?php
                        $classes = $connection->query("SELECT id, classes FROM classes ORDER BY classes ASC");
                        while ($row = $classes->fetch_assoc()) {
                            echo "<option value='{$row['id']}'>{$row['classes']}</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="col-md-6">
                    <label class="form-label">Subject:</label>
                    <select name="subject_id" class="form-control" required>
                        <option value="">-- Select Subject --</option>
                        <?php
                        $subjects = $connection->query("SELECT id, subjects FROM school_subjects ORDER BY subjects ASC");
                        while ($row = $subjects->fetch_assoc()) {
                            echo "<option value='{$row['id']}'>{$row['subjects']}</option>";
                        }
                        ?>
                    </select>
                </div>

                <div class="col-md-3">
                    <label class="form-label">Duration (mins):</label>
                    <input type="number" name="duration" class="form-control" required>
                </div>

                <div class="col-md-3">
                    <label class="form-label">Total Marks:</label>
                    <input type="number" name="total_marks" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Exam Date:</label>
                    <input type="date" name="exam_date" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Start Time:</label>
                    <input type="time" name="start_time" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label class="form-label">End Time:</label>
                    <input type="time" name="end_time" class="form-control" required>
                </div>

                <div class="col-md-12 text-end mt-3">
                    <button type="submit" class="btn btn-success"> Create Exam</button>
                </div>
            </div>
        </form>
    </div>
</div>


