<?php

declare(strict_types=1);

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

allow_roles(['admin']);

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

$page_title = 'Exam Types';

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

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

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

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

$listStmt = $pdo->prepare("
    SELECT
        id,
        exam_name,
        status,
        created_at
    FROM exam_types
    WHERE school_id = ?
    ORDER BY display_order ASC, id DESC
");

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

$exam_types = $listStmt->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>Manage Exam Types</h1>

                </div>

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

                    <button
                        type="button"
                        class="btn btn-primary"
                        id="addExamTypeBtn"
                    >
                        <i class="fas fa-plus"></i>
                        Add Exam Type
                    </button>

                </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; ?>

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

                <div class="card-header">

                    <h3 class="card-title">

                        Exam Types List

                    </h3>

                </div>

                <div class="card-body table-responsive">

                    <table
                        id="examTypesTable"
                        class="table table-bordered table-hover"
                    >

                        <thead>

                            <tr>

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

                                <th>Exam Name</th>

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

                                <th width="180">Created</th>

                                <th width="180">Action</th>

                            </tr>

                        </thead>

                        <tbody>

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

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

                                    <tr>

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

                                        <td>
                                            <?= htmlspecialchars($row['exam_name']); ?>
                                        </td>

                                        <td>

                                            <?php if ($row['status'] === 'Active'): ?>

                                                <span class="badge badge-success">
                                                    Active
                                                </span>

                                            <?php else: ?>

                                                <span class="badge badge-danger">
                                                    Inactive
                                                </span>

                                            <?php endif; ?>

                                        </td>

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

                                        <td>

                                            <button
                                                type="button"
                                                class="btn btn-success btn-sm editExamTypeBtn"

                                                data-id="<?= (int) $row['id']; ?>"

                                                data-name="<?= htmlspecialchars($row['exam_name']); ?>"

                                                data-status="<?= htmlspecialchars($row['status']); ?>"
                                            >
                                                <i class="fas fa-edit"></i>
                                                Edit
                                            </button>

                                            <form
                                                action="process/delete_exam_type.php"
                                                method="POST"
                                                class="d-inline deleteExamTypeForm"
                                            >

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

                                                <input
                                                    type="hidden"
                                                    name="id"
                                                    value="<?= (int) $row['id']; ?>"
                                                >

                                                <button
                                                    type="submit"
                                                    class="btn btn-danger btn-sm"
                                                >
                                                    <i class="fas fa-trash"></i>
                                                    Delete
                                                </button>

                                            </form>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </section>

</div>

<!-- MODAL -->

<div
    class="modal fade"
    id="examTypeModal"
    tabindex="-1"
>

    <div class="modal-dialog">

        <div class="modal-content">

            <form
                action="process/save_exam_type.php"
                method="POST"
                id="examTypeForm"
            >

                <div class="modal-header">

                    <h5 class="modal-title" id="modalTitle">

                        Add Exam Type

                    </h5>

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

                </div>

                <div class="modal-body">

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

                    <input
                        type="hidden"
                        name="id"
                        id="exam_type_id"
                        value="0"
                    >

                    <div class="form-group">

                        <label>
                            Exam Name
                        </label>

                        <input
                            type="text"
                            name="exam_name"
                            id="exam_name"
                            class="form-control"
                            maxlength="100"
                            required
                        >

                    </div>

                    <div class="form-group">

                        <label>
                            Status
                        </label>

                        <select
                            name="status"
                            id="status"
                            class="form-control"
                            required
                        >

                            <option value="Active">
                                Active
                            </option>

                            <option value="Inactive">
                                Inactive
                            </option>

                        </select>

                    </div>

                </div>

                <div class="modal-footer">

                    <button
                        type="button"
                        class="btn btn-secondary"
                        data-dismiss="modal"
                    >
                        Close
                    </button>

                    <button
                        type="submit"
                        class="btn btn-primary"
                    >
                        Save Exam Type
                    </button>

                </div>

            </form>

        </div>

    </div>

</div>

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

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

<!-- DataTables -->
<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>

<!-- DataTables Responsive -->
<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>

<!-- 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>


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

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

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

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