<?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 = 'Result Settings';

/*
|--------------------------------------------------------------------------
| FETCH SETTINGS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        passing_percentage,
        show_rank,
        show_grade,
        show_percentage,
        auto_publish_results,
        remarks_enabled

    FROM exam_result_settings

    WHERE school_id = ?

    LIMIT 1
");

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

$settings = $stmt->fetch(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>

                        Result Settings

                    </h1>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

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

                <div class="card-header">

                    <h3 class="card-title">

                        Configure Result Settings

                    </h3>

                </div>

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

                    <?= csrfField(); ?>

                    <div class="card-body">

                        <div class="row">

                            <!-- PASSING PERCENTAGE -->

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

                                <label>

                                    Passing Percentage

                                </label>

                                <input
                                    type="number"
                                    name="passing_percentage"
                                    class="form-control"
                                    min="1"
                                    max="100"
                                    required
                                    value="<?= htmlspecialchars($settings['passing_percentage'] ?? 33); ?>"
                                >

                            </div>

                            <!-- SHOW RANK -->

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

                                <label>

                                    Show Rank

                                </label>

                                <select
                                    name="show_rank"
                                    class="form-control"
                                >

                                    <option
                                        value="1"
                                        <?= ((int) ($settings['show_rank'] ?? 1) === 1) ? 'selected' : ''; ?>
                                    >

                                        Yes

                                    </option>

                                    <option
                                        value="0"
                                        <?= ((int) ($settings['show_rank'] ?? 1) === 0) ? 'selected' : ''; ?>
                                    >

                                        No

                                    </option>

                                </select>

                            </div>

                            <!-- SHOW GRADE -->

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

                                <label>

                                    Show Grade

                                </label>

                                <select
                                    name="show_grade"
                                    class="form-control"
                                >

                                    <option
                                        value="1"
                                        <?= ((int) ($settings['show_grade'] ?? 1) === 1) ? 'selected' : ''; ?>
                                    >

                                        Yes

                                    </option>

                                    <option
                                        value="0"
                                        <?= ((int) ($settings['show_grade'] ?? 1) === 0) ? 'selected' : ''; ?>
                                    >

                                        No

                                    </option>

                                </select>

                            </div>

                            <!-- SHOW PERCENTAGE -->

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

                                <label>

                                    Show Percentage

                                </label>

                                <select
                                    name="show_percentage"
                                    class="form-control"
                                >

                                    <option
                                        value="1"
                                        <?= ((int) ($settings['show_percentage'] ?? 1) === 1) ? 'selected' : ''; ?>
                                    >

                                        Yes

                                    </option>

                                    <option
                                        value="0"
                                        <?= ((int) ($settings['show_percentage'] ?? 1) === 0) ? 'selected' : ''; ?>
                                    >

                                        No

                                    </option>

                                </select>

                            </div>

                            <!-- AUTO PUBLISH -->

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

                                <label>

                                    Auto Publish Results

                                </label>

                                <select
                                    name="auto_publish_results"
                                    class="form-control"
                                >

                                    <option
                                        value="1"
                                        <?= ((int) ($settings['auto_publish_results'] ?? 0) === 1) ? 'selected' : ''; ?>
                                    >

                                        Enabled

                                    </option>

                                    <option
                                        value="0"
                                        <?= ((int) ($settings['auto_publish_results'] ?? 0) === 0) ? 'selected' : ''; ?>
                                    >

                                        Disabled

                                    </option>

                                </select>

                            </div>

                            <!-- REMARKS -->

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

                                <label>

                                    Remarks System

                                </label>

                                <select
                                    name="remarks_enabled"
                                    class="form-control"
                                >

                                    <option
                                        value="1"
                                        <?= ((int) ($settings['remarks_enabled'] ?? 1) === 1) ? 'selected' : ''; ?>
                                    >

                                        Enabled

                                    </option>

                                    <option
                                        value="0"
                                        <?= ((int) ($settings['remarks_enabled'] ?? 1) === 0) ? 'selected' : ''; ?>
                                    >

                                        Disabled

                                    </option>

                                </select>

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

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </section>

</div>

<script src="assets/js/result-settings.js"></script>

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