<?php

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Http\RequestHandlers\AddNewFact;
use Fisharebest\Webtrees\Http\RequestHandlers\DeleteRecord;
use Fisharebest\Webtrees\Http\RequestHandlers\EditFact;
use Fisharebest\Webtrees\Http\RequestHandlers\EditRawRecordPage;
use Fisharebest\Webtrees\Http\RequestHandlers\ReorderMediaPage;
use Fisharebest\Webtrees\Http\RequestHandlers\ReorderNamesPage;
use Fisharebest\Webtrees\Http\RequestHandlers\ReorderFamiliesPage;
use Fisharebest\Webtrees\I18N;

?>

<div class="dropdown wt-page-menu">
    <button class="btn btn-primary dropdown-toggle wt-page-menu-button" type="button" id="page-menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <?= view('icons/edit') ?>
        <?= I18N::translate('edit') ?>
    </button>

    <div class="dropdown-menu dropdown-menu-right wt-page-menu-items" aria-labelledby="page-menu">
        <a class="dropdown-item" href="<?= e(route('add-name', ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
            <?= view('icons/add') ?>
            <?= I18N::translate('Add a name') ?>
        </a>

        <?php if ($individual->facts(['NAME'], false, null, true)->count() > 1) : ?>
            <a class="dropdown-item" href="<?= e(route(ReorderNamesPage::class, ['tree' => $tree->name(), 'xref' => $individual->xref()])) ?>">
                <?= view('icons/reorder') ?>
                <?= I18N::translate('Re-order names') ?>
            </a>
        <?php else : ?>
            <div class="dropdown-item disabled">
                <?= view('icons/spacer') ?>
                <?= I18N::translate('Re-order names') ?>
            </div>
        <?php endif ?>

        <div class="dropdown-divider"></div>

        <?php if ($individual->facts(['SEX'], false, null, true)->isEmpty()) : ?>
            <a class="dropdown-item" href="<?= e(route(AddNewFact::class, ['fact' => 'SEX', 'tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                <?= view('icons/edit') ?>
                <?= I18N::translate('Edit the gender') ?>
            </a>
        <?php endif ?>

        <?php foreach ($individual->facts(['SEX'], false, null, true) as $fact) : ?>
            <?php if ($fact->canEdit()) : ?>
                <a class="dropdown-item" href="<?= e(route(EditFact::class, ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) ?>">
                    <?= view('icons/edit') ?>
                    <?= I18N::translate('Edit the gender') ?>
                </a>
            <?php endif ?>
        <?php endforeach ?>

        <div class="dropdown-divider"></div>

        <?php if ($individual->spouseFamilies()->count() > 1 || $individual->childFamilies()->count() > 1) : ?>
            <a class="dropdown-item" href="<?= e(route(ReorderFamiliesPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                <?= view('icons/reorder') ?>
                <?= I18N::translate('Re-order families') ?>
            </a>
        <?php else : ?>
            <div class="dropdown-item disabled">
                <?= view('icons/spacer') ?>
                <?= I18N::translate('Re-order families') ?>
            </div>
        <?php endif ?>

        <div class="dropdown-divider"></div>

        <a class="dropdown-item" href="<?= e(route(AddNewFact::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'fact' => 'OBJE'])) ?>">
            <?= view('icons/add') ?>
            <?= I18N::translate('Add a media object') ?>
        </a>

        <?php if ($individual->facts(['OBJE'], false, null, true)->count() > 1) : ?>
            <a class="dropdown-item" href="<?= e(route(ReorderMediaPage::class, ['tree' => $tree->name(), 'xref' => $individual->xref()])) ?>">
                <?= view('icons/reorder') ?>
                <?= I18N::translate('Re-order media') ?>
            </a>
        <?php else : ?>
            <div class="dropdown-item disabled">
                <?= view('icons/spacer') ?>
                <?= I18N::translate('Re-order media') ?>
            </div>
        <?php endif ?>

        <div class="dropdown-divider"></div>

        <a class="dropdown-item" href="#" data-confirm="<?= I18N::translate('Are you sure you want to delete “%s”?', strip_tags($individual->fullName())) ?>" data-post-url="<?= e(route(DeleteRecord::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
            <?= view('icons/delete') ?>
            <?= I18N::translate('Delete') ?>
        </a>

        <?php if (Auth::isAdmin() || $individual->tree()->getPreference('SHOW_GEDCOM_RECORD')) : ?>
            <a class="dropdown-item" href="<?= e(route(EditRawRecordPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                <?= view('icons/edit') ?>
                <?= I18N::translate('Edit the raw GEDCOM') ?>
            </a>
        <?php endif ?>
    </div>
</div>
