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

$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'teacher']);

// ======================================================
// SAVE SETTINGS
// ======================================================

if (isset($_POST['update_settings'])) {

    $max_periods = (int)($_POST['max_periods'] ?? 6);

    // Safety
   if ($max_periods < 1 || $max_periods > 20) {
    $max_periods = 6;
}

    // Check existing row
   $stmt = $pdo->prepare("
    INSERT INTO period_settings
    (
        school_id,
        total_periods
    )
    VALUES (?, ?)

    ON DUPLICATE KEY UPDATE
        total_periods = VALUES(total_periods)
");

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

    header("Location: period_settings.php?msg=Settings Updated Successfully");
    exit;
}

// ======================================================
// FETCH CURRENT SETTINGS
// ======================================================

$stmt = $pdo->prepare("
    SELECT total_periods
    FROM period_settings
    WHERE school_id = ?
    LIMIT 1
");

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

$current_periods = $stmt->fetchColumn() ?: 6;

// ======================================================

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>Period Settings</h1>
                </div>

            </div>

        </div>
    </section>

    <section class="content">

        <div class="container-fluid">

            <?php if (isset($_GET['msg'])): ?>

                <div class="alert alert-success">
                    <?= htmlspecialchars($_GET['msg']) ?>
                </div>

            <?php endif; ?>

            <div class="card">

                <div class="card-header">
                    <h3 class="card-title">Manage Daily Periods</h3>
                </div>

                <div class="card-body">

                    <form method="POST">

                        <div class="form-group">

                            <label>
                                Maximum Periods Per Day
                            </label>

                            <input
                                type="number"
                                name="max_periods"
                                class="form-control"
                                min="1"
                                max="20"
                                value="<?= htmlspecialchars($current_periods) ?>"
                                required
                            >

                        </div>

                        <button
                            type="submit"
                            name="update_settings"
                            class="btn btn-success"
                        >
                            Update Settings
                        </button>

                        <a
                            href="timetable.php"
                            class="btn btn-secondary"
                        >
                            Back to Timetable
                        </a>

                    </form>

                </div>

            </div>

        </div>

    </section>

</div>

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