<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\Http\RequestHandlers\AddUnlinkedPage;
use Fisharebest\Webtrees\Http\RequestHandlers\CheckTree;
use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\CreateMediaObjectModal;
use Fisharebest\Webtrees\Http\RequestHandlers\CreateNoteModal;
use Fisharebest\Webtrees\Http\RequestHandlers\CreateRepositoryModal;
use Fisharebest\Webtrees\Http\RequestHandlers\CreateSourceModal;
use Fisharebest\Webtrees\Http\RequestHandlers\CreateSubmitterModal;
use Fisharebest\Webtrees\Http\RequestHandlers\DataFixChoose;
use Fisharebest\Webtrees\Http\RequestHandlers\DeleteTreeAction;
use Fisharebest\Webtrees\Http\RequestHandlers\ExportGedcomPage;
use Fisharebest\Webtrees\Http\RequestHandlers\FindDuplicateRecords;
use Fisharebest\Webtrees\Http\RequestHandlers\GedcomLoad;
use Fisharebest\Webtrees\Http\RequestHandlers\ImportGedcomPage;
use Fisharebest\Webtrees\Http\RequestHandlers\MergeRecordsPage;
use Fisharebest\Webtrees\Http\RequestHandlers\PendingChangesLogPage;
use Fisharebest\Webtrees\Http\RequestHandlers\RenumberTreePage;
use Fisharebest\Webtrees\Http\RequestHandlers\SelectDefaultTree;
use Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
use Fisharebest\Webtrees\Http\RequestHandlers\TreePreferencesPage;
use Fisharebest\Webtrees\Http\RequestHandlers\TreePrivacyPage;
use Fisharebest\Webtrees\Http\RequestHandlers\UnconnectedPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;

/**
 * @var Collection<Tree> $all_trees
 * @var string           $title
 * @var Tree             $tree
 */

?>

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

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

<?php if ($all_trees->isEmpty()) : ?>
    <div class="alert alert-info">
        <?= I18N::translate('You need to create a family tree.') ?>
    </div>
<?php endif ?>

<div id="accordion" aria-multiselectable="true">
    <?php foreach ($all_trees as $managed_tree) : ?>
        <?php if (Auth::isManager($managed_tree)) : ?>
            <div class="accordion-item">
                <div class="accordion-header" id="card-tree-header-<?= $managed_tree->id() ?>">
                    <button class="accordion-button <?= $managed_tree->id() === $tree->id() || !$managed_tree->imported() ? ' ' : ' collapsed' ?>" type="button" data-bs-toggle="collapse" data-bs-target="#card-tree-content-<?= $managed_tree->id() ?>" aria-expanded="<?= $managed_tree->id() === $tree->id() || !$managed_tree->imported() ? 'true' : 'false' ?>" aria-controls="card-tree-content-<?= $managed_tree->id() ?>">
                        <h2>
                            <?= view('icons/tree') ?>
                            <?= e($managed_tree->name()) ?> — <?= e($managed_tree->title()) ?>
                        </h2>
                    </button>
                </div>

                <div id="card-tree-content-<?= $managed_tree->id() ?>" class="accordion-collapse collapse<?= $managed_tree->id() === $tree->id() || !$managed_tree->imported() ? ' show' : '' ?>" aria-labelledby="panel-tree-header-<?= $managed_tree->id() ?>">
                    <div class="accordion-body">
                        <?php $importing = DB::table('gedcom_chunk')->where('gedcom_id', '=', $managed_tree->id())->where('imported', '=', 0)->exists() ?>
                        <?php if ($importing) : ?>
                            <div id="import<?= $managed_tree->id() ?>" class="col-12">
                                <div class="progress" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
                                    <div class="progress-bar">
                                        <?= I18N::translate('Calculating…') ?>
                                    </div>
                                </div>
                            </div>
                            <?php View::push('javascript'); ?>
                                <script>
                                    $("#import<?= $managed_tree->id() ?>").load("<?= route(GedcomLoad::class, ['tree' => $managed_tree->name()]) ?>", {});
                                </script>
                                <?php View::endpush() ?>
                        <?php endif ?>
                        <div class="row<?= $importing ? ' d-none' : '' ?>" id="actions<?= $managed_tree->id() ?>">
                            <div class="col-sm-6 col-md-3">
                                <h3>
                                    <?= I18N::translate('Family tree') ?>
                                </h3>
                                <ul class="fa-ul mx-0">
                                    <li>
                                        <span class="fa-li"><?= view('icons/tree') ?></span>
                                        <a href="<?= e(route(TreePage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Home page') ?>
                                        </a>
                                    </li>
                                </ul>
                                <ul class="fa-ul mx-0">
                                    <!-- PREFERENCES -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                                        <a href="<?= e(route(TreePreferencesPage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Preferences') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- PRIVACY -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/lock') ?></span>
                                        <a href="<?= e(route(TreePrivacyPage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Privacy') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- DELETE -->
                                    <?php if (count($all_trees) > 1 && Auth::isAdmin()) : ?>
                                        <li>
                                            <span class="fa-li"><?= view('icons/delete') ?></span>
                                            <a href="#" data-wt-post-url="<?= e(route(DeleteTreeAction::class, ['tree' => $managed_tree->name()])) ?>" data-wt-reload-url="<?= e(route(ControlPanel::class)) ?>" data-wt-confirm="<?= I18N::translate('Are you sure you want to delete “%s”?', e($managed_tree->title())) ?>">
                                                <?= I18N::translate('Delete') ?>
                                            </a>
                                        </li>
                                    <?php endif ?>

                                    <!-- SET AS DEFAULT -->
                                    <?php if (count($all_trees) > 1 && Auth::isAdmin()) : ?>
                                        <li>
                                            <span class="fa-li"><?= view('icons/favorite') ?></span>
                                            <?php if ($managed_tree->name() === Site::getPreference('DEFAULT_GEDCOM')) : ?>
                                                <?= I18N::translate('Default family tree') ?>
                                            <?php else : ?>
                                                <a href="#" onclick="document.defaultform<?= $managed_tree->id() ?>.submit();">
                                                    <?= I18N::translate('Set as default') ?>
                                                    <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                                </a>
                                                <form method="post" action="<?= route(SelectDefaultTree::class, ['tree' => $managed_tree->name()]) ?>" name="defaultform<?= $managed_tree->id() ?>">
                                                    <!-- A11Y - forms need submit buttons, but they look ugly here -->
                                                    <button class="visually-hidden" type="submit">
                                                        <?= I18N::translate('Set as default') ?>
                                                    </button>

                                                    <?= csrf_field() ?>
                                                </form>
                                            <?php endif ?>
                                        </li>
                                    <?php endif ?>
                                </ul>

                                <ul class="fa-ul mx-0">
                                    <!-- DATA FIXES -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/data-fix') ?></span>
                                        <a href="<?= e(route(DataFixChoose::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Data fixes') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                            <div class="col-sm-6 col-md-3">
                                <h3>
                                    <?= /* I18N: Individuals, sources, dates, places, etc. */
                                    I18N::translate('Genealogy data') ?>
                                </h3>
                                <ul class="fa-ul mx-0">
                                    <!-- FIND DUPLICATES -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/copy') ?></span>
                                        <a href="<?= e(route(FindDuplicateRecords::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Find duplicates') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- MERGE -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/merge') ?></span>
                                        <a href="<?= e(route(MergeRecordsPage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Merge records') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- CHECK FOR ERRORS -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/warning') ?></span>
                                        <a href="<?= e(route(CheckTree::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Check for errors') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- UNCONNECTED INDIVIDUALS -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/unlink') ?></span>
                                        <a href="<?= e(route(UnconnectedPage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Find unrelated individuals') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- RENUMBER -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/sort') ?></span>
                                        <a href="<?= e(route(RenumberTreePage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Renumber XREFs') ?>
                                            <span class="visually-hidden">
                                        <?= e($managed_tree->title()) ?>
                                    </span>
                                        </a>
                                    </li>
                                    <!-- CHANGES -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/edit') ?></span>
                                        <a href="<?= route(PendingChangesLogPage::class, ['tree' => $managed_tree->name()]) ?>">
                                            <?= I18N::translate('Changes log') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                            <div class="col-sm-6 col-md-3">
                                <h3>
                                    <?= I18N::translate('Add unlinked records') ?>
                                </h3>
                                <ul class="fa-ul mx-0">
                                    <!-- UNLINKED INDIVIDUAL -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/individual') ?></span>
                                        <a href="<?= e(route(AddUnlinkedPage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Individual') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- UNLINKED SOURCE -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/source') ?></span>
                                        <a href="#" data-wt-href="<?= e(route(CreateSourceModal::class, ['tree' => $managed_tree->name()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
                                            <?= I18N::translate('Source') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- UNLINKED REPOSITORY -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/repository') ?></span>
                                        <a href="#" data-wt-href="<?= e(route(CreateRepositoryModal::class, ['tree' => $managed_tree->name()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
                                            <?= I18N::translate('Repository') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- UNLINKED MEDIA OBJECT -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/media') ?></span>
                                        <a href="#" data-wt-href="<?= e(route(CreateMediaObjectModal::class, ['tree' => $managed_tree->name()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">

                                            <?= I18N::translate('Media object') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- UNLINKED NOTE -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/note') ?></span>
                                        <a href="#" data-wt-href="<?= e(route(CreateNoteModal::class, ['tree' => $managed_tree->name()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
                                            <?= I18N::translate('Shared note') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- UNLINKED SUBMITTER -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/submitter') ?></span>
                                        <a href="#" data-wt-href="<?= e(route(CreateSubmitterModal::class, ['tree' => $managed_tree->name()])) ?>" data-bs-target="#wt-ajax-modal" data-bs-toggle="modal">
                                            <?= I18N::translate('Submitter') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                            <div class="col-sm-6 col-md-3">
                                <h3>
                                    <?= I18N::translate('GEDCOM file') ?>
                                </h3>
                                <ul class="fa-ul mx-0">
                                    <!-- DOWNLOAD/Export -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/download') ?></span>
                                        <a href="<?= e(route(ExportGedcomPage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Export') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                    <!-- UPLOAD/IMPORT -->
                                    <li>
                                        <span class="fa-li"><?= view('icons/upload') ?></span>
                                        <a href="<?= e(route(ImportGedcomPage::class, ['tree' => $managed_tree->name()])) ?>">
                                            <?= I18N::translate('Import') ?>
                                            <span class="visually-hidden"><?= e($managed_tree->title()) ?></span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif ?>
    <?php endforeach ?>
</div>

<?= view('modals/ajax') ?>
