<?php

use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
use Fisharebest\Webtrees\Http\RequestHandlers\AddNewFact;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;

/**
 * @var bool             $can_edit
 * @var Collection<Fact> $clipboard_facts
 * @var Collection<Fact> $facts
 * @var Individual       $individual
 */

?>

<div class="wt-tab-sources py-4">
    <table class="table wt-facts-table">
        <tr>
            <td colspan="2">
                <label>
                    <input id="show-level-2-sources" type="checkbox" data-toggle="collapse" data-target=".wt-level-two-source">
                    <?= I18N::translate('Show all sources') ?>
                </label>
            </td>
        </tr>

        <?php foreach ($facts as $fact) : ?>
            <?php FunctionsPrintFacts::printMainSources($fact, 1) ?>
            <?php FunctionsPrintFacts::printMainSources($fact, 2) ?>
        <?php endforeach ?>

        <?php if ($facts->isEmpty()) : ?>
            <tr>
                <td colspan="2">
                    <?= I18N::translate('There are no source citations for this individual.') ?>
                    </td>
            </tr>
        <?php endif ?>

        <?php if ($can_edit) : ?>
            <?= view('edit/paste-fact-row', ['record' => $individual, 'facts' => $clipboard_facts]) ?>

            <tr>
                <th scope="row">
                    <?= I18N::translate('Source') ?>
                </th>
                <td>
                    <a href="<?= e(route(AddNewFact::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'fact' => 'SOUR'])) ?>">
                        <?= I18N::translate('Add a source citation') ?>
                    </a>
                </td>
            </tr>
        <?php endif ?>
    </table>
</div>

<?php View::push('javascript') ?>
<script>
  'use strict';

  webtrees.persistentToggle("show-level-2-sources");
</script>
<?php View::endpush() ?>
