<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesAllPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleExternalUrlInterface;
use Fisharebest\Webtrees\Module\ModuleInterface;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;

/**
 * @var array<string,array<string,string>> $access_summary
 * @var string                             $description
 * @var class-string<ModuleInterface>      $interface
 * @var Collection<int,ModuleInterface>    $modules
 * @var string                             $title
 * @var Collection<int,Tree>               $trees
 * @var bool                               $uses_access
 * @var bool                               $uses_sorting
 */

?>

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

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

<p><?= $description ?></p>

<form method="post">
    <table class="table table-bordered wt-table-components">
        <thead>
            <tr>
                <th><?= I18N::translate('Module') ?></th>
                <th class="text-center"><?= I18N::translate('Enabled') ?></th>
                <?php if ($uses_access) : ?>
                    <th colspan="2" class="text-center"><?= I18N::translate('Access level') ?></th>
                <?php endif ?>
                <?php if ($uses_sorting) : ?>
                    <th class="text-center"><?= I18N::translate('Move up') ?></th>
                    <th class="text-center"><?= I18N::translate('Move down') ?></th>
                <?php endif ?>
            </tr>
        </thead>

        <tbody>
            <?php foreach ($modules as $module) : ?>
                <tr>
                    <th scope="col">
                        <input type="hidden" name="order[]" value="<?= e($module->name()) ?>">
                        <span title="<?= e(strip_tags($module->description())) ?>">
                            <?= $module instanceof ModuleCustomInterface ? $module->customTranslations(I18N::languageTag())[$module->title()] ?? $module->title() : $module->title() ?>
                        </span>
                        <?php if ($module instanceof ModuleConfigInterface) : ?>
                            <?php if ($module->isEnabled()) : ?>
                                <a href="<?= e($module->getConfigLink()) ?>" title="<?= I18N::translate('Preferences') ?>">
                                    <?= view('icons/preferences') ?>
                                    <span class="visually-hidden">
                                        <?= I18N::translate('Preferences') ?>
                                    </span>
                                </a>
                            <?php else : ?>
                                <span class="text-muted">
                                    <?= view('icons/preferences') ?>
                                </span>
                            <?php endif ?>
                        <?php endif ?>
                        <?php if ($module instanceof ModuleCustomInterface) : ?>
                            <?= view('admin/custom-module-info', ['module' => $module]) ?>
                        <?php endif ?>
                        <?php if ($module instanceof ModuleExternalUrlInterface) : ?>
                            <?= view('admin/external-module-info', ['module' => $module]) ?>
                        <?php endif ?>
                    </th>

                    <td class="text-center">
                        <label class="d-block">
                            <input type="checkbox" name="status-<?= e($module->name()) ?>"
                                   id="status-<?= e($module->name()) ?>" <?= $module->isEnabled() ? 'checked' : '' ?>>
                            <span class="visually-hidden">
                                <?= I18N::translate('Enabled') ?>
                            </span>
                        </label>
                    </td>

                    <?php if ($uses_access) : ?>
                        <td>
                            <ul class="list-unstyled">
                                <?php foreach ($access_summary[$module->name()] as $level) : ?>
                                    <li><?= $level ?></li>
                                <?php endforeach ?>
                            </ul>

                            <div class="modal fade" data-bs-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true" id="access-level-<?= $module->name() ?>">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h2 class="modal-title">
                                                <?= e($module->title()) ?> – <?= I18N::translate('Access level') ?>
                                            </h2>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?= I18N::translate('close') ?>">
                                            </button>
                                        </div>
                                        <div class="modal-body">
                                            <table class="table table-sm">
                                                <tbody>
                                                    <?php foreach ($trees as $tree) : ?>
                                                        <tr>
                                                            <td>
                                                                <?= e($tree->title()) ?>
                                                            </td>
                                                            <td>
                                                                <?= view('components/select', ['name' => 'access-' . $module->name() . '-' . $tree->id(), 'selected' => $module->accessLevel($tree, $interface), 'options' => Auth::accessLevelNames()]) ?>
                                                        </tr>
                                                    <?php endforeach ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">
                                                <?= view('icons/cancel') ?>
                                                <?= I18N::translate('close') ?>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>

                        <td>
                            <button type="button" class="btn btn-link" data-bs-toggle="modal"
                                    data-bs-target="#access-level-<?= $module->name() ?>">
                                <?= view('icons/edit') ?>
                                <span class="visually-hidden"><?= I18N::translate('edit') ?></span>
                            </button>
                        </td>
                    <?php endif ?>

                    <?php if ($uses_sorting) : ?>
                        <td class="move up text-center">
                            <a href="#" title="<?= I18N::translate('Move up') ?>">
                                <?= view('icons/arrow-up') ?>
                            </a>
                        </td>

                        <td class="move down text-center">
                            <a href="#" title="<?= I18N::translate('Move down') ?>">
                                <?= view('icons/arrow-down') ?>
                            </a>
                        </td>
                    <?php endif ?>
                </tr>
            <?php endforeach ?>
        </tbody>
    </table>

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

    <a class="btn btn-secondary" href="<?= e(route(ControlPanel::class)) ?>">
        <?= view('icons/cancel') ?>
        <?= I18N::translate('cancel') ?>
    </a>

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

<?php View::push('javascript') ?>
<script>
  $('.wt-table-components td.move').click(function () {
    let row = $(this).closest('tr');

    if ($(this).hasClass('up')) {
      row.prev().before(row);
    } else {
      row.next().after(row);
    }

    return false;
  });
</script>
<?php View::endpush() ?>
