{% extends 'admin/index.html' %}
{% from 'bootstrap/pagination.html' import render_pagination %}

{% block title %}管理用户{% endblock %}

{% block content %}
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            {{ render_breadcrumb_item('admin.index', '控制板主页') }}
            {{ render_breadcrumb_item('admin.manage_user', '管理用户') }}
        </ol>
    </nav>
    <div class="page-header">
        <h1>用户
            <small class="text-muted">{{ pagination.total }}</small>
        </h1>
        <ul class="nav nav-pills">
            <li class="nav-item">
                <a class="nav-link disabled" href="#">过滤 </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter', 'all') == 'all' %}active{% endif %}"
                   href="{{ url_for('admin.manage_user', filter='all') }}">所有</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter') == 'locked' %}active{% endif %}"
                   href="{{ url_for('admin.manage_user', filter='locked') }}">已锁</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter') == 'blocked' %}active{% endif %}"
                   href="{{ url_for('admin.manage_user', filter='blocked') }}">堵塞</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter') == 'administrator' %}active{% endif %}"
                   href="{{ url_for('admin.manage_user', filter='administrator') }}">管理者</a>
            </li>
            <li class="nav-item">
                <a class="nav-link {% if request.args.get('filter') == 'moderator' %}active{% endif %}"
                   href="{{ url_for('admin.manage_user', filter='moderator') }}">版主</a>
            </li>
        </ul>
    </div>
    {% if users %}
        <table class="table table-striped">
            <thead>
            <tr>
                <th>图像</th>
                <th>用户名</th>
                <th>角色</th>
                <th>个人简历</th>
                <th>城市</th>
                <th>日期</th>
                <th>照片</th>
                <th>操作</th>
            </tr>
            </thead>
            {% for user in users %}
                <tr>
                    <td><img src="{{ url_for('main.get_avatar', filename=user.avatar_s) }}"></td>
                    <td>{{ user.name }}<br>{{ user.username }}</td>
                    <td>{{ user.role.name }}</td>
                    <td>{{ user.bio }}</td>
                    <td>{{ user.location }}</td>
                    <td>{{ moment(user.member_since).format('LL') }}</td>
                    <td>
                        <a href="{{ url_for('user.index', username=user.username) }}">{{ user.photos|length }}</a>
                    </td>
                    <td>
                    {% if user.role.name not in ['Administrator', 'Moderator'] %}
                        {% if user.locked %}
                            <form class="inline" action="{{ url_for('admin.unlock_user', user_id=user.id) }}"
                                  method="post">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <input type="submit" onclick="return confirm('Are you sure?');"
                                       class="btn btn-secondary btn-sm" value="未锁">
                            </form>
                        {% else %}
                            <form class="inline" action="{{ url_for('admin.lock_user', user_id=user.id) }}"
                                  method="post">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <input type="submit" onclick="return confirm('Are you sure?');"
                                       class="btn btn-warning btn-sm" value="锁住">
                            </form>
                        {% endif %}
                        {% if user.active %}
                            <form class="inline" action="{{ url_for('admin.block_user', user_id=user.id) }}"
                                  method="post">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <input type="submit" onclick="return confirm('Are you sure?');"
                                       class="btn btn-warning btn-sm" value="堵塞">
                            </form>
                        {% else %}
                            <form class="inline" action="{{ url_for('admin.unblock_user', user_id=user.id) }}"
                                  method="post">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <input type="submit" onclick="return confirm('Are you sure?');"
                                       class="btn btn-secondary btn-sm" value="未堵塞">
                            </form>
                        {% endif %}
                    {% endif %}
                        <a class="btn btn-light btn-sm" href="mailto:{{ user.email }}">邮箱</a>
                        {% if current_user.is_admin %}
                            <a class="btn btn-warning btn-sm"
                               href="{{ url_for('admin.edit_profile_admin', user_id=user.id) }}">编辑简介</a>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </table>
        <div class="page-footer">{{ render_pagination(pagination) }}</div>
    {% else %}
        <div class="tip"><h5>无用户</h5></div>
    {% endif %}
{% endblock %}