<?php

declare(strict_types=1);

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

allow_roles(['admin']);

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

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

$page_title = 'Generate Results';

/*
|--------------------------------------------------------------------------
| 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);

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>

                        Generate Results

                    </h1>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <div class="card card-outline card-primary">

                <div class="card-header">

                    <h3 class="card-title">

                        Generate Student Results

                    </h3>

                </div>

                <form
                    id="generateResultForm"
                    method="POST"
                >

                    <?= csrfField(); ?>

                    <div class="card-body">

                        <div class="row">

                            <!-- EXAM -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Exam Type

                                </label>

                                <select
                                    name="exam_type_id"
                                    class="form-control select2"
                                    required
                                >

                                    <option value="">

                                        Select Exam

                                    </option>

                                    <?php foreach ($examTypes as $exam): ?>

                                        <option
                                            value="<?= (int) $exam['id']; ?>"
                                        >

                                            <?= htmlspecialchars($exam['exam_name']); ?>

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                            <!-- CLASS -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Class

                                </label>

                                <select
                                    name="class_id"
                                    class="form-control select2"
                                    required
                                >

                                    <option value="">

                                        Select Class

                                    </option>

                                    <?php foreach ($classes as $class): ?>

                                        <option
                                            value="<?= (int) $class['id']; ?>"
                                        >

                                            <?= htmlspecialchars($class['class_name']); ?>

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                            <!-- SESSION -->

                            <div class="col-md-4 mb-3">

                                <label>

                                    Session

                                </label>

                                <input
                                    type="text"
                                    class="form-control"
                                    value="<?= htmlspecialchars($_SESSION['session_name'] ?? 'N/A'); ?>"
                                    readonly
                                >

                            </div>

                        </div>

                    </div>

                    <div class="card-footer text-right">

                        <button
                            type="submit"
                            class="btn btn-success"
                        >

                            <i class="fas fa-cogs mr-1"></i>

                            Generate Results

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </section>

</div>

<!-- SELECT2 -->

<link
    rel="stylesheet"
    href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css"
>

<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

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

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