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

$teacher_id = $_SESSION['user_id'];

// Ensure exam_id is provided
if (!isset($_GET['exam_id'])) {
    die("Exam ID not provided.");
}

$exam_id = intval($_GET['exam_id']);

// Verify that the exam belongs to the logged-in teacher
$exam_stmt = $pdo->prepare("SELECT * FROM cbt_exams WHERE id = ? AND teacher_id = ?");
$exam_stmt->execute([$exam_id, $teacher_id]);
$exam = $exam_stmt->fetch(PDO::FETCH_ASSOC);

if (!$exam) {
    die("Invalid exam or access denied.");
}

// Handle question submission
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'];

    $stmt = $pdo->prepare("INSERT INTO cbt_questions (exam_id, question_text, option_a, option_b, option_c, option_d, correct_option)
                           VALUES (?, ?, ?, ?, ?, ?, ?)");
    $stmt->execute([$exam_id, $question_text, $option_a, $option_b, $option_c, $option_d, $correct_option]);

    $success = "Question added successfully!";
}

// Fetch all questions for this exam
$questions_stmt = $pdo->prepare("SELECT id, question_text, option_a, option_b, option_c, option_d, correct_option FROM cbt_questions WHERE exam_id = ?");
$questions_stmt->execute([$exam_id]);
$questions = $questions_stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Questions - <?= htmlspecialchars($exam['title']) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container py-5">
    <div class="mb-4 d-flex justify-content-between align-items-center">
        <h3 class="text-primary">Add Questions to: <?= htmlspecialchars($exam['title']) ?></h3>
        <a href="manage_exams.php" class="btn btn-secondary">← Back to Exams</a>
    </div>

    <?php if (!empty($success)): ?>
        <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>

    <!-- Question Form -->
    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Question Text</label>
                    <textarea name="question_text" class="form-control" rows="3" placeholder="Enter your question..." required></textarea>
                </div>
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">Option A</label>
                        <input type="text" name="option_a" class="form-control" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Option B</label>
                        <input type="text" name="option_b" class="form-control" required>
                    </div>
                </div>
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">Option C</label>
                        <input type="text" name="option_c" class="form-control" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">Option D</label>
                        <input type="text" name="option_d" class="form-control" required>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label">Correct Option</label>
                    <select name="correct_option" class="form-select" required>
                        <option value="">-- Select Correct Option --</option>
                        <option value="A">A</option>
                        <option value="B">B</option>
                        <option value="C">C</option>
                        <option value="D">D</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">Add Question</button>
            </form>
        </div>
    </div>

    <!-- Questions List -->
        <div class="mt-4">
            <h5>Questions for this Exam</h5>

            <?php if (count($questions) > 0): ?>
                <table class="table table-bordered table-striped align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>#</th>
                            <th>Question</th>
                            <th>Option A</th>
                            <th>Option B</th>
                            <th>Option C</th>
                            <th>Option D</th>
                            <th>Correct</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($questions as $index => $q): ?>
                            <tr>
                                <td><?= $index + 1 ?></td>
                                <td><?= htmlspecialchars($q['question_text']) ?></td>
                                <td><?= htmlspecialchars($q['option_a']) ?></td>
                                <td><?= htmlspecialchars($q['option_b']) ?></td>
                                <td><?= htmlspecialchars($q['option_c']) ?></td>
                                <td><?= htmlspecialchars($q['option_d']) ?></td>
                                <td><span class="badge bg-success"><?= $q['correct_option'] ?></span></td>
                                <td>
                                    <a href="edit_questions.php?question_id=<?= $q['id'] ?>" class="btn btn-sm btn-primary">
                                        Edit
                                    </a>
                                    <a href="delete_questions.php?question_id=<?= $q['id'] ?>" 
                                    class="btn btn-sm btn-danger"
                                    onclick="return confirm('Are you sure you want to delete this question?');">
                                        Delete
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="alert alert-info">No questions added yet.</div>
            <?php endif; ?>
        </div>

</div>
</body>
</html>
