<?php

use Fisharebest\Webtrees\Http\RequestHandlers\SearchAdvancedAction;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Tree;
use Illuminate\Support\Collection;

/**
 * @var array<string,string>       $date_options
 * @var array<string,string>       $fields
 * @var array<string,string>       $field_labels
 * @var Collection<int,Individual> $individuals
 * @var array<string,string>       $name_options
 * @var array<string,string>       $other_fields
 * @var string                     $title
 * @var Tree                       $tree
 */

?>

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

<div id="advanced-search-page">
    <form method="post" action="<?= e(route(SearchAdvancedAction::class, ['tree' => $tree->name()])) ?>" class="wt-page-options wt-page-options-search-advanced hidden-print mb-4">
        <div class="row">
            <div class="col col-form-label wt-page-options-label">
                <?= I18N::translate('Individual') ?>
            </div>
        </div>

        <?php foreach ($fields as $field_name => $field_value) : ?>
            <?php if (!str_starts_with($field_name, 'FATHER:') && !str_starts_with($field_name, 'MOTHER:')) : ?>
                <?= view('search-advanced-field', ['field_label' => $field_labels[$field_name], 'field_name' => $field_name, 'field_value' => $field_value, 'modifier' => $modifiers[$field_name] ?? '', 'date_options' => $date_options, 'name_options' => $name_options]) ?>
            <?php endif ?>
        <?php endforeach ?>

        <div class="row">
            <div class="col-sm-3 col-form-label wt-page-options-label">
                <?= I18N::translate('Add more fields') ?>
            </div>

            <div class="col-sm-3 wt-page-options-value">
                <?= view('components/select', ['name' => 'other_field', 'selected' => '', 'options' => ['' => ''] + $other_fields]) ?>
            </div>

            <div class="col-sm-3 wt-page-options-value">
                <input type="text" class="form-control" name="other_value">
            </div>

            <div class="col-sm-3 wt-page-options-value">
            </div>
        </div>

        <br>

        <div class="row">
            <div class="col col-form-label wt-page-options-label">
                <?= I18N::translate('Father') ?>
            </div>
        </div>

        <?php foreach ($fields as $field_name => $field_value) : ?>
            <?php if (str_starts_with($field_name, 'FATHER:')) : ?>
                <?= view('search-advanced-field', ['field_label' => $field_labels[$field_name], 'field_name' => $field_name, 'field_value' => $field_value, 'modifier' => $modifiers[$field_name] ?? '', 'date_options' => $date_options, 'name_options' => $name_options]) ?>
            <?php endif ?>
        <?php endforeach ?>

        <br>

        <div class="row">
            <div class="col col-form-label wt-page-options-label">
                <?= I18N::translate('Mother') ?>
            </div>
        </div>

        <?php foreach ($fields as $field_name => $field_value) : ?>
            <?php if (str_starts_with($field_name, 'MOTHER:')) : ?>
                <?= view('search-advanced-field', ['field_label' => $field_labels[$field_name], 'field_name' => $field_name, 'field_value' => $field_value, 'modifier' => $modifiers[$field_name] ?? '', 'date_options' => $date_options, 'name_options' => $name_options]) ?>
            <?php endif ?>
        <?php endforeach ?>

        <br>

        <div class="row mb-3">
            <div class="col">
                <input type="submit" class="btn btn-primary" value="<?= /* I18N: A button label. */ I18N::translate('search') ?>">
            </div>
        </div>

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

<?php if (array_filter($fields) !== []) : ?>
    <?php if ($individuals->isEmpty()) : ?>
        <div class="alert alert-info">
            <?= I18N::translate('No results found.') ?>
        </div>
    <?php else : ?>
        <?= view('search-results', ['individuals' => $individuals, 'search_families' => false, 'search_individuals' => true, 'search_locations' => false, 'search_notes' => false, 'search_sources' => false, 'search_repositories' => false, 'tree' => $tree]) ?>
    <?php endif ?>
<?php endif ?>
