<?php
require_once '../includes/init.php';

$user = new User($pdo);
if (!$user->isLoggedIn() || !$user->isTeacher()) {
    header("Location: ../index.php");
    exit;
}

$exam_id = isset($_GET['exam_id']) ? $_GET['exam_id'] : '';
$results = [];
$exam_details = null;

// Fetch Exams for Dropdown
$exams = $pdo->query("SELECT exam_id, title, session, term FROM exams ORDER BY exam_id DESC")->fetchAll();

if ($exam_id) {
    $stmt = $pdo->prepare("SELECT * FROM exams WHERE exam_id = ?");
    $stmt->execute([$exam_id]);
    $exam_details = $stmt->fetch();

    $stmt = $pdo->prepare("
        SELECT r.*, u.username 
        FROM results r 
        JOIN users u ON r.user_id = u.user_id 
        WHERE r.exam_id = ? 
        ORDER BY r.score DESC
    ");
    $stmt->execute([$exam_id]);
    $results = $stmt->fetchAll();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Result Broadsheet</title>
    <link rel="stylesheet" href="../assets/css/admin.css">
</head>
<body>
    <?php include '../includes/admin_sidebar.php'; ?>
    <div class="content">
        <?php include '../includes/page_header.php'; ?>
        <h1>Result Broadsheet</h1>

        <div class="card">
            <form method="GET">
                <select name="exam_id" onchange="this.form.submit()">
                    <option value="">Select Exam to View Broadsheet</option>
                    <?php foreach ($exams as $e): ?>
                        <option value="<?php echo $e['exam_id']; ?>" <?php if($exam_id == $e['exam_id']) echo 'selected'; ?>>
                            <?php echo htmlspecialchars($e['title']); ?> (<?php echo $e['session'] . ' ' . $e['term']; ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </form>
        </div>

        <?php if ($exam_id && $exam_details): ?>
            <div style="margin-bottom: 20px;">
                <h3>Results for: <?php echo htmlspecialchars($exam_details['title']); ?></h3>
                <a href="export_pdf.php?exam_id=<?php echo $exam_id; ?>" class="btn" target="_blank">Export to PDF</a>
            </div>

            <table>
                <thead>
                    <tr>
                        <th>S/N</th>
                        <th>Student Name</th>
                        <th>Score Obtained</th>
                        <th>Total Questions</th>
                        <th>Percentage</th>
                        <th>Date Taken</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($results) > 0): ?>
                        <?php $sn = 1; foreach ($results as $r): ?>
                        <tr>
                            <td><?php echo $sn++; ?></td>
                            <td><?php echo htmlspecialchars($r['username']); ?></td>
                            <td><?php echo $r['score']; ?></td>
                            <td><?php echo $r['total_questions']; ?></td>
                            <td>
                                <?php 
                                    $percent = ($r['score'] / $r['total_questions']) * 100;
                                    echo number_format($percent, 2) . '%';
                                ?>
                            </td>
                            <td><?php echo $r['date_taken']; ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="6">No results found for this exam.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</body>
</html>
