<?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 Admit Card';

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

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

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

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

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

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

$currentSession = $sessionStmt->fetch(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);

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

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 Admit Card

                    </h1>

                </div>

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

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

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

                    </span>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <!-- FILTER CARD -->

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

                <div class="card-header">

                    <h3 class="card-title">

                        Load Students

                    </h3>

                </div>

                <div class="card-body">

                    <div class="row">

                        <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-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-2 d-flex align-items-end">

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

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

                                Load

                            </button>

                        </div>

                    </div>

                </div>

            </div>

            <!-- STUDENT LIST -->

            <form
                action="exam_print_admit_card.php"
                method="POST"
                target="_blank"
                id="admitCardForm"
            >

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

                <input
                    type="hidden"
                    name="exam_id"
                    id="hidden_exam_id"
                >

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

                    <div class="card-header d-flex justify-content-between align-items-center">

                        <h3 class="card-title">

                            Student List

                        </h3>

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

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

                            Generate & Print

                        </button>

                    </div>

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

                        <div id="studentsContainer">

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

                                <i class="fas fa-id-card fa-3x mb-3"></i>

                                <p class="mb-0">

                                    Select class and exam type to load students.

                                </p>

                            </div>

                        </div>

                    </div>

                </div>

            </form>

        </div>

    </section>

</div>

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

<script src="assets/js/exam-admit-card.js"></script>
<!-- Bootstrap 4 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">

<!-- FontAwesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">

<!-- Toastr -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

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

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

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

<!-- Custom JS -->
<script src="/schools/exams/assets/js/exam-admit-card.js"></script>