<?php

declare(strict_types=1);

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

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

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

$page_title = 'Exam Schedule';

/*
|--------------------------------------------------------------------------
| CSRF TOKEN
|--------------------------------------------------------------------------
*/

if (empty($_SESSION['csrf_token'])) {

    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

/*
|--------------------------------------------------------------------------
| FETCH ACTIVE SESSION
|--------------------------------------------------------------------------
*/

$sessionStmt = $pdo->prepare("
    SELECT
        id,
        session_name
    FROM academic_sessions
    WHERE id = ?
    AND school_id = ?
    LIMIT 1
");

$sessionStmt->execute([
    $session_id,
    $school_id
]);

$current_session = $sessionStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH EXAM TYPES
|--------------------------------------------------------------------------
*/

$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]);

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

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

$classStmt = $pdo->prepare("
    SELECT
        id,
        class_name,
        section
    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>

                        Exam Schedule Setup

                    </h1>

                </div>

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

                    <span class="badge badge-primary p-2">

                        Session :
                        <?= htmlspecialchars($current_session['session_name'] ?? 'N/A'); ?>

                    </span>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <?php if (isset($_SESSION['success_message'])): ?>

                <div class="alert alert-success alert-dismissible fade show">

                    <?= htmlspecialchars($_SESSION['success_message']); ?>

                    <button
                        type="button"
                        class="close"
                        data-dismiss="alert"
                    >
                        <span>&times;</span>
                    </button>

                </div>

                <?php unset($_SESSION['success_message']); ?>

            <?php endif; ?>

            <?php if (isset($_SESSION['error_message'])): ?>

                <div class="alert alert-danger alert-dismissible fade show">

                    <?= htmlspecialchars($_SESSION['error_message']); ?>

                    <button
                        type="button"
                        class="close"
                        data-dismiss="alert"
                    >
                        <span>&times;</span>
                    </button>

                </div>

                <?php unset($_SESSION['error_message']); ?>

            <?php endif; ?>

            <!-- FILTER CARD -->

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

                <div class="card-header">

                    <h3 class="card-title">

                        Load Schedule

                    </h3>

                </div>

                <div class="card-body">

                    <div class="row">

                        <div class="col-md-5">

                            <div class="form-group">

                                <label>
                                    Exam Type
                                </label>

                                <select
                                    id="exam_type_id"
                                    class="form-control"
                                >

                                    <option value="">
                                        Select Exam Type
                                    </option>

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

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

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

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                        </div>

                        <div class="col-md-5">

                            <div class="form-group">

                                <label>
                                    Class
                                </label>

                                <select
                                    id="class_id"
                                    class="form-control"
                                >

                                    <option value="">
                                        Select Class
                                    </option>

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

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

                                            <?= htmlspecialchars(
                                                $class['class_name'] .
                                                (!empty($class['section'])
                                                    ? ' - ' . $class['section']
                                                    : '')
                                            ); ?>

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                        </div>

                        <div class="col-md-2 d-flex align-items-end">

                            <button
                                type="button"
                                class="btn btn-primary btn-block"
                                id="loadSubjectsBtn"
                            >

                                <i class="fas fa-search"></i>

                                Load

                            </button>

                        </div>

                    </div>

                </div>

            </div>

            <!-- SCHEDULE FORM -->

            <form
                action="process/save_exam_schedule.php"
                method="POST"
                id="scheduleForm"
            >

                <input
                    type="hidden"
                    name="csrf_token"
                    value="<?= htmlspecialchars($_SESSION['csrf_token']); ?>"
                >

                <input
                    type="hidden"
                    name="exam_type_id"
                    id="hidden_exam_type_id"
                >

                <input
                    type="hidden"
                    name="class_id"
                    id="hidden_class_id"
                >

                <div class="card card-outline card-info shadow-sm">

                    <div class="card-header">

                        <h3 class="card-title">

                            Subject Schedule

                        </h3>

                    </div>

                    <div class="card-body p-0">

                        <div id="subjectsContainer">

                            <div class="text-center p-5 text-muted">

                                <i class="fas fa-calendar-alt fa-3x mb-3"></i>

                                <p class="mb-0">

                                    Select exam type and class to load subjects.

                                </p>

                            </div>

                        </div>

                    </div>

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

                        <button
                            type="submit"
                            class="btn btn-success"
                            id="saveScheduleBtn"
                            disabled
                        >

                            <i class="fas fa-save"></i>

                            Save Schedule

                        </button>

                    </div>

                </div>

            </form>

        </div>

    </section>

</div>

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

<!-- Toastr CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

<script src="/schools/exams/assets/js/exam-schedule.js"></script>