<?php
// Reusable academic filter: School Year + Semester (GET form)
// Usage: include this view in management pages that need academic scoping.
// - Accepts ambient vars if available: $schoolYears, $schoolYear, $semester.
// - Defaults to current config values when not provided via GET or vars.
// - No action attribute to auto-submit to current URL.
// - Reset link uses '?' to clear query string reliably.

// Ensure helper functions are available
if (function_exists('helper')) { @helper('GlobalConfigHelper'); }

// Resolve defaults
$currentYear = function_exists('getSchoolYear') ? (string) (getSchoolYear() ?? '') : '';
$currentSem  = function_exists('getSemester')   ? (string) (getSemester()   ?? '') : '';
if ($currentYear === '' || $currentSem === '') {
  try {
    $cfg = new \App\Models\ConfigurationModel();
    if ($currentYear === '') $currentYear = (string) ($cfg->getConfig('school_year') ?? '');
    if ($currentSem  === '') $currentSem  = (string) ($cfg->getConfig('semester')    ?? '');
  } catch (\Throwable $e) { /* ignore */ }
}

$showSchoolYearControl = $showSchoolYearControl ?? true;

$selectedYear = (string)($schoolYear ?? ($_GET['school_year'] ?? ''));
if ($selectedYear === '') { $selectedYear = $currentYear; }

$semVal = (string)($semester ?? ($_GET['semester'] ?? ''));
if ($semVal === '') { $semVal = $currentSem; }

$classSectionVal = (string)($classSectionId ?? ($_GET['class_section_id'] ?? ''));
?>
<form method="get" class="row g-2 align-items-center justify-content-center mb-3">
  <?php if ($classSectionVal !== ''): ?>
    <input type="hidden" name="class_section_id" value="<?= esc($classSectionVal) ?>">
  <?php endif; ?>
  <?php if ($showSchoolYearControl): ?>
    <div class="col-auto"><label class="form-label mb-0">School year</label></div>
    <div class="col-auto">
      <?php $years = isset($schoolYears) && is_array($schoolYears) ? $schoolYears : []; ?>
      <select name="school_year" class="form-select form-select-sm" style="min-width: 180px;">
        <?php if (!empty($years)): ?>
          <?php foreach ($years as $y): $val = is_array($y) && isset($y['school_year']) ? (string)$y['school_year'] : (string)$y; ?>
            <option value="<?= esc($val) ?>" <?= ($selectedYear === $val ? 'selected' : '') ?>><?= esc($val) ?></option>
          <?php endforeach; ?>
        <?php else: ?>
          <?php if ($selectedYear !== ''): ?>
            <option value="<?= esc($selectedYear) ?>" selected><?= esc($selectedYear) ?></option>
          <?php else: ?>
            <option value="">-</option>
          <?php endif; ?>
        <?php endif; ?>
      </select>
    </div>
  <?php endif; ?>

  <div class="col-auto"><label class="form-label mb-0">Semester</label></div>
  <div class="col-auto">
    <select name="semester" class="form-select form-select-sm" style="min-width: 140px;">
      <option value="">—</option>
      <?php if (!empty($showAllSemesterOption)): ?>
      <option value="year" <?= (strcasecmp($semVal, 'year') === 0 ? 'selected' : '') ?>>Whole Year</option>
      <?php endif; ?>
      <option value="Fall"   <?= (strcasecmp($semVal, 'Fall')   === 0 ? 'selected' : '') ?>>Fall</option>
      <option value="Spring" <?= (strcasecmp($semVal, 'Spring') === 0 ? 'selected' : '') ?>>Spring</option>
    </select>
  </div>

  <div class="col-auto">
    <button type="submit" class="btn btn-secondary btn-sm">Apply</button>
    <a class="btn btn-outline-secondary btn-sm" href="?">Reset</a>
  </div>
</form>
