<?php

use Fisharebest\Webtrees\GedcomCode\GedcomCodeName;
use Fisharebest\Webtrees\GedcomTag;
use Fisharebest\Webtrees\Http\RequestHandlers\ReorderNamesAction;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;

?>

<h2 class="wt-page-title"><?= $title ?></h2>

<form method="post" action="<?= e(route(ReorderNamesAction::class, ['tree' => $tree->name(), 'xref' => $individual->xref()])) ?>" class="wt-page-content">
    <?= csrf_field() ?>

    <div class="wt-sortable-list">
        <?php foreach ($individual->facts(['NAME']) as $fact) : ?>
            <div class="card mb-2 wt-sortable-item">
                <input type="hidden" name="order[]" value="<?= $fact->id() ?>">
                <div class="card-header">
                    <span class="drag-handle">
                        <?= view('icons/drag-handle') ?>
                    </span>
                    <?= $fact->value() ?>
                </div>
                <div class="card-body">
                    <?= GedcomTag::getLabelValue('TYPE', GedcomCodeName::getValue($fact->attribute('TYPE'), $fact->record())) ?>
                </div>
            </div>
        <?php endforeach ?>
    </div>

    <p>
        <button class="btn btn-primary" type="submit">
            <?= view('icons/save') ?>
            <?= /* I18N: A button label. */ I18N::translate('save') ?>
        </button>

        <a class="btn btn-secondary" href="<?= e($individual->url()) ?>">
            <?= view('icons/cancel') ?>
            <?= /* I18N: A button label. */ I18N::translate('cancel') ?>
        </a>
    </p>
</form>

<?php View::push('javascript') ?>
<script>
    new Sortable(document.querySelector(".wt-sortable-list"), {
        handle: ".drag-handle",
    });
</script>
<?php View::endpush() ?>
