<?php

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

?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), route('manage-trees', ['tree' => $tree->name()]) => I18N::translate('Manage family trees '), $title]]) ?>

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

<p>
    <?= I18N::translate('This will update the highest-level part or parts of the place name. For example, “Mexico” will match “Quintana Roo, Mexico”, but not “Santa Fe, New Mexico”.') ?>
</p>

<form method="post" action="<?= e(route(UpdatePlacesAction::class, ['tree' => $tree->name()])) ?>">
    <?= csrf_field() ?>

    <dl>
        <dt>
            <label for="search"><?= I18N::translate('Search for') ?></label>
        </dt>
        <dd>
            <input name="search" id="search" type="text" size="60" value="<?= e($search) ?>" data-autocomplete-type="PLAC" required autofocus>
        </dd>
        <dt>
            <label for="replace"><?= I18N::translate('Replace with') ?></label>
        </dt>
        <dd>
            <input name="replace" id="replace" type="text" size="60" value="<?= e($replace) ?>" data-autocomplete-type="PLAC" required>
        </dd>
    </dl>

    <button class="btn btn-primary" type="submit" name="submit" value="preview">
        <?= /* I18N: A button label. */ I18N::translate('preview') ?>
    </button>

    <?php if ($search !== '' && $replace !== '') : ?>
        <?php if (empty($changes)) : ?>
            <p>
                <?= I18N::translate('No places have been found.') ?>
            </p>
        <?php else : ?>
            <p>
                <?= I18N::translate('The following places would be changed:') ?>
            </p>

            <ul>
                <?php foreach ($changes as $old_place => $new_place) : ?>
                    <li>
                        <?= e($old_place) ?> &rarr; <?= e($new_place) ?>
                    </li>
                <?php endforeach ?>
            </ul>

            <button class="btn btn-primary" type="submit" name="submit" value="update">
                <?= /* I18N: A button label. */ I18N::translate('update') ?>
            </button>
        <?php endif ?>
    <?php endif ?>
</form>
