<?php
$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$hyperlink = $this->plugin('hyperlink');
$itemsFound = count($items) > 0;
$expanded = $resourceClassId || $itemSetId || $id;
?>

<div id="item-results">
    <h3><?php echo $translate('Select item'); ?></h3>

    <div class="search-nav">
        <div id="sidebar-resource-search" class="resource-search" data-search-url="<?php echo $escape($this->url(null, [], [], true)); ?>">
            <?php
            // Preserve any original filter params that may have been set during
            // the initial load, removing sorting and pagination params as well
            // as params set by this form.
            echo $this->queryToHiddenInputs([
                'sort_by', 'sort_order', 'page',
                'search', 'resource_class_id', 'item_set_id', 'id',
            ]);
            ?>
            <input type="text" name="search" id="resource-list-search" value="<?php echo $escape($search); ?>">
            <button type="button" class="o-icon-search"><?php echo $translate('Search'); ?></button>
            <div class="resource-search-filters">
                <a href="#"
                    class="<?php echo $expanded ? 'collapse' : 'expand'; ?>"
                    aria-label="<?php echo $expanded ? $translate('Collapse') : $translate('Expand'); ?>"><?php echo $translate('Filter search'); ?>
                </a>
                <div class="collapsible">
                <?php
                echo $this->resourceClassSelect([
                    'name' => 'resource_class_id',
                    'options' => [
                        'empty_option' => '',
                    ],
                    'attributes' => [
                        'id' => 'filter-resource-class',
                        'class' => 'chosen-select',
                        'value' => $resourceClassId,
                        'data-placeholder' => 'Filter by class', // @translate
                    ],
                ]);
                echo $this->itemSetSelect([
                    'name' => 'item_set_id',
                    'options' => [
                        'empty_option' => '',
                    ],
                    'attributes' => [
                        'id' => 'filter-item-set',
                        'class' => 'chosen-select',
                        'value' => $itemSetId,
                        'data-placeholder' => 'Filter by item set', // @translate
                    ],
                ]);
                ?>
                <input type="number" name="id" min="1" step="1" value="<?php echo $this->escapeHtml($id); ?>" placeholder="<?php echo $this->escapeHtml($translate('Filter by item ID')); ?>">
                </div>
            </div>
        </div>
        <?php if ($itemsFound): ?>
        <?php echo $this->pagination('common/sidebar-pagination.phtml'); ?>
        <?php endif; ?>
    </div>

    <?php if ($itemsFound): ?>
    <?php echo $this->partial('common/sidebar-select-quick-add'); ?>

    <div class="items resource-list">
        <?php foreach ($items as $item):?>
        <?php $itemId = $item->id(); ?>
        <div class="resource item" data-resource-values="<?php echo $escape(json_encode($item->valueRepresentation())); ?>">
            <div class="select-resource-checkbox-wrapper">
              <input type="checkbox" class="select-resource-checkbox" name="resource_ids[]" aria-labelledby="select-item-<?php echo $itemId; ?>" value="<?php echo $itemId; ?>">
            </div>
            <?php
            $content = sprintf(
                '%s<span class="resource-name">%s</span>',
                $this->thumbnail($item, 'square'),
                $escape($item->displayTitle())
            );
            if ($showDetails):
                $attrs = [
                    'class' => 'sidebar-content select-resource resource-link',
                    'data-sidebar-content-url' => $item->url('show-details'),
                    'data-sidebar-selector' => '#resource-details',
                ];
            else:
                $attrs = [
                    'class' => 'select-resource resource-link',
                    'id' => 'select-item-' . $itemId,
                ];
            endif;
            echo $hyperlink->raw($content, '#', $attrs);
            ?>
        </div>
        <?php endforeach; ?>
    </div>
    <?php else: ?>
    <span class="no-resources"><?php $translate('Omeka could not find any items.'); ?></span>
    <?php endif; ?>
</div>

<div class="confirm-panel">
    <button type="button" class="select-resources-button"><?php echo $translate('Add selected'); ?></button>
</div>
