<?php

use Fisharebest\Webtrees\Census\CensusPlaceInterface;
use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\I18N;

/**
 * @var array<CensusPlaceInterface> $census_places
 */

?>

<script>
  function selectCensus(el) {
    var option = $(":selected", el);

    $("input[id^=DATE]", $(el).closest("form")).val(option.val());
    $("input[id^=PLAC]", $(el).closest("form")).val(option.data("place"));
    $("input.census-class", $(el).closest("form")).val(option.data("census"));
  }
</script>

<select id="census-selector" class="form-control" onchange="selectCensus(this)">
    <option value="" disabled selected hidden><?= I18N::translate('Census date') ?></option>

    <?php foreach ($census_places as $census_place) : ?>
        <optgroup label="<?= $census_place->censusPlace() ?>">
          <?php foreach ($census_place->allCensusDates() as $census) : ?>
              <option value="<?= $census->censusDate() ?>" data-place="<?= $census->censusPlace() ?>" data-census="<?= get_class($census) ?>">
                  <?= (new Date($census->censusDate()))->minimumDate()->format('%Y') ?>
                  —
                  <?= $census->censusPlace() ?>
              </option>
          <?php endforeach ?>
        </optgroup>
    <?php endforeach ?>
</select>
