<?php

declare(strict_types=1);

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

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

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

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

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

$page_title = 'View Results';

/*
|--------------------------------------------------------------------------
| FILTERS
|--------------------------------------------------------------------------
*/

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

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

/*
|--------------------------------------------------------------------------
| FETCH EXAMS
|--------------------------------------------------------------------------
*/

$examStmt = $pdo->prepare("
    SELECT
        id,
        exam_name
    FROM exam_types
    WHERE school_id = ?
    AND status = 'Active'
    ORDER BY display_order ASC,
    exam_name ASC
");

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

$examTypes = $examStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH CLASSES
|--------------------------------------------------------------------------
*/

$classStmt = $pdo->prepare("
    SELECT
        id,
        class_name
    FROM classes
    WHERE school_id = ?
    ORDER BY sort_order ASC,
    class_name ASC
");

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

$classes = $classStmt->fetchAll(PDO::FETCH_ASSOC);

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

$results = [];

if (
    $exam_type_id > 0 &&
    $class_id > 0
) {

    $resultStmt = $pdo->prepare("
        SELECT

            er.id,
            er.percentage,
            er.overall_grade,
            er.rank_position,
            er.result_status,
            er.is_published,

            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
    ");

    $resultStmt->execute([

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

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

require_once __DIR__ . '/../include/header.php';

require_once __DIR__ . '/../include/sidebar.php';
?>

<div class="content-wrapper">

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-2">

                <div class="col-sm-6">

                    <h1>

                        View Results

                    </h1>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <?php include __DIR__ . '/partials/result-filter.php'; ?>

            <?php if (!empty($results)): ?>

                <?php include __DIR__ . '/partials/result-table.php'; ?>

            <?php endif; ?>

        </div>

    </section>

</div>

<link
    rel="stylesheet"
    href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap4.min.css"
>

<link
    rel="stylesheet"
    href="https://cdn.datatables.net/responsive/2.5.0/css/responsive.bootstrap4.min.css"
>

<script src="https://cdn.datatables.net/1.13.8/js/jquery.dataTables.min.js"></script>

<script src="https://cdn.datatables.net/1.13.8/js/dataTables.bootstrap4.min.js"></script>

<script src="https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js"></script>

<script src="https://cdn.datatables.net/responsive/2.5.0/js/responsive.bootstrap4.min.js"></script>

<script src="assets/js/result-table.js"></script>

<?php require_once __DIR__ . '/../include/footer.php'; ?>