<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'db.php';

$message = "";

// 1. PROCESS DELETION REQUEST
if (isset($_POST['delete_q_id'])) {
    try {
        $stmt = $pdo->prepare("DELETE FROM questions WHERE id = ?");
        $stmt->execute([intval($_POST['delete_q_id'])]);
        $message = "<div class='alert alert-success'>✅ Success: Question removed from the bank successfully!</div>";
    } catch (\PDOException $e) {
        $message = "<div class='alert alert-danger'>❌ Error: Could not delete question. " . htmlspecialchars($e->getMessage()) . "</div>";
    }
}

// 2. FETCH QUESTIONS FILTERED BY COURSE
$course_filter = isset($_GET['course_filter']) ? intval($_GET['course_filter']) : 0;

try {
    $courses = $pdo->query("SELECT * FROM courses ORDER BY course_code ASC")->fetchAll();
    
    if ($course_filter > 0) {
        $q_stmt = $pdo->prepare("SELECT * FROM questions WHERE course_id = ? ORDER BY id DESC");
        $q_stmt->execute([$course_filter]);
    } else {
        $q_stmt = $pdo->query("SELECT * FROM questions ORDER BY id DESC LIMIT 50");
    }
    $questions = $q_stmt->fetchAll();
} catch (\PDOException $e) {
    $courses = []; $questions = [];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Delete Questions</title>
    <style>
        body { font-family: Arial, sans-serif; background: #fff; padding: 15px; color: #333; margin: 0; }
        .card { background: #fdfdfd; padding: 22px; border: 1px solid #dee2e6; border-radius: 6px; margin-bottom: 20px; border-top: 4px solid #dc3545; }
        select { width: 100%; padding: 11px; margin: 8px 0; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; background: white; }
        th, td { border: 1px solid #dee2e6; padding: 11px; text-align: left; font-size: 13px; } th { background: #e9ecef; }
        .btn-del { padding: 6px 12px; background: #dc3545; color: white; border: none; cursor: pointer; border-radius: 4px; font-weight: bold; font-size: 12px; }
        .btn-del:hover { background: #c82333; }
        .alert { padding: 12px; border-radius: 4px; margin-bottom: 18px; font-weight: bold; font-size: 14px; }
        .alert-success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .alert-danger { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
    </style>
</head>
<body>

    <h2 style="color: #333; border-bottom: 1px solid #eee; padding-bottom: 8px; font-size: 20px;">Delete Questions Manager</h2>
    <?php echo $message; ?>

    <div class="card">
        <h4 style="margin-top:0; color:#dc3545; font-size: 15px;">Filter by Course to Manage Questions</h4>
        <form method="GET" action="delete_question.php">
            <select name="course_filter" onchange="this.form.submit()">
                <option value="0">-- Showing All Questions (Max 50) --</option>
                <?php foreach ($courses as $c) { ?>
                    <option value="<?php echo $c['id']; ?>" <?php echo ($course_filter == $c['id']) ? 'selected' : ''; ?>>[<?php echo htmlspecialchars($c['course_code']); ?>] - <?php echo htmlspecialchars($c['course_title']); ?></option>
                <?php } ?>
            </select>
        </form>

        <?php if (empty($questions)) { ?>
            <p style="color:#666; font-style:italic; margin-top:15px;">No questions found in this course selection.</p>
        <?php } else { ?>
            <table>
                <thead>
                    <tr>
                        <th style="width: 50%;">Question Text</th>
                        <th>Options (A, B, C, D)</th>
                        <th>Ans</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($questions as $q) { ?>
                    <tr>
                        <td><strong><?php echo htmlspecialchars($q['question_text']); ?></strong></td>
                        <td>
                            <small>
                                A: <?php echo htmlspecialchars($q['option_a']); ?><br>
                                B: <?php echo htmlspecialchars($q['option_b']); ?><br>
                                C: <?php echo htmlspecialchars($q['option_c']); ?><br>
                                D: <?php echo htmlspecialchars($q['option_d']); ?>
                            </small>
                        </td>
                        <td><strong style="color: #28a745;"><?php echo $q['correct_option']; ?></strong></td>
                        <td>
                            <form method="POST" action="delete_question.php" onsubmit="return confirm('Are you sure you want to permanently delete this question?')">
                                <input type="hidden" name="delete_q_id" value="<?php echo $q['id']; ?>">
                                <button type="submit" class="btn-del">Delete</button>
                            </form>
                        </td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        <?php } ?>
    </div>

</body>
</html>
