<?php
chdir('../');
session_start();
require_once('public/connection.php');

// Check if admin is logged in
if (!isset($_SESSION['admin_id'])) {
    header("Location: ../index.php");
    exit;
}

// Fetch classes for the select dropdown
$stmt = $conn->prepare("SELECT * FROM classes");
$stmt->execute();
$classes = $stmt->get_result();

// Handle class selection
$class_id = $_GET['class_id'] ?? null;

if ($class_id) {
    // Fetch results for students in the selected class
    $stmt = $conn->prepare("
        SELECT e.title AS subject_name, s.id AS student_id, s.firstname, s.lastname, er.mcq_score, er.theory_score, er.total_score, er.status
        FROM students s
        JOIN student_class sc ON sc.student_id = s.id
        JOIN exam_results er ON er.student_id = s.id
        JOIN exams e ON e.id = er.exam_id
        WHERE sc.class_id = ? AND er.status != 'pending'
    ");
    $stmt->bind_param("i", $class_id);
    $stmt->execute();
    $results = $stmt->get_result();
}
?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Admin - View Student Results</h1>
                </div>
            </div>
        </div>
    </div>

    <div class="content">
        <div class="container-fluid">
            <!-- Link to Admin Theory Review Page -->
            <div class="mb-3">
                <a href="admin_theory_review.php" class="btn btn-primary">Go to Admin Theory Review</a>
            </div>

            <!-- Class Selection Form -->
            <form method="GET" class="mb-3">
                <div class="form-group">
                    <label for="class_id">Select Class</label>
                    <select name="class_id" id="class_id" class="form-control" onchange="this.form.submit()">
                        <option value="">Select Class</option>
                        <?php while ($class = $classes->fetch_assoc()): ?>
                            <option value="<?= $class['id'] ?>" <?= ($class['id'] == $class_id) ? 'selected' : '' ?>><?= htmlspecialchars($class['class_name']) ?></option>
                        <?php endwhile; ?>
                    </select>
                </div>
            </form>

            <!-- Display Results for the selected Class -->
            <?php if ($class_id && $results->num_rows > 0): ?>
                <div class="card">
                    <div class="card-header bg-info text-white">
                        Results for Class: <?= htmlspecialchars($class_id) ?>
                    </div>
                    <div class="card-body">
                        <table class="table table-bordered table-hover" id = "myTable">
                            <thead>
                                <tr>
                                    <th>Subject Name</th>
                                    <th>Student Name</th>
                                    <th>MCQ Score</th>
                                    <th>Theory Score</th>
                                    <th>Total Score</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($row = $results->fetch_assoc()): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($row['subject_name']) ?></td>
                                        <td><?= htmlspecialchars($row['firstname'] . ' ' . $row['lastname']) ?></td>
                                        <td><?= $row['mcq_score'] ?></td>
                                        <td><?= $row['theory_score'] ?></td>
                                        <td><?= $row['total_score'] ?? 'Not Calculated' ?></td>
                                        <td><?= $row['status'] ?></td>
                                    </tr>
                                <?php endwhile; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            <?php elseif ($class_id): ?>
                <p class="alert alert-warning">No results available for the selected class.</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include('partials/footer.php'); ?>

