<?php
/**
 * Country-Picker — Native <select> im Header
 * -------------------------------------------
 * Wird per include in den Modul-/Lehrplan-/Simulationen-Seiten eingebunden.
 *
 * Erwartet im eltern-Scope:
 *   $bp         BASE_PATH
 *
 * Beim Ändern:
 *   - POST auf /php/api/country.php (Session + Cookie)
 *   - Fallback: Cookie via JS
 *   - Seite wird neu geladen, damit alle Server-seitigen Filter greifen
 */

require_once __DIR__ . '/../../php/lib/Country.php';

if (!isset($bp)) $bp = defined('BASE_PATH') ? BASE_PATH : '';
$currentCountry = Country::current();
?>
<form class="md-country-form" method="post" action="#" onsubmit="return false;" aria-label="Land auswählen">
  <label for="md-country-select" class="md-country-label">
    <?= Country::flag($currentCountry) ?>
  </label>
  <select id="md-country-select" class="md-country-select"
          onchange="window.__GGS_setCountry(this.value)">
    <?php foreach (Country::allCountries() as $code): ?>
      <option value="<?= htmlspecialchars($code) ?>"
              <?= $code === $currentCountry ? 'selected' : '' ?>>
        <?= Country::flag($code) ?> <?= Country::label($code) ?>
      </option>
    <?php endforeach; ?>
  </select>
</form>

<style>
  /* Native-Select iPad-freundlich gestylt */
  .md-country-form {
    display: inline-flex; align-items: center; gap: .3rem;
    background: var(--ggs-white); border: 1px solid var(--ggs-border);
    border-radius: var(--ggs-radius-sm);
    padding: 0 .5rem 0 .75rem; min-height: 40px;
  }
  .md-country-form:hover, .md-country-form:focus-within {
    border-color: var(--ggs-fjord);
  }
  .md-country-label {
    font-size: 1.1rem; line-height: 1; pointer-events: none;
  }
  .md-country-select {
    appearance: auto;               /* native Wheel-Picker auf iOS */
    -webkit-appearance: auto;
    background: transparent; border: 0; cursor: pointer;
    font-size: .85rem; font-weight: 600; font-family: inherit;
    color: var(--ggs-fjord-dark);
    padding: .5rem .2rem; min-height: 40px;
  }
  .md-country-select:focus { outline: none; }
</style>

<script>
/*
 * window.__GGS_setCountry(code)
 *
 * 1. POST an /php/api/country.php (falls verfügbar) — persistiert Session + Cookie.
 * 2. Zusätzlich lokalen Cookie setzen (Fallback, wenn API-Call scheitert).
 * 3. Seite reloaden, damit Server-Filter greifen (Lehrplan-Default etc.).
 */
if (typeof window.__GGS_setCountry !== 'function') {
  window.__GGS_setCountry = function (code) {
    try {
      // Local Cookie als sofortiger Fallback (1 Jahr)
      document.cookie = 'ggs-country=' + encodeURIComponent(code)
        + '; Max-Age=' + (60*60*24*365)
        + '; Path=<?= $bp ?: "/" ?>; SameSite=Lax';
    } catch (e) {}
    // Server-Speicherung
    fetch('<?= $bp ?>/api/country', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ country: code })
    }).catch(function () {
      // Netz weg, kein Problem — Cookie reicht als Fallback
    }).finally(function () {
      // Seite neu laden, damit Server-Filter (z.B. lehrplan.php ?country=...) greifen
      // Wenn in URL bereits ?country=... steht, diesen Parameter überschreiben
      var u = new URL(window.location.href);
      u.searchParams.set('country', code);
      window.location.href = u.toString();
    });
  };
}
</script>
