<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Http\RequestHandlers\AddChildToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\AddParentToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\AddSpouseToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\LinkChildToFamilyPage;
use Fisharebest\Webtrees\Http\RequestHandlers\LinkSpouseToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\ReorderFamiliesPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Services\RelationshipService;
use Illuminate\Support\Collection;

/**
 * @var bool                   $can_edit
 * @var int                    $fam_access_level
 * @var Individual             $individual
 * @var Collection<int,Family> $parent_families
 * @var RelationshipService    $relationship_service
 * @var Collection<int,Family> $spouse_families
 * @var Collection<int,Family> $step_child_families
 * @var Collection<int,Family> $step_parent_families
 */

?>
<?php
?>

<div class="wt-tab-relatives">
    <table class="table table-sm wt-facts-table" role="presentation">
    <tbody>
        <tr>
            <td>
                <label>
                    <input id="show-date-differences" type="checkbox" data-bs-toggle="collapse" data-bs-target=".wt-date-difference" data-wt-persist="date-differences" autocomplete="off">
                    <?= I18N::translate('Date differences') ?>
                </label>
            </td>
        </tr>
    </tbody>
</table>

<!-- Parents -->
<?php foreach ($parent_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'           => $individual,
        'family'               => $family,
        'type'                 => 'FAMC',
        'label'                => $individual->getChildFamilyLabel($family),
        'fam_access_level'     => $fam_access_level,
        'relationship_service' => $relationship_service,
    ]) ?>
<?php endforeach ?>

<!-- step-parents -->
<?php foreach ($step_parent_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'           => $individual,
        'family'               => $family,
        'type'                 => 'FAMC',
        'label'                => $individual->getStepFamilyLabel($family),
        'fam_access_level'     => $fam_access_level,
        'relationship_service' => $relationship_service,
    ]) ?>
<?php endforeach ?>

<!-- spouses -->
<?php foreach ($spouse_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'           => $individual,
        'family'               => $family,
        'type'                 => 'FAMS',
        'label'                => $individual->getSpouseFamilyLabel($family),
        'fam_access_level'     => $fam_access_level,
        'relationship_service' => $relationship_service,
    ]) ?>
<?php endforeach ?>

<!-- step-children -->
<?php foreach ($step_child_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'           => $individual,
        'family'               => $family,
        'type'                 => 'FAMS',
        'label'                => $family->fullName(),
        'fam_access_level'     => $fam_access_level,
        'relationship_service' => $relationship_service,
    ]) ?>
<?php endforeach ?>

<?php if ($can_edit) : ?>
    <br>
    <table class="table table-sm wt-facts-table" role="presentation">
        <tbody>
            <?php if ($spouse_families->count() > 1) : ?>
                <tr>
                    <td>
                        <a href="<?= e(route(ReorderFamiliesPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                            <?= view('icons/reorder') ?>
                            <?= I18N::translate('Re-order families') ?>
                        </a>
                    </td>
                </tr>
            <?php endif ?>

            <?php if ($parent_families->isEmpty()) : ?>
                <tr>
                    <td>
                        <a href="<?= e(route(AddParentToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'sex' => 'M', 'url' => $individual->url() . '#tab-relatives'])) ?>">
                            <?= I18N::translate('Add a father') ?>
                        </a>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="<?= e(route(AddParentToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'sex' => 'F', 'url' => $individual->url() . '#tab-relatives'])) ?>">
                            <?= I18N::translate('Add a mother') ?>
                        </a>
                    </td>
                </tr>
            <?php endif ?>

            <tr>
                <td>
                    <a href="<?= e(route(LinkChildToFamilyPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                        <?= I18N::translate('Link this individual to an existing family as a child') ?>
                    </a>
                </td>
            </tr>

            <tr>
                <td>
                    <a href="<?= e(route(AddSpouseToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'url' => $individual->url() . '#tab-relatives'])) ?>">
                        <?php if ($individual->sex() !== 'F') : ?>
                            <?= I18N::translate('Add a wife') ?>
                        <?php else : ?>
                            <?= I18N::translate('Add a husband') ?>
                        <?php endif ?>
                    </a>
                </td>
            </tr>

            <tr>
                <td>
                    <a href="<?= e(route(LinkSpouseToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                        <?php if ($individual->sex() !== 'F') : ?>
                            <?= I18N::translate('Add a wife using an existing individual') ?>
                        <?php else : ?>
                            <?= I18N::translate('Add a husband using an existing individual') ?>
                        <?php endif ?>
                    </a>
                </td>
            </tr>

            <tr>
                <td>
                    <a href="<?= e(route(AddChildToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'sex' => 'U'])) ?>">
                        <?= I18N::translate('Add a child to create a one-parent family') ?>
                    </a>
                </td>
            </tr>
        </tbody>
    </table>
<?php endif ?>
</div>
