<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Illuminate\Support\Collection;

/**
 * @var Collection<int,Fact> $facts
 * @var Individual           $record
 * @var string               $url
 */

?>
<div class="input-group mb-3">
    <span class="input-group-text" dir="ltr">iCalendar</span>

    <select class="custom-select" id="share-anniversary-select" aria-label=" <?= I18N::translate('Anniversary') ?>" name="fact_id">
        <?php foreach ($facts as $url => $label) : ?>
            <option value="<?= e($url) ?>"><?= $label ?></option>
        <?php endforeach ?>
    </select>
    <button class="btn btn-primary" type="button" id="share-anniversary-button">
        <?= view('icons/download') ?>
        <?= I18N::translate('Download') ?>
    </button>
</div>
<form id="share-anniversary-form"></form>
<script>
  document.getElementById('share-anniversary-button').addEventListener('click', function () {
    const select = document.getElementById('share-anniversary-select');
    let form = document.getElementById('share-anniversary-form');
    form.action = select.options[select.selectedIndex].value;
    form.submit();
  });
</script>
