<?php
// FILE PATH: C:\xampp\htdocs\Project\Teacher\manage_questions.php
session_start();
require_once "../config/database.php";

// 1. SECURITY CHECK
if (!isset($_SESSION['role']) || !in_array($_SESSION['role'], ['superadmin', 'admin', 'teacher'])) {
    header("Location: ../auth/login.php");
    exit();
}

$exam_id = isset($_GET['exam_id']) ? (int)$_GET['exam_id'] : 0;

// Handle Delete Request
if (isset($_GET['delete_id'])) {
    $delete_id = (int)$_GET['delete_id'];
    $stmt = $pdo->prepare("DELETE FROM questions WHERE id = ?");
    $stmt->execute([$delete_id]);
    header("Location: manage_questions.php?exam_id=$exam_id&msg=deleted");
    exit();
}

// Fetch exams for the filter dropdown
$exams = $pdo->query("SELECT id, exam_title FROM exams WHERE is_deleted = 0 ORDER BY id DESC")->fetchAll();

// Fetch questions for the selected exam
$questions = [];
if ($exam_id > 0) {
    $stmt = $pdo->prepare("SELECT * FROM questions WHERE exam_id = ? ORDER BY id ASC");
    $stmt->execute([$exam_id]);
    $questions = $stmt->fetchAll();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Manage Questions | Teacher Portal</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #2563eb; --danger: #ef4444; --bg: #f8fafc; }
        body { font-family: 'Inter', sans-serif; background: var(--bg); margin: 0; padding: 20px; }
        .container { max-width: 1100px; margin: auto; }
        .card { background: white; padding: 25px; border-radius: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        
        .filter-section { background: #fff; padding: 20px; border-radius: 10px; margin-bottom: 20px; border: 1px solid #e2e8f0; display: flex; gap: 15px; align-items: flex-end; }
        select { padding: 10px; border-radius: 6px; border: 1px solid #cbd5e1; width: 300px; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th { text-align: left; background: #f1f5f9; padding: 12px; color: #475569; font-size: 13px; text-transform: uppercase; }
        td { padding: 15px; border-bottom: 1px solid #f1f5f9; vertical-align: top; font-size: 14px; }
        
        .opt-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; margin-top: 8px; font-size: 12px; color: #64748b; }
        .correct { color: #16a34a; font-weight: bold; }
        
        .btn-del { color: var(--danger); background: #fef2f2; border: 1px solid #fee2e2; padding: 5px 10px; border-radius: 4px; cursor: pointer; text-decoration: none; font-size: 12px; }
        .btn-del:hover { background: var(--danger); color: white; }
        .badge { background: #dbeafe; color: #1e40af; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: bold; }
    </style>
</head>
<body>

<div class="container">
    <div class="header">
        <h2><i class="fa fa-list-check"></i> Manage Questions</h2>
        <div>
            <a href="upload_questions_bulk.php" style="text-decoration:none; background:var(--primary); color:white; padding:10px 15px; border-radius:8px; font-size:14px;">
                <i class="fa fa-plus"></i> Bulk Add
            </a>
        </div>
    </div>

    <form method="GET" class="filter-section">
        <div>
            <label style="display:block; font-size:12px; font-weight:bold; margin-bottom:5px;">Select Exam to View</label>
            <select name="exam_id" onchange="this.form.submit()">
                <option value="">-- Choose Exam --</option>
                <?php foreach($exams as $e): ?>
                    <option value="<?= $e['id'] ?>" <?= $exam_id == $e['id'] ? 'selected' : '' ?>>
                        <?= htmlspecialchars($e['exam_title']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
    </form>

    <div class="card">
        <?php if($exam_id == 0): ?>
            <div style="text-align:center; padding:40px; color:#94a3b8;">
                <i class="fa fa-arrow-up" style="font-size:30px; margin-bottom:10px;"></i>
                <p>Please select an exam from the dropdown to see its questions.</p>
            </div>
        <?php elseif(empty($questions)): ?>
            <p style="text-align:center; color:#64748b;">No questions found for this exam yet.</p>
        <?php else: ?>
            <table>
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Question Detail</th>
                        <th width="100">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($questions as $index => $q): ?>
                    <tr>
                        <td><?= $index + 1 ?></td>
                        <td>
                            <strong><?= htmlspecialchars($q['question_text']) ?></strong>
                            <div class="opt-grid">
                                <span>A: <?= htmlspecialchars($q['option_a']) ?></span>
                                <span>B: <?= htmlspecialchars($q['option_b']) ?></span>
                                <span>C: <?= htmlspecialchars($q['option_c']) ?></span>
                                <span>D: <?= htmlspecialchars($q['option_d']) ?></span>
                            </div>
                            <div style="margin-top:5px;">
                                <span class="badge">Correct: <?= $q['correct_option'] ?></span>
                            </div>
                        </td>
                        <td>
                            <a href="manage_questions.php?exam_id=<?= $exam_id ?>&delete_id=<?= $q['id'] ?>" 
                               class="btn-del" 
                               onclick="return confirm('Are you sure you want to delete this question?')">
                                <i class="fa fa-trash"></i> Delete
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>

</body>
</html>