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

$teacher_id = $_SESSION['user_id'];

// Fetch exams created by this teacher, including session, term, classroom, and subject names
$stmt = $pdo->prepare("
    SELECT e.*, 
           s.name AS subject_name,
           c.name AS class_name,
           ses.name AS session_name,
           t.name AS term_name
    FROM cbt_exams e
    LEFT JOIN subjects s ON e.subject_id = s.id
    LEFT JOIN classrooms c ON e.classroom_id = c.id
    LEFT JOIN sessions ses ON e.session_id = ses.id
    LEFT JOIN terms t ON e.term_id = t.id
    WHERE e.teacher_id = ?
    ORDER BY e.created_at DESC
");
$stmt->execute([$teacher_id]);
$exams = $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>Manage Exams</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">

    <?php if (isset($_GET['status_updated'])): ?>
        <div class="alert alert-success">Exam status updated successfully!</div>
    <?php endif; ?>

    <div class="d-flex justify-content-between align-items-center mb-4">
        <h3 class="text-primary">Manage Exams</h3>
        <a href="add_exam.php" class="btn btn-success">+ Create New Exam</a>
    </div>

    <?php if (count($exams) === 0): ?>
        <div class="alert alert-info">No exams created yet. <a href="add_exam.php">Create one</a>.</div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table table-striped table-bordered align-middle">
                <thead class="table-primary">
                    <tr>
                        <th>#</th>
                        <th>Title</th>
                        <th>Subject</th>
                        <th>Classroom</th>
                        <th>Session</th>
                        <th>Term</th>
                        <th>Duration</th>
                        <th>Exam Date</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $i = 1; foreach ($exams as $exam): ?>
                        <tr>
                            <td><?= $i++ ?></td>
                            <td><?= htmlspecialchars($exam['title']) ?></td>
                            <td><?= htmlspecialchars($exam['subject_name'] ?? 'N/A') ?></td>
                            <td><?= htmlspecialchars($exam['class_name'] ?? 'N/A') ?></td>
                            <td><?= htmlspecialchars($exam['session_name'] ?? 'N/A') ?></td>
                            <td><?= htmlspecialchars($exam['term_name'] ?? 'N/A') ?></td>
                            <td><?= htmlspecialchars($exam['duration']) ?> min</td>
                            <td><?= htmlspecialchars($exam['exam_date']) ?></td>
                            <td>
                                <?php if ($exam['status'] === 'active'): ?>
                                    <span class="badge bg-success">Active</span>
                                <?php else: ?>
                                    <span class="badge bg-secondary">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="add_questions.php?exam_id=<?= $exam['id'] ?>" class="btn btn-sm btn-success">
                                    Add Questions
                                </a>
                                <a href="toggle_exam.php?exam_id=<?= $exam['id'] ?>" class="btn btn-sm <?= $exam['status'] === 'active' ? 'btn-warning' : 'btn-success' ?>">
                                    <?= $exam['status'] === 'active' ? 'Deactivate' : 'Activate' ?>
                                </a>
                                <a href="delete_exam.php?exam_id=<?= $exam['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this exam?')">Delete</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>

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