<?php
session_start();
require_once 'inc/connect.php';

if (!isset($_SESSION['admin_id'])) {
    header("Location: admin_login.php");
    exit;
}

$selected_exam = $_GET['exam_id'] ?? '';
$feedback = '';

$exams = $mysqli->query("SELECT examination_id, examination_title FROM examination_table WHERE admin_status = 'active'");
$question_data = [];

if ($selected_exam !== '') {
    $q = $mysqli->prepare("SELECT * FROM question_bank WHERE examination_id = ? ORDER BY question_id, content_role DESC");
    $q->bind_param("s", $selected_exam);
    $q->execute();
    $res = $q->get_result();

    while ($row = $res->fetch_assoc()) {
        $question_data[$row['question_id']][] = $row;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Questions by Exam</title>
  <link rel="stylesheet" href="/plugins/fontawesome-free/css/all.min.css">
  <link rel="stylesheet" href="/dist/css/adminlte.min.css">
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <?php include 'inc/admin_nav.php'; ?>

  <div class="content-wrapper">
    <div class="content-header">
      <div class="container-fluid">
        <h1 class="m-0">Questions by Examination</h1>
      </div>
    </div>

    <section class="content">
      <div class="container-fluid">
        <form method="GET" class="form-inline mb-3">
          <label class="mr-2">Select Exam:</label>
          <select name="exam_id" class="form-control mr-2" required>
            <option value="">-- Choose Exam --</option>
            <?php while ($ex = $exams->fetch_assoc()): ?>
              <option value="<?= htmlspecialchars($ex['examination_id']) ?>" <?= ($selected_exam === $ex['examination_id']) ? 'selected' : '' ?>>
                <?= htmlspecialchars($ex['examination_title']) ?>
              </option>
            <?php endwhile; ?>
          </select>
          <button type="submit" class="btn btn-primary">Load</button>
        </form>

        <?php if ($selected_exam && empty($question_data)): ?>
          <div class="alert alert-warning">No questions found for this examination.</div>
        <?php endif; ?>

        <?php foreach ($question_data as $qid => $rows): ?>
          <div class="card mb-3">
            <div class="card-header bg-secondary text-white">Question ID: <?= $qid ?></div>
            <div class="card-body">
              <p><strong>Main Question:</strong><br>
                <?= htmlspecialchars(current(array_filter($rows, fn($r) => $r['content_role'] === 'question'))['content'] ?? '—') ?>
              </p>
              <p><strong>Correct Options:</strong><br>
                <ul>
                  <?php foreach ($rows as $r) if ($r['content_role'] === 'correct') echo '<li>' . htmlspecialchars($r['content']) . '</li>'; ?>
                </ul>
              </p>
              <p><strong>Wrong Options:</strong><br>
                <ul>
                  <?php foreach ($rows as $r) if ($r['content_role'] === 'wrong') echo '<li>' . htmlspecialchars($r['content']) . '</li>'; ?>
                </ul>
              </p>
            </div>
          </div>
        <?php endforeach; ?>
      </div>
    </section>
  </div>
</div>

<script src="/plugins/jquery/jquery.min.js"></script>
<script src="/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="/dist/js/adminlte.min.js"></script>
</body>
</html>
