<?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'];

$page_title = 'Grading System';

/*
|--------------------------------------------------------------------------
| FETCH GRADES
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        id,
        grade_name,
        min_percentage,
        max_percentage,
        grade_point,
        remarks

    FROM exam_grades

    WHERE school_id = ?

    ORDER BY max_percentage DESC
");

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

$grades = $stmt->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>

                        Grading System

                    </h1>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <!-- ADD GRADE -->

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

                <div class="card-header">

                    <h3 class="card-title">

                        Add Grade Rule

                    </h3>

                </div>

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

                    <?= csrfField(); ?>

                    <div class="card-body">

                        <div class="row">

                            <!-- GRADE NAME -->

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

                                <label>

                                    Grade

                                </label>

                                <input
                                    type="text"
                                    name="grade_name"
                                    class="form-control"
                                    maxlength="10"
                                    required
                                >

                            </div>

                            <!-- MIN -->

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

                                <label>

                                    Min %

                                </label>

                                <input
                                    type="number"
                                    name="min_percentage"
                                    class="form-control"
                                    min="0"
                                    max="100"
                                    step="0.01"
                                    required
                                >

                            </div>

                            <!-- MAX -->

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

                                <label>

                                    Max %

                                </label>

                                <input
                                    type="number"
                                    name="max_percentage"
                                    class="form-control"
                                    min="0"
                                    max="100"
                                    step="0.01"
                                    required
                                >

                            </div>

                            <!-- GRADE POINT -->

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

                                <label>

                                    Grade Point

                                </label>

                                <input
                                    type="number"
                                    name="grade_point"
                                    class="form-control"
                                    min="0"
                                    max="10"
                                    step="0.01"
                                    required
                                >

                            </div>

                            <!-- REMARKS -->

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

                                <label>

                                    Remarks

                                </label>

                                <input
                                    type="text"
                                    name="remarks"
                                    class="form-control"
                                    maxlength="255"
                                >

                            </div>

                        </div>

                    </div>

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

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

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

                            Save Grade

                        </button>

                    </div>

                </form>

            </div>

            <!-- GRADE TABLE -->

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

                <div class="card-header">

                    <h3 class="card-title">

                        Grade Rules

                    </h3>

                </div>

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

                    <table
                        id="gradeTable"
                        class="table table-bordered table-striped"
                    >

                        <thead>

                            <tr>

                                <th>

                                    Grade

                                </th>

                                <th>

                                    Min %

                                </th>

                                <th>

                                    Max %

                                </th>

                                <th>

                                    Grade Point

                                </th>

                                <th>

                                    Remarks

                                </th>

                                <th width="10%">

                                    Action

                                </th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php foreach ($grades as $grade): ?>

                                <tr>

                                    <td>

                                        <strong>

                                            <?= htmlspecialchars($grade['grade_name']); ?>

                                        </strong>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($grade['min_percentage']); ?>%

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($grade['max_percentage']); ?>%

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($grade['grade_point']); ?>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($grade['remarks']); ?>

                                    </td>

                                    <td>

                                        <form
                                            class="deleteGradeForm"
                                            method="POST"
                                        >

                                            <?= csrfField(); ?>

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

                                            <button
                                                type="submit"
                                                class="btn btn-danger btn-sm"
                                            >

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

                                            </button>

                                        </form>

                                    </td>

                                </tr>

                            <?php endforeach; ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </section>

</div>

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

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

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

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

<script src="assets/js/grading-system.js"></script>

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