<?php

declare(strict_types=1);

require_once __DIR__ . '/../library/auth.php';

require_once __DIR__ . '/helpers/result-helper.php';

allow_roles(['admin', 'teacher']);

requirePostRequest();

verifyCsrfToken();

$school_id = (int) $_SESSION['school_id'];

$session_id = (int) ($_SESSION['session_id'] ?? 0);

/*
|--------------------------------------------------------------------------
| VALIDATE INPUTS
|--------------------------------------------------------------------------
*/

$exam_type_id = validateId(
    $_POST['exam_type_id'] ?? 0
);

$class_id = validateId(
    $_POST['class_id'] ?? 0
);

if (
    $exam_type_id <= 0 ||
    $class_id <= 0
) {

    exit('Invalid request!');
}

/*
|--------------------------------------------------------------------------
| FETCH MERIT LIST
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        er.rank_position,
        er.percentage,
        er.overall_grade,
        er.result_status,

        s.name,

        ss.roll_no

    FROM exam_results er

    INNER JOIN students s
        ON s.id = er.student_id
        AND s.school_id = er.school_id

    INNER JOIN student_sessions ss
        ON ss.student_id = er.student_id
        AND ss.school_id = er.school_id
        AND ss.session_id = er.session_id
        AND ss.class_id = er.class_id

    WHERE er.school_id = ?
    AND er.session_id = ?
    AND er.exam_type_id = ?
    AND er.class_id = ?

    ORDER BY
        er.rank_position ASC,
        er.percentage DESC
");

$stmt->execute([

    $school_id,
    $session_id,
    $exam_type_id,
    $class_id
]);

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FILE NAME
|--------------------------------------------------------------------------
*/

$fileName = 'merit-list-' . date('Y-m-d') . '.xls';

/*
|--------------------------------------------------------------------------
| HEADERS
|--------------------------------------------------------------------------
*/

header('Content-Type: application/vnd.ms-excel');

header(
    'Content-Disposition: attachment; filename="' . $fileName . '"'
);

header('Pragma: no-cache');

header('Expires: 0');

?>

<table border="1">

    <thead>

        <tr>

            <th>

                Rank

            </th>

            <th>

                Roll No

            </th>

            <th>

                Student Name

            </th>

            <th>

                Percentage

            </th>

            <th>

                Grade

            </th>

            <th>

                Status

            </th>

        </tr>

    </thead>

    <tbody>

        <?php foreach ($results as $row): ?>

            <tr>

                <td>

                    <?= (int) $row['rank_position']; ?>

                </td>

                <td>

                    <?= htmlspecialchars($row['roll_no']); ?>

                </td>

                <td>

                    <?= htmlspecialchars($row['name']); ?>

                </td>

                <td>

                    <?= htmlspecialchars($row['percentage']); ?>%

                </td>

                <td>

                    <?= htmlspecialchars($row['overall_grade']); ?>

                </td>

                <td>

                    <?= htmlspecialchars($row['result_status']); ?>

                </td>

            </tr>

        <?php endforeach; ?>

    </tbody>

</table>