<?php

use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Webtrees;
use Illuminate\Support\Collection;

/**
 * @var string     $latest_version
 * @var string     $title
 * @var Collection $server_errors
 * @var Collection $server_warnings
 */

?>

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

<!-- WEBSITE / WEBTREES -->
<div class="card mb-4">
    <div class="card-header">
        <h2 class="mb-0">
            <?= I18N::translate('Website') ?>
            <span class="badge badge-secondary">
                <?= e(Webtrees::VERSION) ?>
            </span>
        </h2>
    </div>
    <div class="card-body">
        <?php foreach ($server_errors as $server_error) : ?>
            <p class="alert alert-danger"><?= $server_error ?></p>
        <?php endforeach ?>

        <?php foreach ($server_warnings as $server_warning) : ?>
            <p class="alert alert-warning"><?= $server_warning ?></p>
        <?php endforeach ?>

        <p class="card-text">
            <?= /* I18N: %s is a URL/link to the project website */
            I18N::translate('Support and documentation can be found at %s.', '<a href="https://webtrees.net/">webtrees.net</a>') ?>
        </p>
        <p class="card-text ">
            <?php if ($latest_version === '') : ?>
                <?= I18N::translate('No upgrade information is available.') ?>
            <?php elseif (version_compare(Webtrees::VERSION, $latest_version) < 0) : ?>
                <?= I18N::translate('A new version of webtrees is available.') ?>
                <a href="<?= e(route('upgrade')) ?>" class="error">
                    <?= /* I18N: %s is a version number */
                    I18N::translate('Upgrade to webtrees %s.', e($latest_version)) ?>
                </a>
            <?php else : ?>
                <?= I18N::translate('This is the latest version of webtrees. No upgrade is available.') ?>
            <?php endif ?>
        </p>

        <div class="row">
            <div class="col-sm-6">
                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-site-preferences')) ?>">
                            <?= I18N::translate('Website preferences') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-site-mail')) ?>">
                            <?= I18N::translate('Sending email') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-site-registration')) ?>">
                            <?= I18N::translate('Sign-in and registration') ?>
                        </a>
                    </li>
                </ul>
            </div>
            <div class="col-sm-6">
                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-site-logs')) ?>">
                            <?= I18N::translate('Website logs') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-clean-data')) ?>">
                            <?= I18N::translate('Clean up data folder') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-site-information')) ?>">
                            <?= I18N::translate('Server information') ?>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

<!-- FAMILY TREES -->
<div class="card mb-4 <?= array_sum($changes) ? 'card-outline-danger' : '' ?>">
    <div class="card-header">
        <h2 class="mb-0">
            <?= I18N::translate('Family trees') ?>
            <span class="badge badge-secondary">
                    <?= I18N::number(count($all_trees)) ?>
                </span>
        </h2>
    </div>
    <div class="card-body">
        <table class="table table-sm">
            <caption class="sr-only">
                <?= I18N::translate('Family trees') ?>
            </caption>
            <thead>
                <tr>
                    <th><?= I18N::translate('Family tree') ?></th>
                    <th>
                        <span class="sr-only"><?= I18N::translate('Manage family trees') ?></span>
                    </th>
                    <th class="text-right"><?= I18N::translate('Pending changes') ?></th>
                    <th class="d-none d-sm-table-cell text-right"><?= I18N::translate('Individuals') ?></th>
                    <th class="d-none d-lg-table-cell text-right"><?= I18N::translate('Families') ?></th>
                    <th class="d-none d-sm-table-cell text-right"><?= I18N::translate('Sources') ?></th>
                    <th class="d-none d-lg-table-cell text-right"><?= I18N::translate('Repositories') ?></th>
                    <th class="d-none d-sm-table-cell text-right"><?= I18N::translate('Media') ?></th>
                    <th class="d-none d-lg-table-cell text-right"><?= I18N::translate('Notes') ?></th>
                </tr>
            </thead>
            <tbody>
                <?= view('admin/control-panel-tree-list', [
                    'all_trees'              => $all_trees,
                    'changes'                => $changes,
                    'families'               => $families,
                    'individuals'            => $individuals,
                    'media'                  => $media,
                    'notes'                  => $notes,
                    'repositories'           => $repositories,
                    'sources'                => $sources,
                    'family_list_module'     => $family_list_module,
                    'individual_list_module' => $individual_list_module,
                    'media_list_module'      => $media_list_module,
                    'note_list_module'       => $note_list_module,
                    'repository_list_module' => $repository_list_module,
                    'source_list_module'     => $source_list_module,
                ]) ?>
            </tbody>
            <tfoot>
                <tr>
                    <th scope="row">
                        <?= I18N::translate('Total') ?>
                    </th>
                    <td></td>
                    <td class="text-right">
                        <?= I18N::number(array_sum($changes)) ?>
                    </td>
                    <td class="d-none d-sm-table-cell text-right">
                        <?= I18N::number($individuals->sum()) ?>
                    </td>
                    <td class="d-none d-lg-table-cell text-right">
                        <?= I18N::number($families->sum()) ?>
                    </td>
                    <td class="d-none d-sm-table-cell text-right">
                        <?= I18N::number($sources->sum()) ?>
                    </td>
                    <td class="d-none d-lg-table-cell text-right">
                        <?= I18N::number($repositories->sum()) ?>
                    </td>
                    <td class="d-none d-sm-table-cell text-right">
                        <?= I18N::number($media->sum()) ?>
                    </td>
                    <td class="d-none d-lg-table-cell text-right">
                        <?= I18N::number($notes->sum()) ?>
                    </td>
                </tr>
            </tfoot>
        </table>

        <ul class="fa-ul">
            <li>
                <span class="fa-li"><?= view('icons/preferences') ?></span>
                <a href="<?= e(route('admin-trees')) ?>">
                    <?= I18N::translate('Manage family trees') ?>
                </a>
            </li>
            <li>
                <span class="fa-li"><?= view('icons/preferences') ?></span>
                <a href="<?= e(route('tree-page-default-edit')) ?>">
                    <?= I18N::translate('Set the default blocks for new family trees') ?>
                </a>
            </li>
            <?php if (count($all_trees) > 1) : ?>
                <li>
                    <span class="fa-li"><?= view('icons/preferences') ?></span>
                    <a href="<?= e(route('admin-trees-merge')) ?>">
                        <?= I18N::translate('Merge family trees') ?>
                    </a>
                </li>
            <?php endif ?>
        </ul>
    </div>
</div>

<!-- USERS -->
<div class="card mb-4 <?= $unapproved || $unverified ? 'card-outline-danger' : '' ?>">
    <div class="card-header">
        <h2 class="mb-0">
            <?= I18N::translate('Users') ?>
            <span class="badge badge-secondary">
                    <?= I18N::number(count($all_users)) ?>
                </span>
        </h2>
    </div>
    <div class="card-body">
        <dl class="row">
            <?php foreach ([I18N::translate('Administrators') => $administrators, I18N::translate('Managers') => $managers, I18N::translate('Moderators') => $moderators, I18N::translate('Not verified by the user') => $unverified, I18N::translate('Not approved by an administrator') => $unapproved] as $label => $list) : ?>
                <?php if (!empty($list)) : ?>
                    <dt class="col-sm-3">
                        <?= $label ?>
                    </dt>
                    <dd class="col-sm-9">
                        <?php foreach ($list as $n => $user) : ?>
                            <?= $n ? I18N::$list_separator : '' ?>
                            <a href="<?= e(route('admin-users-edit', ['user_id' => $user->id()])) ?>" dir="auto">
                                <?= e($user->realName()) ?>
                            </a>
                        <?php endforeach ?>
                    </dd>
                <?php endif ?>
            <?php endforeach ?>
        </dl>

        <div class="row">
            <div class="col-sm-6">
                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-users')) ?>">
                            <?= I18N::translate('User administration') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-users-create')) ?>">
                            <?= I18N::translate('Add a user') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('admin-users-cleanup')) ?>">
                            <?= I18N::translate('Delete inactive users') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/preferences') ?></span>
                        <a href="<?= e(route('user-page-default-edit')) ?>">
                            <?= I18N::translate('Set the default blocks for new users') ?>
                        </a>
                    </li>
                </ul>
            </div>
            <div class="col-sm-6">
                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/email') ?></span>
                        <a href="<?= e(route('broadcast', ['to' => 'all'])) ?>">
                            <?= I18N::translate('Send a message to all users') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/email') ?></span>
                        <a href="<?= e(route('broadcast', ['to' => 'never_logged'])) ?>">
                            <?= I18N::translate('Send a message to users who have never signed in') ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/email') ?></span>
                        <a href="<?= e(route('broadcast', ['to' => 'last_6mo'])) ?>">
                            <?= I18N::translate('Send a message to users who have not signed in for 6 months') ?>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

<!-- MODULES -->
<div class="card mb-4">
    <div class="card-header">
        <h2 class="mb-0">
            <?= I18N::translate('Modules') ?>
            <?= view('components/badge', ['count' => $all_modules_enabled->count(), 'total' => $all_modules_disabled->count(), 'context' => 'secondary']) ?>
        </h2>
    </div>

    <div class="card-body">
        <ul class="fa-ul">
            <li>
                <span class="fa-li"><?= view('icons/module') ?></span>
                <a href="<?= e(route('modules')) ?>">
                    <?= I18N::translate('All modules') ?>
                </a>
                <?= view('components/badge', ['count' => $all_modules_enabled->count(), 'total' => $all_modules_disabled->count(), 'context' => 'primary']) ?>
            </li>
        </ul>

        <div class="row">
            <div class="col-sm-4">
                <h3>
                    <?= I18N::translate('Genealogy') ?>
                </h3>

                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/menu') ?></span>
                        <a href="<?= e(route('menus')) ?>">
                            <?= I18N::translate('Menus') ?>
                        </a>
                        <?= view('components/badge', ['count' => $menu_modules_enabled->count(), 'total' => $menu_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/chart') ?></span>
                        <a href="<?= e(route('charts')) ?>">
                            <?= I18N::translate('Charts') ?>
                        </a>
                        <?= view('components/badge', ['count' => $chart_modules_enabled->count(), 'total' => $chart_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/list') ?></span>
                        <a href="<?= e(route('lists')) ?>">
                            <?= I18N::translate('Lists') ?>
                        </a>
                        <?= view('components/badge', ['count' => $list_modules_enabled->count(), 'total' => $list_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/report') ?></span>
                        <a href="<?= e(route('reports')) ?>">
                            <?= I18N::translate('Reports') ?>
                        </a>
                        <?= view('components/badge', ['count' => $report_modules_enabled->count(), 'total' => $report_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                </ul>

                <h3>
                    <?= I18N::translate('Website') ?>
                </h3>

                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/footer') ?></span>
                        <a href="<?= e(route('footers')) ?>">
                            <?= I18N::translate('Footers') ?>
                        </a>
                        <?= view('components/badge', ['count' => $footer_modules_enabled->count(), 'total' => $footer_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/analytics') ?></span>
                        <a href="<?= e(route('analytics')) ?>">
                            <?= I18N::translate('Tracking and analytics') ?>
                        </a>
                        <?= view('components/badge', ['count' => $analytics_modules_enabled->count(), 'total' => $analytics_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/theme') ?></span>
                        <a href="<?= e(route('themes')) ?>">
                            <?= I18N::translate('Themes') ?>
                        </a>
                        <?= view('components/badge', ['count' => $theme_modules_enabled->count(), 'total' => $theme_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/language') ?></span>
                        <a href="<?= e(route('languages')) ?>">
                            <?= I18N::translate('Languages') ?>
                        </a>
                        <?= view('components/badge', ['count' => $language_modules_enabled->count(), 'total' => $language_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                </ul>

            </div>

            <div class="col-sm-4">

                <h3>
                    <?= I18N::translate('Home page') ?>
                </h3>

                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/block') ?></span>
                        <a href="<?= e(route('blocks')) ?>">
                            <?= I18N::translate('Blocks') ?>
                        </a>
                        <?= view('components/badge', ['count' => $block_modules_enabled->count(), 'total' => $block_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                </ul>

                <h3>
                    <?= I18N::translate('Individual page') ?>
                </h3>

                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/tab') ?></span>
                        <a href="<?= e(route('tabs')) ?>">
                            <?= I18N::translate('Tabs') ?>
                            <?= view('components/badge', ['count' => $tab_modules_enabled->count(), 'total' => $tab_modules_disabled->count(), 'context' => 'primary']) ?>
                        </a>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/sidebar') ?></span>
                        <a href="<?= e(route('sidebars')) ?>">
                            <?= I18N::translate('Sidebars') ?>
                        </a>
                        <?= view('components/badge', ['count' => $sidebar_modules_enabled->count(), 'total' => $sidebar_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                    <li>
                        <span class="fa-li"><?= view('icons/history') ?></span>
                        <a href="<?= e(route('history')) ?>">
                            <?= I18N::translate('Historic events') ?>
                        </a>
                        <?= view('components/badge', ['count' => $history_modules_enabled->count(), 'total' => $history_modules_disabled->count(), 'context' => 'primary']) ?>
                    </li>
                </ul>

            </div>

            <div class="col-sm-4">

                <h3>
                    <?= I18N::translate('Other') ?>
                </h3>

                <ul class="fa-ul">
                    <?php foreach ($other_modules as $module) : ?>
                        <li>
                            <span class="fa-li"><?= view('icons/module') ?></span>

                            <?php if ($module->isEnabled()): ?>
                                <?php if ($module instanceof ModuleConfigInterface): ?>
                                    <a href="<?= e($module->getConfigLink()) ?>">
                                    <?= $module->title() ?>
                                    <span class="fa"><?= view('icons/preferences') ?></span>
                                    </a>
                                <?php else : ?>
                                    <?= $module->title() ?>
                                <?php endif ?>
                            <?php else : ?>
                                <span class="text-muted">
                                    <span class="fa-li"><?= view('icons/module') ?></span>
                                     <?= $module->title() ?>
                                    <?php if ($module instanceof ModuleConfigInterface): ?>
                                        <span class="fa"><?= view('icons/preferences') ?></span>
                                    <?php endif ?>
                                    </span>
                                </span>
                            <?php endif ?>

                            <?php if ($module instanceof ModuleCustomInterface): ?>
                                <?= view('admin/custom-module-info', ['module' => $module]) ?>
                            <?php endif ?>
                        </li>
                    <?php endforeach ?>
                </ul>
            </div>
        </div>
    </div>
</div>

<!-- MEDIA -->
<div class="card mb-4">
    <div class="card-header">
        <h2 class="mb-0">
            <?= I18N::translate('Media') ?>
        </h2>
    </div>
    <div class="card-body">
        <ul class="fa-ul">
            <li>
                <span class="fa-li"><?= view('icons/preferences') ?></span>
                <a href="<?= e(route('admin-media')) ?>">
                    <?= I18N::translate('Manage media') ?>
                </a>
            </li>
            <li>
                <span class="fa-li"><?= view('icons/preferences') ?></span>
                <a href="<?= e(route('admin-media-upload')) ?>">
                    <?= I18N::translate('Upload media files') ?>
                </a>
            </li>
            <li>
                <span class="fa-li"><?= view('icons/preferences') ?></span>
                <a href="<?= e(route('admin-fix-level-0-media')) ?>">
                    <?= I18N::translate('Link media objects to facts and events') ?>
                </a>
            </li>
            <li>
                <span class="fa-li"><?= view('icons/preferences') ?></span>
                <a href="<?= e(route('admin-webtrees1-thumbs')) ?>">
                    <?= I18N::translate('Import custom thumbnails from webtrees version 1') ?>
                </a>
            </li>
        </ul>
    </div>
</div>

<!-- MAPS -->
<div class="card mb-4">
    <div class="card-header">
        <h2 class="mb-0">
            <?= I18N::translate('Map') ?>
        </h2>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-sm-6">
                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/location') ?></span>
                        <a href="<?= e(route('map-data')) ?>">
                            <?= I18N::translate('Geographic data') ?>
                        </a>
                    </li>
                </ul>
            </div>
            <div class="col-sm-6">
                <ul class="fa-ul">
                    <li>
                        <span class="fa-li"><?= view('icons/map') ?></span>
                        <a href="<?= e(route('map-provider')) ?>">
                            <?= I18N::translate('Map provider') ?>
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

<!-- OLD FILES -->
<?php if (!empty($files_to_delete)) : ?>
    <div class="card mb-4 card-outline-danger">
        <div class="card-header">
            <h2 class="mb-0">
                <?= I18N::translate('Old files found') ?>
            </h2>
        </div>
        <div class="card-body">
            <p>
                <?= I18N::translate('Files have been found from a previous version of webtrees. Old files can sometimes be a security risk. You should delete them.') ?>
            </p>
            <ul class="list-unstyled">
                <?php foreach ($files_to_delete as $file_to_delete) : ?>
                    <li dir="ltr">
                        <code><?= e($file_to_delete) ?></code>
                    </li>
                <?php endforeach ?>
            </ul>
        </div>
    </div>
<?php endif ?>
