<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Http\RequestHandlers\MapDataAdd;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataDelete;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataDeleteUnused;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataEdit;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataExportCSV;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataExportGeoJson;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataImportPage;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataList;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
use Fisharebest\Webtrees\Tree;
use Illuminate\Support\Collection;

/**
 * @var array<list<object{p_place:string,tree_name:string,tree_title:string,p_id:int}>> $active
 * @var array<array-key,Tree>                                                           $all_trees
 * @var array<string,string>                                                            $breadcrumbs
 * @var int                                                                             $parent_id
 * @var Collection<int,object{
 *     id:int,
 *     key:string,
 *     place:string,
 *     latitude:float|null,
 *     longitude:float|null,
 *     child_count:int,
 *     no_coord:int}> $placelist
 * @var PlaceHierarchyListModule|null                                                   $list_module
 * @var string                                                                          $title
 */

?>

<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>

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

<p>
    <label>
        <input id="hide-unused-locations" type="checkbox" data-bs-toggle="collapse" data-bs-target=".unused-location" data-wt-persist="hide-unused-locations" autocomplete="off">
        <?= I18N::translate('Hide unused locations') ?>
    </label>
</p>

<table class="table table-bordered table-striped table-sm table-hover wt-table-locations">
    <thead class="thead-dark">
        <tr>
            <th><?= I18N::translate('Place') ?></th>
            <th><?= I18N::translate('Latitude') ?></th>
            <th><?= I18N::translate('Longitude') ?></th>
            <!--
            <th><?= I18N::translate('Flag') ?> </th>
            -->
            <th><?= I18N::translate('Edit') ?></th>
            <th><?= I18N::translate('Facts and events') ?></th>
            <th><?= I18N::translate('Delete') ?></th>
        </tr>
    </thead>

    <tbody>
    <?php foreach ($placelist as $place) : ?>
        <tr class="<?= $active[$place->key] ?? null ? '' : 'unused-location collapse show' ?>">
            <th scope="row">
                <a href="<?= e(route(MapDataList::class, ['parent_id' => $place->id])) ?>">
                    <bdi><?= e($place->place) ?></bdi>
                    <?php if ($place->no_coord > 0) : ?>
                        <span class="badge bg-secondary rounded-pill">
                            <?= view('icons/warning') ?>
                            <?= I18N::number($place->no_coord) ?>
                            /
                            <?= I18N::number($place->child_count) ?>
                        </span>
                    <?php elseif ($place->child_count > 0) : ?>
                        <span class="badge bg-secondary rounded-pill">
                            <?= I18N::number($place->child_count) ?>
                        </span>
                    <?php endif ?>
                </a>
            </th>

            <td dir="ltr">
                <?php if ($place->latitude === null) : ?>
                    <?= view('icons/warning') ?>
                <?php else : ?>
                    <?= $place->latitude ?>
                <?php endif ?>
            </td>

            <td dir="ltr">
                <?php if ($place->longitude === null) : ?>
                    <?= view('icons/warning') ?>
                <?php else : ?>
                    <?= $place->longitude ?>
                <?php endif ?>
            </td>

            <td>
                <a class="btn btn-primary" href="<?= e(route(MapDataEdit::class, ['location_id'  => $place->id])) ?>" title="<?= I18N::translate('Edit') ?>">
                    <?= view('icons/edit') ?>
                    <span class="visually-hidden">
                        <?= I18N::translate('Edit') ?>
                    </span>
                </a>
            </td>

            <td>
                <?php if ($list_module !== null && array_key_exists($place->key, $active)) : ?>
                    <?php if (count($active[$place->key]) === 1) : ?>
                        <a class="btn btn-link" href="<?= e($list_module->listUrl($all_trees[$active[$place->key][0]->tree_name], ['place_id' => $active[$place->key][0]->p_id, 'action2' => 'hierarchy-e'])) ?>">
                            <?= e($active[$place->key][0]->tree_title) ?>
                        </a>
                    <?php else : ?>
                        <div class="dropdown">
                            <a class="btn btn-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <?= I18N::plural('%s family tree', '%s family trees', count($active[$place->key]), I18N::number(count($active[$place->key]))) ?>
                            </a>

                            <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                                <?php foreach ($active[$place->key] as $n => $link) : ?>
                                    <a class="dropdown-item" href="<?= e($list_module->listUrl($all_trees[$active[$place->key][$n]->tree_name], ['place_id' => $link->p_id])) ?>">
                                        <?= e($link->tree_title) ?>
                                    </a>
                                <?php endforeach ?>
                            </div>
                        </div>
                    <?php endif ?>
                <?php endif ?>
            </td>

            <td>
                <?php if (!array_key_exists($place->key, $active)) : ?>
                    <form method="post" action="<?= e(route(MapDataDelete::class, ['location_id' => $place->id])) ?>">
                        <button type="submit" class="btn btn-danger" aria-label="<?= I18N::translate('delete') ?>" data-wt-confirm="<?= I18N::translate('Remove this location?') ?>">
                            <?= view('icons/delete') ?>
                        </button>

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

    <tfoot>
        <tr>
            <td colspan="4">
                <a class="btn btn-primary" href="<?= e(route(MapDataAdd::class, ['parent_id' => $parent_id])) ?>">
                    <?= view('icons/add') ?>
                    <?= /* I18N: A button label. */
                    I18N::translate('add place') ?>
                </a>
                <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton"
                        data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <?= view('icons/download') ?>
                    <?= /* I18N: A button label. */
                    I18N::translate('export file') ?>
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                    <a class="dropdown-item" href="<?= e(route(MapDataExportCSV::class, ['parent_id' => $parent_id])) ?>">
                        csv
                    </a>
                    <a class="dropdown-item" href="<?= e(route(MapDataExportGeoJson::class, ['parent_id' => $parent_id])) ?>">
                        geoJSON
                    </a>
                </div>
                <a class="btn btn-primary" href="<?= e(route(MapDataImportPage::class, ['parent_id' => $parent_id])) ?>">
                    <?= view('icons/upload') ?>
                    <?= /* I18N: A button label. */
                    I18N::translate('import file') ?>
                </a>
            </td>
            <td colspan="2">
                <?php if (count($active) !== count($placelist)) : ?>
                    <form class="d-inline" method="post" action="<?= route(MapDataDeleteUnused::class) ?>">
                        <button type="submit" class="btn btn-danger">
                            <?= view('icons/delete') ?>
                            <?= I18N::translate('Delete unused locations') ?>
                        </button>

                        <?= csrf_field() ?>
                    </form>
                <?php endif ?>
            </td>
        </tr>
    </tfoot>
</table>
