<?php use Fisharebest\Webtrees\Functions\Functions; ?>
<?php use Fisharebest\Webtrees\I18N; ?>

<table class="table table-sm wt-facts-table wt-family-navigator-family">
    <caption class="text-center wt-family-navigator-family-heading">
        <a href="<?= e($family->url()) ?>">
            <?= $title ?>
        </a>
    </caption>
    <tbody>
        <?php foreach ($family->spouses() as $spouse) : ?>
            <tr class="text-center wt-family-navigator-parent wt-gender-<?= $spouse->sex() ?>">
                <th class="text-nowrap align-middle wt-family-navigator-label" scope="row">
                    <?php if ($spouse === $individual) : ?>
                        <?= Functions::getCloseRelationshipName($individual, $spouse) ?>
                        <i class="icon-selected"></i>
                    <?php elseif ($spouse->primaryChildFamily() !== null) : ?>
                    <div class="dropdown">
                        <a class="dropdown-toggle" href="#" role="button" id="dropdown-<?= e($spouse->xref()) ?>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <?= Functions::getCloseRelationshipName($individual, $spouse) ?>
                        </a>

                        <div class="dropdown-menu wt-family-navigator-dropdown">
                            <div class="dropdown-header wt-family-navigator-dropdown-heading">
                                <?= I18N::translate('Parents') ?>
                            </div>
                            <?php foreach ($spouse->primaryChildFamily()->spouses() as $parent) : ?>
                                <a class="dropdown-item" href="<?= e($parent->url()) ?>">
                                    <?= $parent->fullName() ?>
                                </a>
                            <?php endforeach ?>
                        </div>
                    </div>
                    <?php else : ?>
                        <?= Functions::getCloseRelationshipName($individual, $spouse) ?>
                    <?php endif ?>
                </th>

                <td class="wt-family-navigator-name">
                    <?php if ($spouse->canShow()) : ?>
                        <a href="<?= e($spouse->url()) ?>">
                            <?= $spouse->fullName() ?>
                        </a>
                        <div class="small">
                            <?= $spouse->getLifeSpan() ?>
                        </div>
                    <?php else : ?>
                        <?= $spouse->fullName() ?>
                    <?php endif ?>
                </td>
            </tr>
        <?php endforeach ?>

        <?php foreach ($family->children() as $child) : ?>
            <tr class="text-center wt-family-navigator-child wt-gender-<?= $child->sex() ?>">
                <th class="text-nowrap align-middle" scope="row">
                    <?php if ($child === $individual) : ?>
                        <?= Functions::getCloseRelationshipName($individual, $child) ?>
                        <i class="icon-selected"></i>
                    <?php elseif ($child->spouseFamilies() !== []) : ?>
                        <div class="dropdown">
                            <a class="dropdown-toggle" href="#" role="button" id="dropdown-<?= e($child->xref()) ?>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <?= Functions::getCloseRelationshipName($individual, $child) ?>
                            </a>

                            <div class="dropdown-menu">
                                <?php foreach ($child->spouseFamilies() as $n => $in_laws) : ?>
                                    <?php if ($n > 0) : ?>
                                        <div class="dropdown-divider"></div>
                                    <?php endif ?>
                                    <div class="dropdown-header wt-family-navigator-dropdown-heading">
                                        <?= I18N::translate('Family') ?>
                                    </div>

                                    <?php foreach ($in_laws->spouses() as $sibling_in_law) : ?>
                                        <?php if ($sibling_in_law !== $child) : ?>
                                            <a class="dropdown-item" href="<?= e($sibling_in_law->url()) ?>">
                                                <?= $sibling_in_law->fullName() ?>
                                            </a>
                                        <?php endif ?>
                                    <?php endforeach ?>

                                <ul>
                                    <?php foreach ($in_laws->children() as $child_in_law) : ?>
                                    <li>
                                        <a class="dropdown-item" href="<?= e($child_in_law->url()) ?>">
                                            <?= $child_in_law->fullName() ?>
                                        </a>
                                    </li>
                                    <?php endforeach ?>
                                </ul>
                                <?php endforeach ?>
                            </div>
                        </div>
                    <?php else : ?>
                        <?= Functions::getCloseRelationshipName($individual, $child) ?>
                    <?php endif ?>
                </th>

                <td>
                    <?php if ($child->canShow()) : ?>
                        <a  href="<?= e($child->url()) ?>">
                            <?= $child->fullName() ?>
                        </a>
                        <div class="small">
                            <?= $child->getLifeSpan() ?>
                        </div>
                    <?php else : ?>
                        <?= $child->fullName() ?>
                    <?php endif ?>
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>
