<?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 SCHOOL
|--------------------------------------------------------------------------
*/

$schoolStmt = $pdo->prepare("
    SELECT

        school_name,
        school_logo,
        school_address,
        school_phone

    FROM school_settings

    WHERE school_id = ?

    LIMIT 1
");

$schoolStmt->execute([$school_id]);

$school = $schoolStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH FINAL RESULTS
|--------------------------------------------------------------------------
*/

$resultStmt = $pdo->prepare("
    SELECT

        er.id AS result_id,

        er.total_marks,
        er.obtained_marks,
        er.percentage,
        er.overall_grade,
        er.result_status,
        er.rank_position,

        s.id AS student_id,
        s.name,
        s.photo,

        sp.father_name,

        ss.roll_no,

        c.class_name,

        et.exam_name

    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

    LEFT JOIN student_parents sp
        ON sp.student_id = s.id
        AND sp.school_id = s.school_id
        AND sp.session_id = er.session_id

    INNER JOIN classes c
        ON c.id = er.class_id
        AND c.school_id = er.school_id

    INNER JOIN exam_types et
        ON et.id = er.exam_type_id
        AND et.school_id = er.school_id

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

    ORDER BY
        er.rank_position ASC
");

$resultStmt->execute([

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

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

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>

        Bulk Final Marksheet Print

    </title>

    <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css"
    >

    <link
        rel="stylesheet"
        href="assets/css/marksheet.css"
    >

    <link
        rel="stylesheet"
        href="assets/css/print.css"
    >

</head>

<body onload="window.print()">

<div class="container-fluid mt-3">

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

        <?php

        include __DIR__ . '/templates/final-marksheet-template.php';

        ?>

        <div class="page-break"></div>

    <?php endforeach; ?>

</div>

</body>

</html>