<?php

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> $faqs
 * @var string                 $max_block_order
 * @var string                 $min_block_order
 * @var string                 $title
 * @var Tree                   $tree
 * @var array<string,string>   $tree_names
 */

?>

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

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

<p>
    <?= /* I18N: FAQ = “Frequently Asked Question” */
    I18N::translate('FAQs are lists of questions and answers, which allow you to explain the site’s rules, policies, and procedures to your visitors. Questions are typically concerned with privacy, copyright, user-accounts, unsuitable content, requirement for source-citations, etc.') ?>
    <?= I18N::translate('You may use HTML to format the answer and to add links to other websites.') ?>
</p>

<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' => 'faq', 'action' => 'AdminEdit', 'tree' => $tree->name()])) ?>" class="btn btn-link">
        <?= view('icons/add') ?>
        <?= /* I18N: FAQ = “Frequently Asked Question” */
        I18N::translate('Add an FAQ') ?>
    </a>
</p>

<table class="table table-bordered">
    <caption>
        <?= I18N::translate('Frequently asked questions') ?>
    </caption>

    <thead>
        <tr>
            <th><?= I18N::translate('Sort order') ?></th>
            <th><?= I18N::translate('Family tree') ?></th>
            <th><?= I18N::translate('Question') ?></th>
            <th><?= I18N::translate('Move up') ?></th>
            <th><?= I18N::translate('Move down') ?></th>
            <th><?= I18N::translate('Edit') ?></th>
            <th><?= I18N::translate('Delete') ?></th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($faqs as $faq) : ?>
            <tr class="faq_edit_pos">
                <td>
                    <?= I18N::number($faq->block_order) ?>
                </td>
                <td>
                    <?php if ($faq->gedcom_id === null) : ?>
                        <?= I18N::translate('All') ?>
                    <?php else : ?>
                        <?= e($tree->title()) ?>
                    <?php endif ?>
                </td>
                <td>
                    <?= e($faq->header) ?>
                </td>
                <td>
                    <?php if ($faq->block_order !== $min_block_order) : ?>
                        <a href="#" data-wt-post-url="<?= e(route('module', ['module' => 'faq', 'action' => 'AdminMoveUp', 'block_id' => $faq->block_id])) ?>" class="btn btn-secondary">
                            <?= view('icons/arrow-up') ?>
                        </a>
                    <?php endif ?>
                </td>
                <td>
                    <?php if ($faq->block_order !== $max_block_order) : ?>
                        <a href="#" data-wt-post-url="<?= e(route('module', ['module' => 'faq', 'action' => 'AdminMoveDown', 'block_id' => $faq->block_id])) ?>" class="btn btn-secondary">
                        <?= view('icons/arrow-down') ?>
                        </a>
                    <?php endif ?>
                </td>
                <td>
                    <a href="<?= e(route('module', ['module' => 'faq', 'action' => 'AdminEdit', 'block_id' => $faq->block_id, 'tree' => $tree->name()])) ?>" aria-label="<?= I18N::translate('Edit') ?>" class="btn btn-primary">
                        <?= view('icons/edit') ?>
                    </a>
                </td>
                <td>
                    <a href="#" data-wt-post-url="<?= e(route('module', ['module' => 'faq', 'action' => 'AdminDelete', 'block_id' => $faq->block_id])) ?>" data-wt-confirm="<?= I18N::translate('Are you sure you want to delete “%s”?', e($faq->header)) ?>" aria-label="<?= I18N::translate('Delete') ?>" class="btn btn-danger">
                        <?= view('icons/delete') ?>
                    </a>
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>
