<?php

/**
 * This file is part of JohnCMS Content Management System.
 *
 * @copyright JohnCMS Community
 * @license   https://opensource.org/licenses/GPL-3.0 GPL-3.0
 * @link      https://johncms.com JohnCMS Project
 */

/**
 * @var $title
 * @var $page_title
 * @var $curators_list
 * @var $back_url
 * @var $topic
 * @var $saved
 * @var $selected_user_ids
 * @var $set_filter_action
 * @var $clear_filter_action
 * @var $csrf_token
 */

$this->layout(
    'system::layout/default',
    [
        'title'      => $title,
        'page_title' => $page_title,
    ]
);
?>

<?php if ($saved): ?>
    <div class="alert alert-success"><?= __('Changes saved') ?></div>
<?php endif; ?>
<form action="<?= $set_filter_action ?>" method="post">
    <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
    <?php if (empty($list)): ?>
        <div class="alert alert-info">
            <?= __('The list is empty') ?>
        </div>
    <?php else: ?>
        <div class="alert alert-warning mb-3 shadow"><?= __('Filter will be display posts from selected authors only') ?></div>
        <div class="card shadow mb-3">
            <div class="card-body">
                <div class="list-group list-group-flush">
                    <?php foreach ($list as $item): ?>
                        <div class="list-group-item d-flex justify-content-between align-items-center">
                            <div class="custom-control custom-checkbox">
                                <input class="custom-control-input" type="checkbox" name="users[]" value="<?= $item['user_id'] ?>"
                                       id="user_<?= $item['user_id'] ?>"<?= in_array($item['user_id'], $selected_user_ids ?? [], true) ? ' checked' : '' ?>>
                                <label class="custom-control-label" for="user_<?= $item['user_id'] ?>"><?= $item['user_name'] ?></label>
                            </div>
                            <div class="badge rounded-pill text-bg-secondary"><?= $item['count'] ?></div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>
    <div>
        <?php if (! empty($list)): ?>
            <button type="submit" name="submit" value="1" class="btn btn-primary"><?= __('Filter') ?></button>
            <?php if (! empty($selected_user_ids)): ?>
                <button type="submit" formaction="<?= $clear_filter_action ?>" class="btn btn-outline-secondary"><?= __('Cancel Filter') ?></button>
            <?php endif; ?>
        <?php endif; ?>
        <a href="<?= $back_url ?>" class="btn btn-secondary"><?= __('Back to topic') ?></a>
    </div>
</form>
