<?php
include '../../includes/conn.php';
include '../includes/header.php';
requireRole('teacher');

if (!isset($_GET['question_id'])) {
    die("Question ID not provided.");
}

$question_id = $_GET['question_id'];
$message = '';
$message_type = '';

// Fetch the question
$stmt = $pdo->prepare("SELECT * FROM cbt_questions WHERE id = ?");
$stmt->execute([$question_id]);
$question = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$question) {
    die("Question not found.");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $question_text = $_POST['question_text'];
    $option_a = $_POST['option_a'];
    $option_b = $_POST['option_b'];
    $option_c = $_POST['option_c'];
    $option_d = $_POST['option_d'];
    $correct_option = $_POST['correct_option'];

    $update_stmt = $pdo->prepare("
        UPDATE cbt_questions 
        SET question_text = ?, option_a = ?, option_b = ?, option_c = ?, option_d = ?, correct_option = ?
        WHERE id = ?
    ");

    if ($update_stmt->execute([$question_text, $option_a, $option_b, $option_c, $option_d, $correct_option, $question_id])) {
        $message = "Question updated successfully!";
        $message_type = "success";
    } else {
        $message = "Error updating question.";
        $message_type = "danger";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Question</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white">
            <h4>Edit Question</h4>
        </div>
        <div class="card-body">
            <?php if ($message): ?>
                <div class="alert alert-<?= $message_type ?>"><?= $message ?></div>
            <?php endif; ?>

            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Question</label>
                    <textarea name="question_text" class="form-control" required><?= htmlspecialchars($question['question_text']) ?></textarea>
                </div>

                <?php foreach (['A','B','C','D'] as $opt): ?>
                    <div class="mb-3">
                        <label class="form-label">Option <?= $opt ?></label>
                        <input type="text" name="option_<?= strtolower($opt) ?>" 
                               value="<?= htmlspecialchars($question['option_'.strtolower($opt)]) ?>" 
                               class="form-control" required>
                    </div>
                <?php endforeach; ?>

                <div class="mb-3">
                    <label class="form-label">Correct Option</label>
                    <select name="correct_option" class="form-select" required>
                        <?php foreach (['A','B','C','D'] as $opt): ?>
                            <option value="<?= $opt ?>" <?= $question['correct_option'] == $opt ? 'selected' : '' ?>><?= $opt ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <button type="submit" class="btn btn-success">Update Question</button>
                <a href="add_questions.php?exam_id=<?= $question['exam_id'] ?>" class="btn btn-secondary">Back</a>
            </form>
        </div>
    </div>
</div>
</body>
</html>
