<?php
$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$this->htmlElement('body')->appendAttribute('class', 'item-sets browse');
?>

<?php echo $this->pageTitle($translate('Item sets')); ?>

<?php echo $this->searchFilters(); ?>

<div class="browse-controls">
    <?php echo $this->pagination(); ?>
    <?php echo $this->hyperlink($translate('Advanced search'), $this->url(null, ['action' => 'search'], ['query' => $this->params()->fromQuery()], true), ['class' => 'advanced-search']); ?>
    <?php echo $this->browse()->renderSortSelector('item_sets'); ?>
</div>

<div id="page-actions">
    <?php if ($this->userIsAllowed('Omeka\Api\Adapter\ItemSetAdapter', 'create')): ?>
    <?php echo $this->hyperlink($translate('Add new item set'), $this->url(null, ['action' => 'add'], true), ['class' => 'button']); ?>
    <?php endif; ?>
</div>

<?php $this->trigger('view.browse.before'); ?>
<?php if($itemSets): ?>
<form method="post" id="batch-form" class="disable-unsaved-warning">
    <?php if ($this->userIsAllowed('Omeka\Api\Adapter\ItemSetAdapter', 'batch_update')): ?>
    <div class="batch-inputs">
        <select class="batch-actions-select" aria-label="<?php echo $translate('Batch actions'); ?>">
            <option value="default"><?php echo $translate('Batch actions'); ?></option>
            <option value="update-selected" disabled="true"><?php echo $translate('Edit selected'); ?></option>
            <?php if ($this->userIsAllowed('Omeka\Api\Adapter\ItemSetAdapter', 'batch_update_all')): ?>
            <option value="update-all"><?php echo $translate('Edit all'); ?></option>
            <?php endif; ?>
            <option value="delete-selected" disabled="true"><?php echo $translate('Delete selected'); ?></option>
            <?php if ($this->userIsAllowed('Omeka\Api\Adapter\ItemSetAdapter', 'batch_delete_all')): ?>
            <option value="delete-all"><?php echo $translate('Delete all'); ?></option>
            <?php endif; ?>
        </select>
        <div class="batch-actions">
            <button type="button" class="default active" disabled="true"><?php echo $translate('Go'); ?></button>
            <input type="submit" class="update-selected" name="update_selected" value="<?php echo $translate('Go'); ?>" formaction="<?php echo $escape($this->url(null, ['action' => 'batch-edit'], ['query' => $returnQuery], true)); ?>">
            <input type="submit" class="update-all" name="update_all" value="<?php echo $translate('Go'); ?>" formaction="<?php echo $escape($this->url(null, ['action' => 'batch-edit-all'], ['query' => $returnQuery], true)); ?>">
            <input type="hidden" name="query" class="batch-query" value="<?php echo $escape(json_encode($this->params()->fromQuery())); ?>">
            <a class="delete button sidebar-content delete-selected" data-sidebar-selector="#sidebar-delete-selected"><?php echo $translate('Go'); ?></a>
            <a class="delete button sidebar-content delete-all" data-sidebar-selector="#sidebar-delete-all"><?php echo $translate('Go'); ?></a>
        </div>
    </div>
    <?php endif; ?>
<table class="tablesaw batch-edit" data-tablesaw-mode="stack">
    <thead>
        <tr>
            <th>
                <input type="checkbox" class="select-all" aria-label="<?php echo $translate('Select all'); ?>">
                <?php echo $translate('Title'); ?>
            </th>
            <?php echo $this->browse()->renderHeaderRow('item_sets'); ?>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($itemSets as $itemSet): ?>
        <tr>
            <td>
                <?php if ($this->userIsAllowed('Omeka\Api\Adapter\ItemSetAdapter', 'batch_update') && ($itemSet->userIsAllowed('update') || $itemSet->userIsAllowed('delete'))): ?>
                <input type="checkbox" name="resource_ids[]" value="<?php echo $itemSet->id(); ?>" aria-label="<?php echo $translate('Select item set'); ?>">
                <?php endif; ?>
                <?php echo $itemSet->linkPretty(); ?>
                <?php if (!$itemSet->isPublic()): ?>
                    <span class="o-icon-private" aria-label="<?php echo $translate('Private'); ?>"></span>
                <?php endif; ?>
                <ul class="actions">
                    <?php $this->trigger('view.browse.actions', ['resource' => $itemSet]); ?>
                    <?php if ($itemSet->userIsAllowed('update')): ?>
                    <li><?php echo $itemSet->link('', 'edit', [
                        'class' => 'o-icon-edit',
                        'title' => $translate('Edit'),
                    ]); ?></li>
                    <?php endif; ?>
                    <?php if ($itemSet->userIsAllowed('delete')): ?>
                    <li><?php echo $this->hyperlink('', '#', [
                        'data-sidebar-selector' => '#sidebar',
                        'data-sidebar-content-url' => $itemSet->url('delete-confirm'),
                        'class' => 'o-icon-delete sidebar-content',
                        'title' => $translate('Delete'),
                    ]); ?></li>
                    <?php endif; ?>
                    <li><?php echo $this->hyperlink('', '#', [
                        'data-sidebar-selector' => '#sidebar',
                        'data-sidebar-content-url' => $itemSet->url('show-details'),
                        'class' => 'o-icon-more sidebar-content',
                        'title' => $translate('Details'),
                    ]); ?></li>
                </ul>
            </td>
            <?php echo $this->browse()->renderContentRow('item_sets', $itemSet); ?>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

</form>

<?php $this->trigger('view.browse.after'); ?>
<div class="browse-controls">
    <?php echo $this->pagination(); ?>
</div>

<div id="sidebar" class="sidebar">
    <?php echo $this->hyperlink('', '#', [
        'class' => 'sidebar-close o-icon-close',
        'title' => $translate('Close'),
    ]); ?>
    <div class="sidebar-content"></div>
</div>
<div id="sidebar-delete-selected" class="sidebar">
    <?php echo $this->hyperlink('', '#', [
        'class' => 'sidebar-close o-icon-close',
        'title' => $translate('Close'),
    ]); ?>
    <div class="sidebar-content">
        <div id="sidebar-confirm">
            <h3><?php echo $translate('Delete item sets'); ?></h3>
            <p><?php echo $translate('Are you sure you would like to delete the selected item sets?'); ?></p>
            <p class="error"><?php echo sprintf(
                $translate('%1$s: this action will permanently delete %2$s item sets and cannot be undone.'),
                sprintf('<strong>%s</strong>', $translate('Warning')),
                '<strong><span id="delete-selected-count"></span></strong>'
            ); ?></p>
            <?php echo $this->form($this->formDeleteSelected); ?>
        </div>
    </div>
</div>
<div id="sidebar-delete-all" class="sidebar">
    <?php echo $this->hyperlink('', '#', [
        'class' => 'sidebar-close o-icon-close',
        'title' => $translate('Close'),
    ]); ?>
    <div class="sidebar-content">
        <div id="sidebar-confirm">
            <h3><?php echo $translate('Delete item sets'); ?></h3>
            <p><?php echo $translate('Are you sure you would like to delete all item sets on all pages of this result?'); ?></p>
            <p class="error"><?php echo sprintf(
                $translate('%1$s: this action will permanently delete %2$s item sets and cannot be undone.'),
                sprintf('<strong>%s</strong>', $translate('Warning')),
                sprintf('<strong>%s</strong>', number_format($this->pagination()->getPaginator()->getTotalCount()))
            ); ?></p>
            <label><input type="checkbox" name="confirm-delete-all-check"> <?php echo $translate('Are you sure?'); ?></label>
            <?php echo $this->form($this->formDeleteAll); ?>
        </div>
    </div>
</div>

<script>
// Complete the batch delete form after confirmation.
$('#confirm-delete-selected, #confirm-delete-all').on('submit', function(e) {
    var confirmForm = $(this);
    if ('confirm-delete-all' === this.id) {
        confirmForm.append($('.batch-query').clone());
    } else {
        $('#batch-form').find('input[name="resource_ids[]"]:checked').each(function() {
            confirmForm.append($(this).clone().prop('disabled', false).attr('type', 'hidden'));
        });
    }
});
$('.delete-all').on('click', function(e) {
    Omeka.closeSidebar($('#sidebar-delete-selected'));
});
$('.delete-selected').on('click', function(e) {
    Omeka.closeSidebar($('#sidebar-delete-all'));
    var inputs = $('input[name="resource_ids[]"]');
    $('#delete-selected-count').text(inputs.filter(':checked').length);
});
$('#sidebar-delete-all').on('click', 'input[name="confirm-delete-all-check"]', function(e) {
    $('#confirm-delete-all input[type="submit"]').prop('disabled', this.checked ? false : true);
});
</script>

<?php else: ?>

<div class="no-resources">
    <p><?php echo $translate('Omeka could not find any item sets.'); ?></p>
</div>

<?php endif; ?>
