<?php use Fisharebest\Webtrees\Auth; ?>
<?php use Fisharebest\Webtrees\Functions\FunctionsEdit; ?>
<?php use Fisharebest\Webtrees\Functions\FunctionsPrint; ?>
<?php use Fisharebest\Webtrees\Functions\FunctionsPrintFacts; ?>
<?php use Fisharebest\Webtrees\GedcomTag; ?>
<?php use Fisharebest\Webtrees\I18N; ?>

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

<form class="wt-page-options wt-page-options-media-list d-print-none">
    <input type="hidden" name="ged" value="<?= e($tree->name()) ?>">
    <input type="hidden" name="route" value="module">
    <input type="hidden" name="module" value="<?= e($module) ?>">
    <input type="hidden" name="action" value="<?= e($action) ?>">
    <input type="hidden" name="action2" value="1">
    <input type="hidden" name="search" value="yes">

    <div class="row form-group">
        <label class="col-sm-3 col-form-label wt-page-options-label" for="folder">
            <?= I18N::translate('Folder') ?>
        </label>
        <div class="col-sm-3 wt-page-options-value">
            <?= view('components/select', ['name' => 'folder', 'selected' => $folder, 'options' => $folders]) ?>
            <?= view('components/checkbox-inline', ['label' => /* I18N: Label for check-box */ I18N::translate('Include subfolders'), 'name' => 'subdirs', 'checked' => $subdirs === '1']) ?>
        </div>

        <label class="col-sm-3 col-form-label wt-page-options-label" for="max">
            <?= I18N::translate('Media objects per page') ?>
        </label>
        <div class="col-sm-3 wt-page-options-value">
            <?= view('components/select', ['name' => 'max', 'selected' => $max, 'options' => FunctionsEdit::numericOptions([10, 20, 30, 40, 50, 75, 100, 150, 200])]) ?>
        </div>
    </div>

    <div class="row form-group">
        <label class="col-sm-3 col-form-label wt-page-options-label" for="form-type">
            <?= I18N::translate('Type') ?>
        </label>
        <div class="col-sm-3 wt-page-options-value">
            <?= view('components/select', ['name' => 'form_type', 'id' => 'form-type', 'selected' => $form_type, 'options' => ['' => ''] + $formats]) ?>
        </div>

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

    <div class="row form-group">
        <label class="col-sm-3 col-form-label wt-page-options-label" for="filter">
            <?= I18N::translate('Search filters') ?>
        </label>
        <div class="col-sm-3 wt-page-options-value">
            <input type="text" class="form-control" name="filter" id="filter" value="<?= e($filter) ?>">
        </div>

        <div class="col-sm-3 col-form-label wt-page-options-label">
        </div>
        <div class="col-sm-3 wt-page-options-value">
            <button type="submit" name="action2" value="1" class="btn btn-primary">
                <?= /* I18N: A button label. */ I18N::translate('search') ?>
            </button>
            <a class="btn btn-secondary" href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name()])) ?>">
                <?= /* I18N: A button label. */ I18N::translate('reset') ?>
            </a>
        </div>
    </div>
</form>

<div class="wt-page-content">
    <?php if (!empty($media_objects)) : ?>
        <p class="text-center mt-4"><?= I18N::translate('Media objects found') ?> <?= I18N::number($count) ?></p>

        <div class="row text-center">
            <div class="col">
                <?php if ($page > 1) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => 1])) ?>">
                        <?= I18N::translate('first') ?>
                    </a>
                <?php endif ?>
            </div>
            <div class="col">
                <?php if ($page > 1) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => $page - 1])) ?>">
                        <?= I18N::translate('previous') ?>
                    </a>
                <?php endif ?>
            </div>
            <div class="col">
                <?= I18N::translate('Page %s of %s', $page, $pages) ?>
            </div>
            <div class="col">
                <?php if ($page < $pages) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => $page + 1])) ?>">
                        <?= I18N::translate('next') ?>
                    </a>
                <?php endif ?>
            </div>
            <div class="col">
                <?php if ($page < $pages) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => $pages])) ?>">
                        <?= I18N::translate('last') ?>
                    </a>
                <?php endif ?>
            </div>
        </div>

        <div class="card-deck row mb-4 mt-4">
            <?php foreach ($media_objects as $n => $media_object) : ?>
                <div class="col-xs-12 col-sm-6 col-lg-4 d-flex">
                    <div class="card mb-4">
                        <div class="card-header">
                            <h4 class="card-title">
                                <a href="<?= e($media_object->url()) ?>"><?= $media_object->fullName() ?></a>
                            </h4>
                        </div>
                        <div class="card-body">
                            <?php foreach ($media_object->mediaFiles() as $media_file) : ?>
                                <?= $media_file->displayImage(300, 200, 'contain', ['class' => 'img-fluid']) ?>
                            <?php endforeach ?>

                            <p class="card-text">
                                <?php
                                // Show file details
                                $mediatype = $media_file->type();
                                if ($mediatype) {
                                    echo GedcomTag::getLabelValue('TYPE', GedcomTag::getFileFormTypeValue($mediatype));
                                }
                                echo FunctionsPrintFacts::printFactSources($tree, $media_object->gedcom(), 1);
                                echo FunctionsPrint::printFactNotes($tree, $media_object->gedcom(), 1);
                                if ($media_file->isExternal()) {
                                    echo GedcomTag::getLabelValue('URL', $media_file->filename());
                                } elseif ($media_file->fileExists()) {
                                    ?>
                                <button class="btn btn-secondary" type="button" data-toggle="collapse" data-target="#details-<?= e($n) ?>" aria-expanded="false" aria-controls="details-<?= e($n) ?>">
                                    <?= I18N::translate('Media file') ?>
                                </button>
                            <div class="collapse" id="details-<?= e($n) ?>">
                                    <?php
                                    if (Auth::isEditor($tree)) {
                                        echo GedcomTag::getLabelValue('FILE', $media_file->filename());
                                    }
                                    echo GedcomTag::getLabelValue('FORM', $media_file->mimeType());
                                    foreach ($media_file->attributes() as $tag => $value) {
                                        echo GedcomTag::getLabelValue($tag, $value);
                                    }
                                    ?>
                            </div>
                                    <?php
                                } else {
                                    echo '<p class="alert alert-danger">', /* I18N: %s is a filename */ I18N::translate('The file “%s” does not exist.', $media_file->filename()), '</p>';
                                }
                                ?>
                            </p>
                        </div>
                        <div class="card-footer">
                            <?php foreach ($media_object->linkedIndividuals('OBJE') as $record) : ?>
                                <?= view('icons/individual') ?>
                                <a href="<?= e($record->url()) ?>"><?= $record->fullName() ?></a>
                                <br>
                            <?php endforeach ?>

                            <?php foreach ($media_object->linkedFamilies('OBJE') as $record) : ?>
                                <?= view('icons/family') ?>
                                <a href="<?= e($record->url()) ?>"><?= $record->fullName() ?></a>
                                <br>
                            <?php endforeach ?>

                            <?php foreach ($media_object->linkedSources('OBJE') as $record) : ?>
                                <?= view('icons/source') ?>
                                <a href="<?= e($record->url()) ?>"><?= $record->fullName() ?></a>
                                <br>
                            <?php endforeach ?>
                        </div>
                    </div>
                </div>
            <?php endforeach ?>
        </div>

        <div class="row text-center">
            <div class="col">
                <?php if ($page > 1) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => 1])) ?>">
                        <?= I18N::translate('first') ?>
                    </a>
                <?php endif ?>
            </div>
            <div class="col">
                <?php if ($page > 1) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => $page - 1])) ?>">
                        <?= I18N::translate('previous') ?>
                    </a>
                <?php endif ?>
            </div>
            <div class="col">
                <?= I18N::translate('Page %s of %s', $page, $pages) ?>
            </div>
            <div class="col">
                <?php if ($page < $pages) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => $page + 1])) ?>">
                        <?= I18N::translate('next') ?>
                    </a>
                <?php endif ?>
            </div>
            <div class="col">
                <?php if ($page < $pages) : ?>
                    <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'ged' => $tree->name(), 'action2' => '1', 'folder' => $folder, 'subdirs' => $subdirs, 'filter' => $filter, 'form_type' => $form_type, 'max' => $max, 'page' => $pages])) ?>">
                        <?= I18N::translate('last') ?>
                    </a>
                <?php endif ?>
            </div>
        </div>
    <?php endif ?>
</div>
