<?php

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Config;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Functions\FunctionsEdit;
use Fisharebest\Webtrees\Http\RequestHandlers\EditFactAction;
use Fisharebest\Webtrees\Http\RequestHandlers\EditRawFactPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Tree;
use Ramsey\Uuid\Uuid;

/**
 * @var bool   $can_edit_raw
 * @var Fact   $fact
 * @var string $title
 * @var Tree   $tree
 * @var string $url
 */

?>

<h2 class="wt-page-title"><?= $title ?></h2>

<form method="post" action="<?= e(route(EditFactAction::class, ['tree' => $tree->name(), 'xref' => $fact->record()->xref(), 'fact_id' => $fact->id()])) ?>" class="wt-page-content">
    <?= csrf_field() ?>
    <input type="hidden" name="url" value="<?= e($url) ?>">

    <?php FunctionsEdit::createEditForm($fact) ?>

    <?php
    $level1type = $fact->getTag();
    switch ($fact->record()->tag()) {
        case 'SOUR':
            if ($level1type === 'DATA') {
                // SOUR:DATA facts may take a NOTE (but the SOUR record may not).
                echo view('cards/add-note', [
                    'level' => 2,
                    'tree' => $tree,
                ]);
                echo view('cards/add-shared-note', [
                    'level' => 2,
                    'tree' => $tree,
                ]);
                // SOUR:DATA facts may also take multiple EVEN.
                echo view('cards/add-sour-data-even', [
                    'tree' => $tree,
                ]);
            }
            break;
        case 'FAM':
        case 'INDI':
            // FAM and INDI records have real facts. They can take NOTE/SOUR/OBJE/etc.
            if ($level1type !== 'SEX' && $level1type !== 'NOTE' && $level1type !== 'ALIA') {
                if ($level1type !== 'SOUR') {
                    echo view('cards/add-source-citation', [
                        'level'          => 2,
                        'full_citations' => $tree->getPreference('FULL_SOURCES'),
                        'tree'           => $tree,
                    ]);
                }
                if ($level1type !== 'OBJE') {
                    if ($tree->getPreference('MEDIA_UPLOAD') >= Auth::accessLevel($tree)) {
                        echo view('cards/add-media-object', [
                            'level' => 2,
                            'tree'  => $tree,
                        ]);
                    }
                }
                echo view('cards/add-note', [
                    'level' => 2,
                    'tree' => $tree,
                ]);
                echo view('cards/add-shared-note', [
                    'level' => 2,
                    'tree' => $tree,
                ]);
                if ($level1type !== 'ASSO' && $level1type !== 'NOTE' && $level1type !== 'SOUR') {
                    echo view('cards/add-associate', [
                        'id'    => Uuid::uuid4()->toString(),
                        'level' => 2,
                        'tree' => $tree,
                    ]);
                }
                // allow to add godfather and godmother for CHR fact or best man and bridesmaid  for MARR fact in one window
                if (in_array($level1type, Config::twoAssociates(), true)) {
                    echo view('cards/add-associate', [
                        'id'    => Uuid::uuid4()->toString(),
                        'level' => 2,
                        'tree' => $tree,
                    ]);
                }
                if ($level1type !== 'SOUR') {
                    echo view('cards/add-restriction', [
                        'level' => 2,
                        'tree' => $tree,
                    ]);
                }
            }
            break;
        default:
            // Other types of record do not have these lower-level records
            break;
    }

    ?>

    <div class="form-group row">
        <label class="col-sm-3 col-form-label" for="keep_chan">
            <?= I18N::translate('Last change') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/checkbox-inline', ['label' => I18N::translate('Keep the existing “last change” information'), 'name' => 'keep_chan', 'checked' => (bool) $tree->getPreference('NO_UPDATE_CHAN')]) ?>
            <div>
                <?= I18N::translate('%1$s: %2$s', I18N::translate('Timestamp'), view('components/datetime', ['timestamp' => $fact->record()->lastChangeTimestamp()])) ?>
            </div>
            <div>
                <?= I18N::translate('%1$s: %2$s', I18N::translate('Author of last change'), e($fact->record()->lastChangeUser())) ?>
            </div>
        </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-3 wt-page-options-label">
        </div>
        <div class="col-sm-9 wt-page-options-value">
            <button class="btn btn-primary" type="submit">
                <?= view('icons/save') ?>
                <?= /* I18N: A button label. */
                I18N::translate('save') ?>
            </button>
            <a class="btn btn-secondary" href="<?= e($url) ?>">
                <?= view('icons/cancel') ?>
                <?= /* I18N: A button label. */
                I18N::translate('cancel') ?>
            </a>
            <?php if ($can_edit_raw) : ?>
                <a class="btn btn-link" href="<?= e(route(EditRawFactPage::class, ['xref' => $fact->record()->xref(), 'fact_id' => $fact->id(), 'tree' => $tree->name(), 'url' => $url])) ?>">
                    <?= I18N::translate('Edit the raw GEDCOM') ?>
                </a>
            <?php endif; ?>
        </div>
    </div>
</form>

<?= view('modals/on-screen-keyboard') ?>
<?= view('modals/ajax') ?>
<?= view('edit/initialize-calendar-popup') ?>
