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

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

<?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('media'); ?>
</div>

<?php $this->trigger('view.browse.before'); ?>
<?php if ($medias): ?>
<form method="post" id="batch-form" class="disable-unsaved-warning">
    <?php if ($this->userIsAllowed('Omeka\Api\Adapter\MediaAdapter', '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="disabled"><?php echo $translate('Edit selected'); ?></option>
            <?php if ($this->userIsAllowed('Omeka\Api\Adapter\MediaAdapter', 'batch_update_all')): ?>
            <option value="update-all"><?php echo $translate('Edit all'); ?></option>
            <?php endif; ?>
            <option value="delete-selected" disabled="disabled"><?php echo $translate('Delete selected'); ?></option>
            <?php if ($this->userIsAllowed('Omeka\Api\Adapter\MediaAdapter', '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="disabled"><?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('media'); ?>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($medias as $media): ?>
        <tr>
            <td>
                <?php if ($this->userIsAllowed('Omeka\Api\Adapter\MediaAdapter', 'batch_update') && ($media->userIsAllowed('update') || $media->userIsAllowed('delete'))): ?>
                <input type="checkbox" name="resource_ids[]" value="<?php echo $media->id(); ?>"  aria-label="<?php echo $translate('Select media'); ?>">
                <?php endif; ?>
                <?php echo $media->linkPretty(); ?>
                <?php if (!$media->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' => $media]); ?>
                    <?php if ($media->userIsAllowed('update')): ?>
                    <li><?php echo $media->link('', 'edit', [
                        'class' => 'o-icon-edit',
                        'title' => $translate('Edit'),
                    ]); ?></li>
                    <?php endif; ?>
                    <?php if ($media->userIsAllowed('delete')): ?>
                    <li><?php echo $this->hyperlink('', '#', [
                        'data-sidebar-selector' => '#sidebar',
                        'data-sidebar-content-url' => $media->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' => $media->url('show-details'),
                        'class' => 'o-icon-more sidebar-content',
                        'title' => $translate('Details'),
                    ]); ?></li>
                </ul>
            </td>
            <?php echo $this->browse()->renderContentRow('media', $media); ?>
        </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 media'); ?></h3>
            <p><?php echo $translate('Are you sure you would like to delete the selected media?'); ?></p>
            <p class="error"><?php echo sprintf(
                $translate('%1$s: this action will permanently delete %2$s media 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 media'); ?></h3>
            <p><?php echo $translate('Are you sure you would like to delete all media on all pages of this result?'); ?></p>
            <p class="error"><?php echo sprintf(
                $translate('%1$s: this action will permanently delete %2$s media 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:not(:disabled)').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 media.'); ?></p>
</div>

<?php endif; ?>
