<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;

/**
 * @var array<string,string> $steps
 * @var string               $title
 */

?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), $title]]) ?>

<h1><?= $title ?></h1>

<p>
    <?= I18N::translate('It can take several minutes to download and install the upgrade. Be patient.') ?>
</p>

<dl>
    <?php foreach ($steps as $url => $text) : ?>
    <dt><?= $text ?></dt>
    <dd data-url="<?= e($url) ?>"></dd>
    <?php endforeach ?>
</dl>

<?php View::push('javascript') ?>
<script>
  function nextAjaxStep() {
    $("dd:empty:first").each(function(n, el) {
      el.innerHTML=<?= json_encode(view('icons/spinner')) ?>;
      $(el).load(el.dataset.url, {}, function (responseText, textStatus, req) {
        el.innerHTML = responseText;
        if (textStatus === "error") {
        } else {
          nextAjaxStep();
        }
      });

      // Only process one callback at a time.
      return false;
    });
  }

  nextAjaxStep();
</script>
<?php View::endpush() ?>
