<?php

declare(strict_types=1);

use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Tree;

/**
 * @var int                    $block_id
 * @var bool                   $can_edit
 * @var array<int,object{
 *     favorite_id:string,
 *     favorite_type:string,
 *     url:string|null,
 *     note:string|null,
 *     title:string|null,
 *     record:GedcomRecord|null
 * }> $favorites
 * @var string                 $module_name
 * @var Tree                   $tree
 */
?>

<?php foreach ($favorites as $favorite) : ?>
    <div class="pt-2">
        <?php if ($favorite->favorite_type === 'URL') : ?>
            <a href="<?= e($favorite->url) ?>"><b><?= e($favorite->title) ?></b></a>
        <?php elseif ($favorite->record instanceof Individual) : ?>
            <?= view('chart-box', ['individual' => $favorite->record]) ?>
        <?php elseif ($favorite->record !== null) : ?>
            <?= $favorite->record->formatList() ?>
        <?php endif ?>

        <div class="wt-favorites-block-note">
            <?= e((string) $favorite->note) ?>
        </div>
    </div>

    <?php if ($can_edit) : ?>
        <form method="post" action="<?= e(route('module', ['module' => $module_name, 'action' => 'DeleteFavorite', 'tree' => $tree->name(), 'favorite_id' => $favorite->favorite_id])) ?>">
            <button type="submit" class="btn btn-link" data-wt-confirm="<?= I18N::translate('Are you sure you want to remove this item from your list of favorites?') ?>">
                <?= I18N::translate('Remove') ?>
            </button>

            <?= csrf_field() ?>
        </form>
    <?php endif ?>
<?php endforeach ?>

<?php if ($can_edit) : ?>
    <button type="button" class="btn btn-link" data-bs-toggle="modal" data-bs-target="#favorite-form-<?= e($block_id) ?>">
        <?= I18N::translate('Add a favorite') ?>
    </button>

    <div class="modal fade" data-bs-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true" id="favorite-form-<?= e($block_id) ?>" aria-labelledby="favorite-form-<?= e($block_id) ?>-label">
        <div class="modal-dialog modal-xl" role="document">
            <div class="modal-content">
                <form method="post" action="<?= e(route('module', ['module' => $module_name, 'action' => 'AddFavorite', 'tree' => $tree->name()])) ?>">
                    <div class="modal-header">
                        <h5 class="modal-title" id="favorite-form-<?= e($block_id) ?>-label">
                            <?= I18N::translate('Add a favorite') ?>
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?= I18N::translate('close') ?>">
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="row mb-3">
                            <div class="col-sm-4 col-md-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="type" value="indi" id="fav-indi-<?= e($block_id) ?>" checked>
                                    <label class="form-check-label" for="fav-indi-<?= e($block_id) ?>">
                                        <?= I18N::translate('Individual') ?>
                                    </label>
                                </div>
                            </div>
                            <div class="col-sm-8 col-md-9">
                                <?= view('components/select-individual', ['name' => 'indi-xref', 'tree' => $tree]) ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-4 col-md-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="type" value="fam" id="fav-fam-<?= e($block_id) ?>">
                                    <label class="form-check-label" for="fav-fam-<?= e($block_id) ?>">
                                        <?= I18N::translate('Family') ?>
                                    </label>
                                </div>
                            </div>
                            <div class="col-sm-8 col-md-9">
                                <?= view('components/select-family', ['name' => 'fam-xref', 'tree' => $tree]) ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-4 col-md-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="type" value="obje" id="fav-obje-<?= e($block_id) ?>">
                                    <label class="form-check-label" for="fav-obje-<?= e($block_id) ?>">
                                        <?= I18N::translate('Media object') ?>
                                    </label>
                                </div>
                            </div>
                            <div class="col-sm-8 col-md-9">
                                <?= view('components/select-media', ['name' => 'obje-xref', 'tree' => $tree]) ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-4 col-md-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="type" value="sour" id="fav-sour-<?= e($block_id) ?>">
                                    <label class="form-check-label" for="fav-sour-<?= e($block_id) ?>">
                                        <?= I18N::translate('Source') ?>
                                    </label>
                                </div>
                            </div>
                            <div class="col-sm-8 col-md-9">
                                <?= view('components/select-source', ['name' => 'sour-xref', 'tree' => $tree]) ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-4 col-md-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="type" value="repo" id="fav-repo-<?= e($block_id) ?>">
                                    <label class="form-check-label" for="fav-repo-<?= e($block_id) ?>">
                                        <?= I18N::translate('Repository') ?>
                                    </label>
                                </div>
                            </div>
                            <div class="col-sm-8 col-md-9">
                                <?= view('components/select-repository', ['name' => 'repo-xref', 'tree' => $tree]) ?>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-4 col-md-3">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="type" value="url" id="fav-url-<?= e($block_id) ?>">
                                    <label class="form-check-label" for="fav-url-<?= e($block_id) ?>">
                                        <?= I18N::translate('URL') ?>
                                    </label>
                                </div>
                            </div>
                            <div class="col-sm-8 col-md-9">
                                <label>
                                    <span class="visually-hidden">
                                        <?= I18N::translate('URL') ?>
                                    </span>
                                    <input type="text" class="form-control" name="url" id="url" value="" placeholder="<?= I18N::translate('URL') ?>">
                                </label>
                                <label>
                                    <span class="visually-hidden">
                                        <?= I18N::translate('Title') ?>
                                    </span>
                                    <input type="text" class="form-control" name="title" id="title" value="" placeholder="<?= I18N::translate('Title') ?>">
                                </label>
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-sm-4 col-md-3">
                                <label for="fav-note-<?= e($block_id) ?>">
                                    <?= I18N::translate('Note') ?>
                                </label>
                            </div>
                            <div class="col-sm-8 col-md-9">
                                <textarea name="note" id="fav-note-<?= e($block_id) ?>" class="form-control" placeholder="<?= I18N::translate('Enter an optional note about this favorite') ?>" rows="3" dir="auto"></textarea>
                            </div>
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                            <?= view('icons/cancel') ?>
                            <?= I18N::translate('cancel') ?>
                        </button>
                        <button type="submit" class="btn btn-primary">
                            <?= view('icons/save') ?>
                            <?= I18N::translate('save') ?>
                        </button>
                    </div>

                    <?= csrf_field() ?>
                </form>
            </div>
        </div>
    </div>
<?php endif ?>
