<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesAllPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Tree;
use Illuminate\Support\Collection;

/**
 * @var Collection<int,object> $stories
 * @var string                 $title
 * @var Tree                   $tree
 * @var array<string>          $tree_names
 */

?>

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

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

<form method="post" class="row">
    <div class="col-auto">
        <div class="input-group">
            <?= view('components/select', ['name' => 'tree', 'selected' => $tree->name(), 'options' => $tree_names, 'aria_label' => I18N::translate('Family tree')]) ?>
            <input type="submit" class="btn btn-primary" value="<?= I18N::translate('show') ?>">
        </div>
    </div>

    <?= csrf_field() ?>
</form>

<p>
    <a href="<?= e(route('module', ['module' => 'stories', 'action' => 'AdminEdit', 'tree' => $tree->name()])) ?>" class="btn btn-link">
        <?= view('icons/add') ?>
        <?= I18N::translate('Add a story') ?>
    </a>
</p>

<table
    class="table table-bordered table-sm datatables d-none"
    <?= view('lists/datatables-attributes') ?>
    data-columns="<?= e(json_encode([
        null,
        null,
        ['sortable' => false],
        ['sortable' => false],
    ], JSON_THROW_ON_ERROR)) ?>"
>
    <caption>
        <?= I18N::translate('Stories') ?>
    </caption>

    <thead>
        <tr>
            <th><?= I18N::translate('Individual') ?></th>
            <th><?= I18N::translate('Story title') ?></th>
            <th><?= I18N::translate('Edit') ?></th>
            <th><?= I18N::translate('Delete') ?></th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($stories as $story) : ?>
            <tr>
                <td>
                    <?php if ($story->individual !== null) : ?>
                        <a href="<?= e($story->individual->url()) ?>#tab-stories">
                            <?= $story->individual->fullName() ?>
                        </a>
                    <?php else : ?>
                        <?= $story->xref ?>
                    <?php endif ?>
                </td>
                <td>
                    <?= e($story->title) ?>
                </td>
                <td>
                    <a class="btn btn-primary" href="<?= e(route('module', ['module' => 'stories', 'action' => 'AdminEdit', 'tree' => $tree->name(), 'block_id' => $story->block_id])) ?>" aria-label="<?= I18N::translate('Edit') ?>">
                        <?= view('icons/edit') ?>
                        <?= I18N::translate('Edit') ?>
                    </a>
                </td>
                <td>
                    <form method="post" action="<?= e(route('module', ['module' => 'stories', 'action' => 'AdminDelete', 'tree' => $tree->name(), 'block_id' => $story->block_id])) ?>">
                        <button type="submit" class="btn btn-danger" data-wt-confirm="<?= I18N::translate('Are you sure you want to delete “%s”?', e($story->title)) ?>" onclick="return confirm(this.dataset.confirm);" aria-label="<?= I18N::translate('Delete') ?>">
                            <?= view('icons/delete') ?>
                            <?= I18N::translate('Delete') ?>
                        </button>

                        <?= csrf_field() ?>
                    </form>
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>
