File size: 6,758 Bytes
6bcf797
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{% extends "admin/base.html" %}

{% block title %}Управление пользователями{% endblock %}

{% block content %}
<div class="admin-header">
    <h1><i class="fas fa-users"></i> Управление пользователями</h1>
    <div class="admin-actions">
        <form class="search-form" method="get" action="{{ url_for('admin_bp.manage_users') }}">
            <div class="input-group">
                <input type="text" class="form-control" name="search" placeholder="Поиск..."

                       value="{{ search_query }}">
                <button class="btn btn-outline-secondary" type="submit">
                    <i class="fas fa-search"></i>
                </button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Имя</th>
                        <th>Email</th>
                        <th>Дата регистрации</th>
                        <th>Статус</th>
                        <th>Действия</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users.items %}
                    <tr>
                        <td>{{ user.id }}</td>
                        <td>{{ user.username }}</td>
                        <td>{{ user.email }}</td>
                        <td>{{ user.created_at|datetimeformat }}</td>
                        <td>
                            {% if user.is_admin %}
                            <span class="badge bg-danger">Админ</span>
                            {% else %}
                            <span class="badge bg-secondary">Пользователь</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group">
                                <button class="btn btn-sm btn-{{ 'danger' if user.is_admin else 'success' }} toggle-admin"

                                        data-user-id="{{ user.id }}">
                                    {{ 'Убрать админа' if user.is_admin else 'Сделать админом' }}
                                </button>
                                {% if user.id != current_user.id %}
                                <button class="btn btn-sm btn-outline-danger delete-user"

                                        data-user-id="{{ user.id }}">
                                    <i class="fas fa-trash"></i>
                                </button>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Пагинация -->
        <nav aria-label="Page navigation">
            <ul class="pagination justify-content-center">
                {% if users.has_prev %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('admin_bp.manage_users', page=users.prev_num, search=search_query) }}">
                        &laquo;
                    </a>
                </li>
                {% endif %}

                {% for page_num in users.iter_pages(left_edge=1, right_edge=1, left_current=2, right_current=3) %}
                    {% if page_num %}
                        <li class="page-item {% if page_num == users.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('admin_bp.manage_users', page=page_num, search=search_query) }}">
                                {{ page_num }}
                            </a>
                        </li>
                    {% else %}
                        <li class="page-item disabled"><span class="page-link">...</span></li>
                    {% endif %}
                {% endfor %}

                {% if users.has_next %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('admin_bp.manage_users', page=users.next_num, search=search_query) }}">
                        &raquo;
                    </a>
                </li>
                {% endif %}
            </ul>
        </nav>
    </div>
</div>
{% endblock %}

{% block scripts %}
{{ super() }}
<script>

document.addEventListener('DOMContentLoaded', function() {

    // Функция для получения CSRF-токена из cookies

    function getCookie(name) {

        const cookieValue = document.cookie

            .split('; ')

            .find(row => row.startsWith(name + '='))

            ?.split('=')[1];

        return cookieValue ? decodeURIComponent(cookieValue) : null;

    }



    // Обработка переключения админа

    document.querySelectorAll('.toggle-admin').forEach(btn => {

        btn.addEventListener('click', function() {

            const userId = this.dataset.userId;

            fetch(`/admin/toggle_admin/${userId}`, {

                method: 'POST',

                headers: {

                    'Content-Type': 'application/json',

                    'X-CSRFToken': getCookie('csrf_token')

                }

            })

            .then(response => response.json())

            .then(data => {

                if (data.status === 'success') {

                    location.reload();

                }

            });

        });

    });



    // Обработка удаления пользователя

    document.querySelectorAll('.delete-user').forEach(btn => {

        btn.addEventListener('click', function() {

            if (!confirm('Вы уверены, что хотите удалить этого пользователя?')) return;



            const userId = this.dataset.userId;

            fetch(`/admin/delete_user/${userId}`, {

                method: 'POST',

                headers: {

                    'Content-Type': 'application/json',

                    'X-CSRFToken': getCookie('csrf_token')

                }

            })

            .then(response => response.json())

            .then(data => {

                if (data.status === 'success') {

                    location.reload();

                } else {

                    alert(data.message || 'Ошибка при удалении');

                }

            });

        });

    });

});

</script>
{% endblock %}