<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Http\RequestHandlers\SearchGeneralAction;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Location;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Tree;
use Illuminate\Support\Collection;

/**
 * @var Collection<int,Tree>       $all_trees
 * @var bool                       $exist_locations
 * @var bool                       $exist_notes
 * @var bool                       $exist_repositories
 * @var bool                       $exist_sources
 * @var Collection<int,Family>     $families
 * @var Collection<int,Individual> $individuals
 * @var Collection<int,Location>   $locations
 * @var Collection<int,Note>       $notes
 * @var string                     $query
 * @var Collection<int,Repository> $repositories
 * @var Collection<int,Source>     $sources
 * @var bool                       $search_families
 * @var bool                       $search_individuals
 * @var bool                       $search_locations
 * @var bool                       $search_notes
 * @var bool                       $search_repositories
 * @var bool                       $search_sources
 * @var Collection<int,Tree>       $search_trees
 * @var string                     $title
 * @var Tree                       $tree
 */

?>

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

<form method="post" action="<?= e(route(SearchGeneralAction::class, ['tree' => $tree->name()])) ?>" class="wt-page-options wt-page-options-search hidden-print mb-4" name="searchform">
    <div class="row">
        <label class="col-sm-3 col-form-label wt-page-options-label" for="query">
            <?= I18N::translate('Search for') ?>
        </label>
        <div class="col-sm-9 wt-page-options-value">
            <div class="input-group">
                <input id="query" class="form-control" type="text" name="query" value="<?= e($query) ?>" required="required">
                <?= view('edit/input-addon-keyboard', ['id' => 'query']) ?>
            </div>
        </div>
    </div>

    <fieldset class="row">
        <label class="col-sm-3 col-form-label wt-page-options-label">
            <?= I18N::translate('Records') ?>
        </label>
        <div class="col-sm-9 wt-page-options-value">
            <div class="form-check form-check-inline">
                <label class="form-check-label">
                    <input class="form-check-input" <?= $search_individuals ? 'checked' : '' ?>
                           name="search_individuals" type="checkbox">
                    <?= I18N::translate('Individuals') ?>
                </label>
            </div>

            <div class="form-check form-check-inline">
                <label class="form-check-label">
                    <input class="form-check-input" <?= $search_families ? 'checked' : '' ?> name="search_families"
                           type="checkbox">
                    <?= I18N::translate('Families') ?>
                </label>
            </div>

            <?php if ($exist_sources) : ?>
                <div class="form-check form-check-inline">
                    <label class="form-check-label">
                        <input class="form-check-input" <?= $search_sources ? 'checked' : '' ?> name="search_sources"
                               type="checkbox">
                        <?= I18N::translate('Sources') ?>
                    </label>
                </div>
            <?php endif ?>

            <?php if ($exist_repositories) : ?>
                <div class="form-check form-check-inline">
                    <label class="form-check-label">
                        <input class="form-check-input" <?= $search_repositories ? 'checked' : '' ?>
                               name="search_repositories" type="checkbox">
                        <?= I18N::translate('Repositories') ?>
                    </label>
                </div>
            <?php endif ?>

            <?php if ($exist_notes) : ?>
                <div class="form-check form-check-inline">
                    <label class="form-check-label">
                        <input class="form-check-input" <?= $search_notes ? 'checked' : '' ?> name="search_notes"
                               type="checkbox">
                        <?= I18N::translate('Shared notes') ?>
                    </label>
                </div>
            <?php endif ?>

            <?php if ($exist_locations) : ?>
                <div class="form-check form-check-inline">
                    <label class="form-check-label">
                        <input class="form-check-input" <?= $search_locations ? 'checked' : '' ?> name="search_locations"
                               type="checkbox">
                        <?= I18N::translate('Locations') ?>
                    </label>
                </div>
            <?php endif ?>
        </div>
    </fieldset>

    <?= view('search-trees', ['all_trees' => $all_trees, 'search_trees' => $search_trees]) ?>

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

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

<?php if ($query !== '') : ?>
    <?php if ($individuals->isEmpty() && $families->isEmpty() && $locations->isEmpty() && $repositories->isEmpty() && $sources->isEmpty() && $notes->isEmpty()) : ?>
        <div class="alert alert-info">
            <?= I18N::translate('No results found.') ?>
        </div>
    <?php else : ?>
        <?= view('search-results', ['families' => $families, 'individuals' => $individuals, 'locations' => $locations, 'notes' => $notes, 'repositories' => $repositories, 'sources' => $sources, 'search_families' => $search_families, 'search_individuals' => $search_individuals, 'search_locations' => $search_locations, 'search_notes' => $search_notes, 'search_repositories' => $search_repositories, 'search_sources' => $search_sources, 'tree' => $tree]) ?>
    <?php endif ?>
<?php endif ?>

<?= view('modals/on-screen-keyboard') ?>
