<?php use Fisharebest\Webtrees\I18N; ?>
<?php use Fisharebest\Webtrees\Auth; ?>
<?php use Fisharebest\Webtrees\Module\IndividualListModule; ?>
<?php use Fisharebest\Webtrees\Module\FamilyListModule; ?>
<?php use Fisharebest\Webtrees\Module\MediaListModule; ?>
<?php use Fisharebest\Webtrees\Module\ModuleInterface; ?>
<?php use Fisharebest\Webtrees\Module\ModuleListInterface;
use Fisharebest\Webtrees\Module\NoteListModule; ?>
<?php use Fisharebest\Webtrees\Module\RepositoryListModule; ?>
<?php use Fisharebest\Webtrees\Module\SourceListModule; ?>
<?php use Fisharebest\Webtrees\Services\ModuleService; ?>

<?php foreach ($all_trees as $tree) : ?>
    <tr class="<?= $changes[$tree->id()] ? 'danger' : '' ?>">
        <th scope="row">
            <a href="<?= e(route('tree-page', ['ged' => $tree->name()])) ?>">
                <?= e($tree->name()) ?>
                -
                <?= e($tree->title()) ?>
            </a>
        </th>
        <td>
            <a href="<?= e(route('admin-trees', ['ged' => $tree->name()])) ?>" title="<?= I18N::translate('Preferences') ?>">
                <?= view('icons/preferences') ?>
            </a>
        </td>
        <td class="text-right">
            <?php if ($changes[$tree->id()]) : ?>
                <a href="<?= e(route('show-pending', ['ged' => $tree->name(), 'url' => route('admin-control-panel')])) ?>">
                    <?= I18N::number($changes[$tree->id()]) ?>
                    <span class="sr-only"><?= I18N::translate('Pending changes') ?> <?= e($tree->title()) ?></span>
                </a>
            <?php else : ?>
                -
            <?php endif ?>
        </td>
        <td class="d-none d-sm-table-cell text-right">
            <?php if ($individuals[$tree->id()]) : ?>
                <?php
                  //find a module providing individual lists
                  $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
                      return $module instanceof IndividualListModule;
                  });

                  if ($module instanceof IndividualListModule) {
                    echo '<a href="'.e($module->listUrl($tree)).'">';
                  } else {
                    echo '<a>';
                  }            
                ?>
                    <?= I18N::number($individuals[$tree->id()]) ?>
                </a>
            <?php else : ?>
                -
            <?php endif ?>
        </td>
        <td class="d-none d-lg-table-cell text-right">
            <?php if ($families[$tree->id()]) : ?>
                <?php
                  //find a module providing family lists
                  $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
                      return $module instanceof FamilyListModule;
                  });

                  if ($module instanceof FamilyListModule) {
                    echo '<a href="'.e($module->listUrl($tree)).'">';
                  } else {
                    echo '<a>';
                  }            
                ?>
                    <?= I18N::number($families[$tree->id()]) ?>
                </a>
            <?php else : ?>
                -
            <?php endif ?>
        </td>
        <td class="d-none d-sm-table-cell text-right">
            <?php if ($sources[$tree->id()]) : ?>
                <?php
                  //find a module providing source lists
                  $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
                      return $module instanceof SourceListModule;
                  });

                  if ($module instanceof SourceListModule) {
                    echo '<a href="'.e($module->listUrl($tree)).'">';
                  } else {
                    echo '<a>';
                  }            
                ?>
                    <?= I18N::number($sources[$tree->id()]) ?>
                </a>
            <?php else : ?>
                -
            <?php endif ?>
        </td>
        <td class="d-none d-lg-table-cell text-right">
            <?php if ($repositories[$tree->id()]) : ?>
                <?php
                  //find a module providing repository lists
                  $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
                      return $module instanceof RepositoryListModule;
                  });

                  if ($module instanceof RepositoryListModule) {
                    echo '<a href="'.e($module->listUrl($tree)).'">';
                  } else {
                    echo '<a>';
                  }            
                ?>
                    <?= I18N::number($repositories[$tree->id()]) ?>
                </a>
            <?php else : ?>
                -
            <?php endif ?>
        </td>
        <td class="d-none d-sm-table-cell text-right">
            <?php if ($media[$tree->id()]) : ?>
                <?php
                  //find a module providing media lists
                  $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
                      return $module instanceof MediaListModule;
                  });

                  if ($module instanceof MediaListModule) {
                    echo '<a href="'.e($module->listUrl($tree)).'">';
                  } else {
                    echo '<a>';
                  }            
                ?>
                    <?= I18N::number($media[$tree->id()]) ?>
                </a>
            <?php else : ?>
                -
            <?php endif ?>
        </td>
        <td class="d-none d-lg-table-cell text-right">
            <?php if ($notes[$tree->id()]) : ?>
                <?php
                  //find a module providing note lists
                  $module = app(ModuleService::class)->findByComponent(ModuleListInterface::class, $tree, Auth::user())->first(function (ModuleInterface $module) {
                      return $module instanceof NoteListModule;
                  });

                  if ($module instanceof NoteListModule) {
                    echo '<a href="'.e($module->listUrl($tree)).'">';
                  } else {
                    echo '<a>';
                  }            
                ?>
                    <?= I18N::number($media[$tree->id()]) ?>
                </a>
            <?php else : ?>
                -
            <?php endif ?>
        </td>
    </tr>
<?php endforeach ?>
