<?php
$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');
$this->htmlElement('body')->appendAttribute('class', 'users browse');
$currentUserId = $this->identity()->getId();
$userIsAllowedBatchUpdate = $this->userIsAllowed('Omeka\Api\Adapter\UserAdapter', 'batch_update');
$userIsAllowedBatchDelete = $this->userIsAllowed('Omeka\Api\Adapter\UserAdapter', 'batch_delete');
?>

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

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

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

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

<?php $this->trigger('view.browse.before'); ?>
<?php if (count($users)): ?>
<form method="post" id="batch-form" class="disable-unsaved-warning">
    <?php if ($userIsAllowedBatchUpdate || $userIsAllowedBatchDelete): ?>
    <div class="batch-inputs">
        <select class="batch-actions-select" name="batch_action" aria-label="<?php echo $translate('Batch actions'); ?>">
            <option value="default"><?php echo $translate('Batch actions'); ?></option>
            <?php if ($userIsAllowedBatchUpdate): ?>
            <option value="update-selected" class="batch-selected" disabled><?php echo $translate('Edit selected'); ?></option>
            <option value="update-all"><?php echo $translate('Edit all'); ?></option>
            <?php endif; ?>
            <?php if ($userIsAllowedBatchDelete): ?>
            <option value="delete-selected" class="batch-selected" disabled><?php echo $translate('Delete selected'); ?></option>
            <option value="delete-all"><?php echo $translate('Delete all'); ?></option>
            <?php endif; ?>
        </select>
        <div class="batch-actions">
            <button type="button" class="default active" disabled><?php echo $translate('Go'); ?></button>
            <?php if ($userIsAllowedBatchUpdate): ?>
            <input type="submit" class="update-selected" value="<?php echo $translate('Go'); ?>" formaction="<?php echo $escape($this->url(null, ['action' => 'batch-edit'], true)); ?>">
            <input type="submit" class="update-all" value="<?php echo $translate('Go'); ?>" formaction="<?php echo $escape($this->url(null, ['action' => 'batch-edit-all'], true)); ?>">
            <?php endif; ?>
            <input type="hidden" name="query" class="batch-query" value="<?php echo $escape(json_encode($this->params()->fromQuery())); ?>">
            <?php if ($userIsAllowedBatchDelete): ?>
            <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>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

<table class="tablesaw batch-edit" data-tablesaw-mode="stack">
    <thead>
        <tr>
            <th>
                <?php if ($userIsAllowedBatchUpdate || $userIsAllowedBatchDelete): ?>
                <input type="checkbox" class="select-all">
                <?php endif; ?>
                <?php echo $translate('Email'); ?>
            </th>
            <th><?php echo $translate('Role'); ?></th>
            <th><?php echo $translate('Created'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($users as $user): ?>
        <?php $userIsAllowedUpdate = $user->userIsAllowed('update'); ?>
        <?php $userIsAllowedDelete = $user->userIsAllowed('delete'); ?>
        <tr>
            <td>
                <?php if ($userIsAllowedBatchUpdate || $userIsAllowedBatchDelete): ?>
                <input type="checkbox" name="resource_ids[]" value="<?php echo $user->id(); ?>"<?php if (!($userIsAllowedUpdate || $userIsAllowedDelete) || $currentUserId == $user->id()): ?> disabled="disabled"<?php endif; ?>>
                <?php endif; ?>
                <?php echo $user->link($user->email() . ' (' . $user->name() . ')'); ?>
                <?php if (!$user->isActive()): ?>
                <span class="o-icon-user-inactive" aria-label="<?php echo $translate('Inactive'); ?>"></span>
                <?php endif; ?>
                <ul class="actions">
                    <?php $this->trigger('view.browse.actions', ['resource' => $user]); ?>
                    <?php if ($userIsAllowedUpdate): ?>
                    <li><?php echo $user->link('', 'edit', [
                        'class' => 'o-icon-edit',
                        'title' => $translate('Edit'),
                    ]) ?></li>
                    <?php endif; ?>
                    <?php if ($userIsAllowedDelete): ?>
                    <li><?php echo $this->hyperlink('', '#', [
                        'data-sidebar-content-url' => $user->url('delete-confirm'),
                        'class' => 'o-icon-delete sidebar-content',
                        'title' => $translate('Delete'),
                        'data-sidebar-selector' => '#sidebar-delete-selected',
                    ]); ?></li>
                    <?php endif; ?>
                    <li><?php echo $this->hyperlink('', '#', [
                        'data-sidebar-content-url' => $user->url('show-details'),
                        'class' => 'o-icon-more sidebar-content',
                        'title' => $translate('Details'),
                        'data-sidebar-selector' => '#sidebar',
                    ]); ?></li>
                </ul>
            </td>
            <td><?php echo $escape($translate($user->displayRole())); ?></td>
            <td><?php echo $escape($this->i18n()->dateFormat($user->created())); ?></td>
        </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>

<?php if ($userIsAllowedBatchDelete): ?>
<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 users'); ?></h3>
            <p><?php echo $translate('Are you sure you would like to delete the selected users?'); ?></p>
            <p class="error"><?php echo sprintf(
                $translate('%1$s: this action will permanently delete %2$s users 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 users'); ?></h3>
            <p><?php echo $translate('Are you sure you would like to delete all users (except yourself) on all pages of this result?'); ?></p>
            <p class="error"><?php echo sprintf(
                $translate('%1$s: this action will permanently delete %2$s users (except yourself) 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 endif; ?>

<?php else: ?>

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

<?php endif; ?>
