{% set page_title = _("Users") %}

{% extends theme("management/management_layout.html") %}

{% block breadcrumb %}
<ol class="breadcrumb flaskbb-breadcrumb">
    <li class="breadcrumb-item"><a href="{{ url_for('forum.index') }}">{% trans %}Forum{% endtrans %}</a></li>
    <li class="breadcrumb-item"><a href="{{ url_for('management.overview') }}">{% trans %}Management{% endtrans %}</a></li>
    <li class="breadcrumb-item active">{% trans %}Manage Users{% endtrans %}</li>
</ol>
{% endblock %}

{% block management_content %}
{%- from theme('_macros/pagination.html') import render_pagination -%}
{%- from theme('_macros/navigation.html') import navlink with context -%}
{%- from theme('_macros/form.html') import render_input_field, action_confirm -%}

<div class="col-md-3 settings-col">
    <div class="nav-sidebar">
        <ul class="nav">
            {{ navlink('management.users', _('Manage Users')) }}
            {{ navlink('management.banned_users', _('Banned Users')) }}

            {% if current_user|is_admin %}
                {{ navlink('management.add_user', _("Add User")) }}
            {% endif %}
        </ul>
    </div>
</div><!--/.col-md-3 -->

<div class="col-md-9 settings-col with-left-border">
    <div class="card settings">
        <div class="card-header settings-header">
            <div class="row">
                <div class="col-auto me-auto">
                    <span class="far fa-user"></span> {% trans %}Users{% endtrans %}
                </div>
                <div class="col-auto">
                    <button type="button" class="btn btn-white btn-xs" onclick="return window.app.show_management_search()">
                        <span class="fas fa-search"></span> {% trans %}Search{% endtrans %}
                    </button>
                </div>
            </div>
        </div>
        <div class="card-body settings-body">

            <div class="settings-search">
                <form class="search-form" role="form" method="post">
                    {{ search_form.hidden_tag() }}
                    <div class="input-group">
                        {{ render_input_field(search_form.search_query, class="form-control") }}
                        <button class="btn btn-white" type="button"><span class="fas fa-search"></span></button>
                    </div>
                </form>
            </div>

            <div class="settings-content">
                <div class="settings-meta row">
                    <div class="col-md-1 col-sm-1 col-1 meta-item"><input type="checkbox" name="rowtoggle" class="action-checkall" title="Select All"/></div>
                    <div class="col-md-2 col-sm-2 col-3 meta-item">{% trans %}Username{% endtrans %}</div>
                    <div class="col-md-2 col-sm-2 col-3 meta-item">{% trans %}Posts{% endtrans %}</div>
                    <div class="col-md-3 col-sm-3 d-none d-sm-block meta-item">{% trans %}Date registered{% endtrans %}</div>
                    <div class="col-md-2 col-sm-2 col-3 meta-item">{% trans %}Group{% endtrans %}</div>
                    <div class="col-md-2 col-sm-2 col-2">
                        <div class="dropdown">
                            <a class="dropdown-toggle text-dark text-decoration-none fw-bold" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false">
                                <span class="fa fa-fw fa-cog"></span>
                                {% trans %}Actions{% endtrans %}
                            </a>

                            <ul class="dropdown-menu">
                                <li>
                                    <button class="dropdown-item btn btn-sm" onclick="return bulk_actions.execute('{{ url_for('management.ban_user') }}')">
                                    <span class="fas fa-fw fa-flag text-warning"></span>
                                    {% trans %}Ban selected Users{% endtrans %}
                                    </button>
                                </li>

                                <li>
                                    <button class="dropdown-item btn btn-sm" onclick="return bulk_actions.execute('{{ url_for('management.unban_user') }}')">
                                        <span class="fas fa-fw fa-flag text-success"></span>
                                        {% trans %}Unban selected Users{% endtrans %}
                                    </button>
                                </li>

                                <li>
                                    <button class="dropdown-item btn btn-sm" onclick="return bulk_actions.execute('{{ url_for('management.delete_user') }}')">
                                        <span class="far fa-fw fa-trash-alt text-danger"></span>
                                        {% trans %}Delete selected Users{% endtrans %}
                                    </button>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            {% for user in users.items %}
                <div class="row settings-row hover">
                    <div class="col-md-1 col-sm-1 col-1"><input type="checkbox" name="rowid" class="action-checkbox" value="{{ user.id }}" title="Select User"/></div>
                    <div class="col-md-2 col-sm-2 col-3"><a href="{{ user.url }}">{{ user.username }}</a></div>
                    <div class="col-md-2 col-sm-2 col-3">{{ user.post_count }}</div>
                    <div class="col-md-3 col-sm-3 d-none d-sm-block">{{ user.date_joined|format_date }}</div>
                    <div class="col-md-2 col-sm-2 col-3">{{ user.primary_group.name }}</div>
                    <div class="col-2">
                    {% if current_user|can_edit_user and not user|is_admin or current_user|is_admin %}
                        <a href="{{ url_for('management.edit_user', user_id = user.id) }}" class="btn btn-icon">
                            <span class="fas fa-edit text-primary" data-bs-toggle="tooltip" title="{% trans %}Edit{% endtrans %}"></span>
                        </a>
                    {% endif %}

                    {% if current_user|can_ban_user and not user.permissions['banned'] %}
                        {{
                            action_confirm(
                                id="ban-" ~ user.id,
                                url=url_for('management.ban_user', user_id=user.id),
                                title=_("Ban"),
                                icon="fas fa-flag text-warning"
                            )
                        }}
                    {% endif %}

                    {% if current_user|can_ban_user and user.permissions['banned'] %}
                        {{
                            action_confirm(
                                id="ban-" ~ user.id,
                                url=url_for('management.unban_user', user_id=user.id),
                                title=_("Unban"),
                                icon="fas fa-flag text-success"
                            )
                        }}
                    {% endif %}

                    {% if current_user|is_admin %}
                        {{
                            action_confirm(
                                id="delete-" ~ user.id,
                                url=url_for('management.delete_user', user_id=user.id),
                                title=_("Delete"),
                                icon="far fa-trash-alt text-danger"
                            )
                        }}
                    {% endif %}
                    </div>
                </div>
            {% else %}
                <div class="row settings-row">
                    <div class="col-12">{% trans %}No users found matching your search criteria.{% endtrans %}</div>
                </div>
            {% endfor %}

            </div> <!-- end settings content -->
        </div>

        <div class="card-footer settings-footer p-2">
            {{ render_pagination(users, url_for('management.users')) }}
        </div>
    </div>
</div>
{% include theme('_partials/confirm_dialog.html') %}
{% endblock %}

{% block scripts %}
    <script>
    var bulk_actions = new window.app.BulkActions();
    </script>
{% endblock %}
