<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Location;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Submitter;
use Fisharebest\Webtrees\Timestamp;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;

/**
 * @var int                                                                           $limit_high
 * @var int                                                                           $limit_low
 * @var array<array<int,string>>                                                      $order
 * @var Collection<int,object{record:GedcomRecord,time:Timestamp,user:UserInterface}> $rows
 * @var bool                                                                          $show_date
 * @var bool                                                                          $show_user
 */
?>

<div class="table-responsive">
    <table
        class="table table-sm wt-table-changes datatables d-none"
        <?= view('lists/datatables-attributes') ?>
        data-paging="<?= $rows->count() >= $limit_high ? 'true' : 'false' ?>"
        data-filter="false"
        data-info="false"
        data-length-change="false"
        data-order="<?= e(json_encode($order, JSON_THROW_ON_ERROR)) ?>"
        data-page-length="<?= e($limit_low) ?>"
    >
        <thead>
            <tr>
                <th class="wt-side-block-optional">
                <span class="visually-hidden">
                    <?= I18N::translate('Type') ?>
                </span>
                </th>
                <th>
                    <?= I18N::translate('Record') ?>
                </th>
                <th class="<?= $show_date ? '' : 'd-none' ?>">
                    <?= I18N::translate('Last change') ?>
                </th>
                <th class="<?= $show_user ? '' : 'd-none' ?>">
                    <?= I18N::translate('Editor') ?>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row) : ?>
                <tr>
                    <td data-sort="<?= $row->record->tag() ?>" class="text-centre wt-side-block-optional">
                        <?php if ($row->record->tag() === Individual::RECORD_TYPE) : ?>
                            <?= view('icons/individual') ?>
                            <span class="visually-hidden"><?= I18N::translate('Individual') ?></span>
                        <?php elseif ($row->record->tag() === Family::RECORD_TYPE) : ?>
                            <?= view('icons/family') ?>
                            <span class="visually-hidden"><?= I18N::translate('Family') ?></span>
                        <?php elseif ($row->record->tag() === Media::RECORD_TYPE) : ?>
                            <?= view('icons/media') ?>
                            <span class="visually-hidden"><?= I18N::translate('Media') ?></span>
                        <?php elseif ($row->record->tag() === Note::RECORD_TYPE) : ?>
                            <?= view('icons/note') ?>
                            <span class="visually-hidden"><?= I18N::translate('Note') ?></span>
                        <?php elseif ($row->record->tag() === Source::RECORD_TYPE) : ?>
                            <?= view('icons/source') ?>
                            <span class="visually-hidden"><?= I18N::translate('Source') ?></span>
                        <?php elseif ($row->record->tag() === Submitter::RECORD_TYPE) : ?>
                            <?= view('icons/submitter') ?>
                            <span class="visually-hidden"><?= I18N::translate('Submitter') ?></span>
                        <?php elseif ($row->record->tag() === Repository::RECORD_TYPE) : ?>
                            <?= view('icons/repository') ?>
                            <span class="visually-hidden"><?= I18N::translate('Repository') ?></span>
                        <?php elseif ($row->record->tag() === Location::RECORD_TYPE) : ?>
                            <?= view('icons/location') ?>
                            <span class="visually-hidden"><?= I18N::translate('Location') ?></span>
                        <?php endif ?>
                    </td>

                    <td data-sort="<?= e($row->record->sortName()) ?>">
                        <a href="<?= e($row->record->url()) ?>"><?= $row->record->fullName() ?></a>
                    </td>

                    <td data-sort="<?= sprintf('x%07d', $row->time->timestamp()) ?>" class="<?= $show_date ? '' : 'd-none' ?>">
                        <?= view('components/datetime', ['timestamp' => $row->time]) ?>
                    </td>

                    <td class="<?= $show_user ? '' : 'd-none' ?>">
                        <?= e($row->user->userName()) ?>
                    </td>
                </tr>
            <?php endforeach ?>
        </tbody>
    </table>
</div>

<?php View::push('javascript') ?>
<script>
  $(".wt-table-changes").dataTable().removeClass("d-none");
</script>
<?php View::endpush() ?>
