<?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 = 'View Exam Schedules';

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

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

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

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

$schedules = [];

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

    $scheduleStmt = $pdo->prepare("
        SELECT

            es.exam_date,
            es.max_marks,
            es.no_exam,

            et.exam_name,

            c.class_name,
            c.section,

            s.name AS subject_name,
            s.subject_code

        FROM exam_schedule es

        INNER JOIN exam_types et
            ON et.id = es.exam_type_id

        INNER JOIN classes c
            ON c.id = es.class_id

        INNER JOIN subjects s
            ON s.id = es.subject_id

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

        ORDER BY es.exam_date ASC, s.name ASC
    ");

    $scheduleStmt->execute([

        $school_id,
        $session_id,
        $exam_type_id,
        $class_id

    ]);

    $schedules = $scheduleStmt->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 List

                    </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">

                        Filter Schedule

                    </h3>

                </div>

                <div class="card-body">

                    <form method="GET">

                        <div class="row">

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

                                <div class="form-group">

                                    <label>
                                        Exam Type
                                    </label>

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

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

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

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

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

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                            </div>

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

                                <div class="form-group">

                                    <label>
                                        Class
                                    </label>

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

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

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

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

                                                <?= 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="submit"
                                    class="btn btn-primary btn-block"
                                >

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

                                    Search

                                </button>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

            <!-- RESULT CARD -->

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

                <div class="card-header">

                    <h3 class="card-title">

                        Schedule Result

                    </h3>

                </div>

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

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

                        <div class="table-responsive">

                            <table
                                class="table table-bordered table-hover mb-0"
                                id="scheduleTable"
                            >

                                <thead class="thead-light">

                                    <tr>

                                        <th width="70">
                                            #
                                        </th>

                                        <th>
                                            Subject
                                        </th>

                                        <th width="150">
                                            Subject Code
                                        </th>

                                        <th width="180">
                                            Exam Date
                                        </th>

                                        <th width="140">
                                            Max Marks
                                        </th>

                                        <th width="140">
                                            Status
                                        </th>

                                    </tr>

                                </thead>

                                <tbody>

                                    <?php foreach ($schedules as $index => $row): ?>

                                        <tr>

                                            <td>
                                                <?= $index + 1; ?>
                                            </td>

                                            <td>

                                                <strong>
                                                    <?= htmlspecialchars($row['subject_name']); ?>
                                                </strong>

                                            </td>

                                            <td>

                                                <?= htmlspecialchars($row['subject_code']); ?>

                                            </td>

                                            <td>

                                                <?php if ((int) $row['no_exam'] === 1): ?>

                                                    <span class="badge badge-danger">

                                                        No Exam

                                                    </span>

                                                <?php else: ?>

                                                    <?= date(
                                                        'd M Y',
                                                        strtotime($row['exam_date'])
                                                    ); ?>

                                                <?php endif; ?>

                                            </td>

                                            <td>

                                                <?= htmlspecialchars($row['max_marks']); ?>

                                            </td>

                                            <td>

                                                <?php if ((int) $row['no_exam'] === 1): ?>

                                                    <span class="badge badge-secondary">

                                                        Disabled

                                                    </span>

                                                <?php else: ?>

                                                    <span class="badge badge-success">

                                                        Scheduled

                                                    </span>

                                                <?php endif; ?>

                                            </td>

                                        </tr>

                                    <?php endforeach; ?>

                                </tbody>

                            </table>

                        </div>

                    <?php else: ?>

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

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

                            <p class="mb-0">

                                No schedules found.

                            </p>

                        </div>

                    <?php endif; ?>

                </div>

            </div>

        </div>

    </section>

</div>

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

<script src="assets/js/view-schedules.js"></script>