<?php
    $currentPath = trim(service('request')->getUri()->getPath(), '/');
    $hiddenOnPaths = [
        'administrator/emergency_contact',
        'flags/flags_management',
        'administrator/calendar_view',
        'administrator/student_class_assignment',
        'administrator/sections/auto-distribute',
        'admin/enrollment/new-students',
        'payment/unpaid-parents',
        'discounts/list',
        'expenses/index',
        'reimbursements/index',
    ];
?>
<?php if (!in_array($currentPath, $hiddenOnPaths, true) && !empty($schoolYearSelectorEnabled) && isset($schoolYearContext, $schoolYearOptions)): ?>
<?php
    $query = service('request')->getUri()->getQuery();
    $returnTo = current_url() . ($query !== '' ? '?' . $query : '');
    $activeSchoolYearId = null;
    foreach ($schoolYearOptions as $year) {
        if (strtolower((string) ($year['status'] ?? '')) === 'active') {
            $activeSchoolYearId = (int) ($year['id'] ?? 0);
            break;
        }
    }
?>
<div class="school-year-context-bar border-bottom bg-light py-2">
    <div class="container-fluid px-3">
        <form method="post" action="<?= site_url('school-year/select') ?>" class="d-flex align-items-center gap-2 flex-wrap">
            <?= csrf_field() ?>
            <input type="hidden" name="return_to" value="<?= esc($returnTo) ?>">

            <label for="global-school-year" class="form-label mb-0 fw-semibold">
                School year
            </label>

            <select
                id="global-school-year"
                name="school_year_id"
                class="form-select form-select-sm w-auto"
                onchange="this.form.submit()"
                aria-label="Selected school year"
            >
                <?php foreach ($schoolYearOptions as $year): ?>
                    <option
                        value="<?= (int) $year['id'] ?>"
                        <?= (int) $year['id'] === $schoolYearContext->id() ? 'selected' : '' ?>
                    >
                        <?= esc($year['name']) ?> - <?= esc(ucfirst((string) $year['status'])) ?>
                    </option>
                <?php endforeach; ?>
            </select>

            <?php if ($schoolYearContext->isReadonly()): ?>
                <span class="btn btn-warning btn-sm disabled" aria-disabled="true">
                    <i class="bi bi-lock-fill"></i> READ-ONLY
                </span>
            <?php else: ?>
                <span class="badge text-bg-success">ACTIVE</span>
            <?php endif; ?>

            <span class="text-muted small"><?= esc($schoolYearContext->yearName()) ?></span>
        </form>
        <?php if ($schoolYearContext->isExplicitSelection() && $activeSchoolYearId !== null): ?>
            <form method="post" action="<?= site_url('school-year/select') ?>" class="d-inline-block mt-2 mt-sm-0 ms-sm-2">
                <?= csrf_field() ?>
                <input type="hidden" name="return_to" value="<?= esc($returnTo) ?>">
                <input type="hidden" name="school_year_id" value="<?= (int) $activeSchoolYearId ?>">
                <button type="submit" class="btn btn-outline-secondary btn-sm">
                    Active year
                </button>
            </form>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>
