Spaces:
Running
Running
Update templates/admin_dashboard.html
Browse files- templates/admin_dashboard.html +364 -452
templates/admin_dashboard.html
CHANGED
|
@@ -1,453 +1,365 @@
|
|
| 1 |
-
<!DOCTYPE html>
|
| 2 |
-
<html lang="vi">
|
| 3 |
-
|
| 4 |
-
<
|
| 5 |
-
<meta
|
| 6 |
-
<
|
| 7 |
-
<
|
| 8 |
-
|
| 9 |
-
<link href="
|
| 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 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
<
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
const
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
}
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
}
|
| 366 |
-
}
|
| 367 |
-
|
| 368 |
-
async function resetQuery(userId) {
|
| 369 |
-
const response = await fetch(`/admin/user/${userId}/reset_query`, {
|
| 370 |
-
method: 'POST',
|
| 371 |
-
headers: { 'Content-Type': 'application/json' }
|
| 372 |
-
});
|
| 373 |
-
const result = await response.json();
|
| 374 |
-
alert(result.message || result.error);
|
| 375 |
-
location.reload();
|
| 376 |
-
}
|
| 377 |
-
|
| 378 |
-
function openUpdateModal(userId, accountType, isAdmin, queryLimit) {
|
| 379 |
-
document.getElementById('updateUserId').value = userId;
|
| 380 |
-
document.getElementById('updateAccountType').value = accountType;
|
| 381 |
-
document.getElementById('updateIsAdmin').checked = isAdmin;
|
| 382 |
-
document.getElementById('updateQueryLimit').value = queryLimit || '';
|
| 383 |
-
toggleQueryLimit();
|
| 384 |
-
const modal = new bootstrap.Modal(document.getElementById('updateModal'));
|
| 385 |
-
modal.show();
|
| 386 |
-
}
|
| 387 |
-
|
| 388 |
-
function closeUpdateModal() {
|
| 389 |
-
const modal = bootstrap.Modal.getInstance(document.getElementById('updateModal'));
|
| 390 |
-
modal.hide();
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
function toggleQueryLimit() {
|
| 394 |
-
const accountType = document.getElementById('updateAccountType').value;
|
| 395 |
-
const queryLimitInput = document.getElementById('updateQueryLimit');
|
| 396 |
-
queryLimitInput.disabled = accountType === 'unlimited';
|
| 397 |
-
if (accountType === 'unlimited') {
|
| 398 |
-
queryLimitInput.value = '';
|
| 399 |
-
}
|
| 400 |
-
}
|
| 401 |
-
|
| 402 |
-
async function logout() {
|
| 403 |
-
try {
|
| 404 |
-
const response = await fetch('/logout', {
|
| 405 |
-
method: 'POST',
|
| 406 |
-
headers: { 'Content-Type': 'application/json' }
|
| 407 |
-
});
|
| 408 |
-
if (response.ok) {
|
| 409 |
-
// Clear client-side state (adapted for dashboard context)
|
| 410 |
-
// Variables like currentUser, conversations, etc., may not exist here
|
| 411 |
-
// If needed, define them or remove irrelevant ones
|
| 412 |
-
alert('Đăng xuất thành công');
|
| 413 |
-
window.location.href = '/';
|
| 414 |
-
} else {
|
| 415 |
-
alert('Lỗi khi đăng xuất');
|
| 416 |
-
}
|
| 417 |
-
} catch (error) {
|
| 418 |
-
alert('Lỗi khi đăng xuất: ' + error.message);
|
| 419 |
-
}
|
| 420 |
-
}
|
| 421 |
-
|
| 422 |
-
document.getElementById('updateForm').addEventListener('submit', async (e) => {
|
| 423 |
-
e.preventDefault();
|
| 424 |
-
const userId = document.getElementById('updateUserId').value;
|
| 425 |
-
const accountType = document.getElementById('updateAccountType').value;
|
| 426 |
-
const isAdmin = document.getElementById('updateIsAdmin').checked;
|
| 427 |
-
const queryLimit = document.getElementById('updateQueryLimit').value;
|
| 428 |
-
if (accountType === 'limited' && queryLimit && !/^\d+$/.test(queryLimit)) {
|
| 429 |
-
alert('Giới hạn hỏi đáp phải là một số nguyên dương.');
|
| 430 |
-
return;
|
| 431 |
-
}
|
| 432 |
-
const updates = { account_type: accountType, is_admin: isAdmin };
|
| 433 |
-
if (accountType === 'limited' && queryLimit) {
|
| 434 |
-
updates.query_limit = parseInt(queryLimit);
|
| 435 |
-
}
|
| 436 |
-
try {
|
| 437 |
-
const response = await fetch(`/admin/user/${userId}`, {
|
| 438 |
-
method: 'PUT',
|
| 439 |
-
headers: { 'Content-Type': 'application/json' },
|
| 440 |
-
body: JSON.stringify(updates)
|
| 441 |
-
});
|
| 442 |
-
const result = await response.json();
|
| 443 |
-
alert(result.message || result.error);
|
| 444 |
-
closeUpdateModal();
|
| 445 |
-
location.reload();
|
| 446 |
-
} catch (error) {
|
| 447 |
-
alert('Lỗi khi cập nhật người dùng: ' + error.message);
|
| 448 |
-
}
|
| 449 |
-
});
|
| 450 |
-
</script>
|
| 451 |
-
</body>
|
| 452 |
-
|
| 453 |
</html>
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="vi">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Quản lý người dùng</title>
|
| 7 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
| 8 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
|
| 9 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style_admin.css') }}">
|
| 10 |
+
</head>
|
| 11 |
+
<body>
|
| 12 |
+
<div class="sidebar" id="sidebar">
|
| 13 |
+
<div class="sidebar-header">Quản lý</div>
|
| 14 |
+
<hr>
|
| 15 |
+
<nav class="sidebar-nav">
|
| 16 |
+
<ul>
|
| 17 |
+
<li><a href="#" id="dashboardLink"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
|
| 18 |
+
<li><a href="#" id="usersLink" class="active"><i class="fas fa-users"></i> Người dùng</a></li>
|
| 19 |
+
<li><a href="#" id="pendingUsersLink"><i class="fas fa-user-clock"></i> Người dùng chờ xác thực</a></li>
|
| 20 |
+
<li><a href="#"><i class="fas fa-cog"></i> Cài đặt</a></li>
|
| 21 |
+
<li><a href="#"><i class="fas fa-chart-bar"></i> Báo cáo</a></li>
|
| 22 |
+
<li><a href="#"><i class="fas fa-user-circle"></i> Hồ sơ</a></li>
|
| 23 |
+
<li><a href="#" onclick="logout()"><i class="fas fa-sign-out-alt"></i> Đăng xuất</a></li>
|
| 24 |
+
</ul>
|
| 25 |
+
</nav>
|
| 26 |
+
</div>
|
| 27 |
+
|
| 28 |
+
<div class="main-content" id="mainContent">
|
| 29 |
+
<div class="top-bar">
|
| 30 |
+
<button class="menu-toggle" id="menuToggle"><i class="fas fa-bars"></i></button>
|
| 31 |
+
<span>Xin chào, {{ user_name | default('Admin') | e }}!</span>
|
| 32 |
+
<button onclick="logout()">Đăng xuất</button>
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
<div class="content-area" id="contentArea">
|
| 36 |
+
<!-- Users Tab -->
|
| 37 |
+
<div id="usersTab">
|
| 38 |
+
<div class="content-header">
|
| 39 |
+
<h1>Danh sách người dùng</h1>
|
| 40 |
+
<button class="add-new-btn"><i class="fas fa-plus"></i> Thêm mới</button>
|
| 41 |
+
</div>
|
| 42 |
+
<div class="search-bar">
|
| 43 |
+
<i class="fas fa-search"></i>
|
| 44 |
+
<input type="text" placeholder="Tìm kiếm người dùng...">
|
| 45 |
+
</div>
|
| 46 |
+
<div class="table-responsive">
|
| 47 |
+
<table class="table table-striped table-bordered" id="userTable">
|
| 48 |
+
<thead class="table-success">
|
| 49 |
+
<tr>
|
| 50 |
+
<th>ID</th>
|
| 51 |
+
<th>Tên người dùng</th>
|
| 52 |
+
<th>Vai trò</th>
|
| 53 |
+
<th>Email</th>
|
| 54 |
+
<th>Số điện thoại</th>
|
| 55 |
+
<th>Loại tài khoản</th>
|
| 56 |
+
<th>Lượt hỏi đáp</th>
|
| 57 |
+
<th>Hành động</th>
|
| 58 |
+
</tr>
|
| 59 |
+
</thead>
|
| 60 |
+
<tbody>
|
| 61 |
+
{% for user in users %}
|
| 62 |
+
<tr>
|
| 63 |
+
<td data-label="ID">{{ user.id }}</td>
|
| 64 |
+
<td data-label="Tên người dùng">{{ user.username | e }}</td>
|
| 65 |
+
<td data-label="Vai trò">{% if user.is_admin %}Admin{% else %}User{% endif %}</td>
|
| 66 |
+
<td data-label="Email">{{ user.email | e }}</td>
|
| 67 |
+
<td data-label="Số điện thoại">{{ user.phone | e }}</td>
|
| 68 |
+
<td data-label="Loại tài khoản">
|
| 69 |
+
{% if user.account_type == 'limited' %}
|
| 70 |
+
<span class="badge badge-pill badge-warning">Giới hạn</span>
|
| 71 |
+
{% else %}
|
| 72 |
+
<span class="badge badge-pill badge-success">Không giới hạn</span>
|
| 73 |
+
{% endif %}
|
| 74 |
+
</td>
|
| 75 |
+
<td data-label="Lượt hỏi đáp">
|
| 76 |
+
{% if user.account_type == 'unlimited' %}
|
| 77 |
+
<span class="query-normal">Không giới hạn</span>
|
| 78 |
+
{% elif user.query_limit %}
|
| 79 |
+
<span class="query-normal">{{ user.query_count }}/{{ user.query_limit | e }}</span>
|
| 80 |
+
{% else %}
|
| 81 |
+
<span class="query-normal">{{ user.query_count }}/{{ user.account_type | e }}</span>
|
| 82 |
+
{% endif %}
|
| 83 |
+
</td>
|
| 84 |
+
<td data-label="Hành động" class="action-buttons">
|
| 85 |
+
<button class="edit-btn update-btn" onclick="openUpdateModal('{{ user.id | e }}', '{{ user.account_type | e }}', {{ 'true' if user.is_admin else 'false' }}, '{{ user.query_limit | default('') | e }}')">
|
| 86 |
+
<i class="fas fa-pencil-alt"></i>
|
| 87 |
+
</button>
|
| 88 |
+
<button class="delete-btn" onclick="deleteUser('{{ user.id | e }}')">
|
| 89 |
+
<i class="fas fa-trash-alt"></i>
|
| 90 |
+
</button>
|
| 91 |
+
<button class="reset-btn" onclick="resetQuery('{{ user.id | e }}')">
|
| 92 |
+
<i class="fas fa-redo-alt"></i>
|
| 93 |
+
</button>
|
| 94 |
+
</td>
|
| 95 |
+
</tr>
|
| 96 |
+
{% endfor %}
|
| 97 |
+
</tbody>
|
| 98 |
+
</table>
|
| 99 |
+
</div>
|
| 100 |
+
</div>
|
| 101 |
+
|
| 102 |
+
<!-- Pending Users Tab -->
|
| 103 |
+
<div id="pendingUsersTab" style="display: none;">
|
| 104 |
+
<div class="content-header">
|
| 105 |
+
<h1>Danh sách người dùng chờ xác thực</h1>
|
| 106 |
+
</div>
|
| 107 |
+
<div class="table-responsive">
|
| 108 |
+
<table class="table table-striped table-bordered" id="pendingUserTable">
|
| 109 |
+
<thead class="table-success">
|
| 110 |
+
<tr>
|
| 111 |
+
<th>ID</th>
|
| 112 |
+
<th>Tên người dùng</th>
|
| 113 |
+
<th>Email</th>
|
| 114 |
+
<th>Số điện thoại</th>
|
| 115 |
+
<th>Loại tài khoản</th>
|
| 116 |
+
<th>Ngày đăng ký</th>
|
| 117 |
+
<th>Hành động</th>
|
| 118 |
+
</tr>
|
| 119 |
+
</thead>
|
| 120 |
+
<tbody id="pendingUsersBody">
|
| 121 |
+
<!-- Populated by JavaScript -->
|
| 122 |
+
</tbody>
|
| 123 |
+
</table>
|
| 124 |
+
</div>
|
| 125 |
+
</div>
|
| 126 |
+
</div>
|
| 127 |
+
</div>
|
| 128 |
+
|
| 129 |
+
<!-- Update Modal (Existing) -->
|
| 130 |
+
<div class="modal fade" id="updateModal" tabindex="-1" aria-labelledby="updateModalLabel" aria-hidden="true">
|
| 131 |
+
<div class="modal-dialog">
|
| 132 |
+
<div class="modal-content">
|
| 133 |
+
<div class="modal-header">
|
| 134 |
+
<h5 class="modal-title" id="updateModalLabel">Cập nhật người dùng</h5>
|
| 135 |
+
<button type="button" class="btn-close" onclick="closeUpdateModal()" aria-label="Close"></button>
|
| 136 |
+
</div>
|
| 137 |
+
<div class="modal-body">
|
| 138 |
+
<form id="updateForm">
|
| 139 |
+
<input type="hidden" id="updateUserId">
|
| 140 |
+
<div class="mb-3">
|
| 141 |
+
<label for="updateAccountType" class="form-label">Loại tài khoản:</label>
|
| 142 |
+
<select id="updateAccountType" class="form-select" onchange="toggleQueryLimit()">
|
| 143 |
+
<option value="limited">Limited</option>
|
| 144 |
+
<option value="unlimited">Unlimited</option>
|
| 145 |
+
</select>
|
| 146 |
+
</div>
|
| 147 |
+
<div class="mb-3 form-check">
|
| 148 |
+
<input type="checkbox" class="form-check-input" id="updateIsAdmin">
|
| 149 |
+
<label class="form-check-label" for="updateIsAdmin">Admin</label>
|
| 150 |
+
</div>
|
| 151 |
+
<div class="mb-3">
|
| 152 |
+
<label for="updateQueryLimit class="form-label">Giới hạn hỏi đáp (cho tài khoản limited):</label>
|
| 153 |
+
<input type="number" class="form-control" id="updateQueryLimit" min="1" placeholder="Nhập giới hạn (ví dụ: 10)">
|
| 154 |
+
</div>
|
| 155 |
+
<div class="d-flex justify-content-end">
|
| 156 |
+
<button type="submit" class="btn btn-success me-2">Lưu</button>
|
| 157 |
+
<button type="button" class="btn btn-secondary cancel-btn" onclick="closeUpdateModal()">Hủy</button>
|
| 158 |
+
</div>
|
| 159 |
+
</form>
|
| 160 |
+
</div>
|
| 161 |
+
</div>
|
| 162 |
+
</div>
|
| 163 |
+
</div>
|
| 164 |
+
|
| 165 |
+
<!-- jQuery and Bootstrap JS -->
|
| 166 |
+
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
| 167 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
|
| 168 |
+
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
|
| 169 |
+
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
|
| 170 |
+
<script>
|
| 171 |
+
// Sidebar toggle
|
| 172 |
+
document.getElementById('menuToggle').addEventListener('click', () => {
|
| 173 |
+
const sidebar = document.getElementById('sidebar');
|
| 174 |
+
const mainContent = document.getElementById('mainContent');
|
| 175 |
+
sidebar.classList.toggle('collapsed');
|
| 176 |
+
mainContent.classList.toggle('shifted');
|
| 177 |
+
});
|
| 178 |
+
|
| 179 |
+
// Tab switching
|
| 180 |
+
document.getElementById('usersLink').addEventListener('click', (e) => {
|
| 181 |
+
e.preventDefault();
|
| 182 |
+
document.getElementById('usersTab').style.display = 'block';
|
| 183 |
+
document.getElementById('pendingUsersTab').style.display = 'none';
|
| 184 |
+
document.getElementById('usersLink').classList.add('active');
|
| 185 |
+
document.getElementById('pendingUsersLink').classList.remove('active');
|
| 186 |
+
});
|
| 187 |
+
|
| 188 |
+
document.getElementById('pendingUsersLink').addEventListener('click', (e) => {
|
| 189 |
+
e.preventDefault();
|
| 190 |
+
document.getElementById('usersTab').style.display = 'none';
|
| 191 |
+
document.getElementById('pendingUsersTab').style.display = 'block';
|
| 192 |
+
document.getElementById('usersLink').classList.remove('active');
|
| 193 |
+
document.getElementById('pendingUsersLink').classList.add('active');
|
| 194 |
+
loadPendingUsers();
|
| 195 |
+
});
|
| 196 |
+
|
| 197 |
+
// Initialize DataTables for users
|
| 198 |
+
$(document).ready(function () {
|
| 199 |
+
$('#userTable').DataTable({
|
| 200 |
+
language: { url: '//cdn.datatables.net/plug-ins/1.13.6/i18n/vi.json' },
|
| 201 |
+
pageLength: 10,
|
| 202 |
+
responsive: true,
|
| 203 |
+
columnDefs: [{ orderable: false, targets: 6 }]
|
| 204 |
+
});
|
| 205 |
+
|
| 206 |
+
$('#pendingUserTable').DataTable({
|
| 207 |
+
language: { url: '//cdn.datatables.net/plug-ins/1.13.6/i18n/vi.json' },
|
| 208 |
+
pageLength: 10,
|
| 209 |
+
responsive: true,
|
| 210 |
+
columnDefs: [{ orderable: false, targets: 6 }],
|
| 211 |
+
searching: false
|
| 212 |
+
});
|
| 213 |
+
});
|
| 214 |
+
|
| 215 |
+
// Load pending users
|
| 216 |
+
async function loadPendingUsers() {
|
| 217 |
+
try {
|
| 218 |
+
const response = await fetch('/admin/pending_users', {
|
| 219 |
+
method: 'GET',
|
| 220 |
+
headers: { 'Content-Type': 'application/json' }
|
| 221 |
+
});
|
| 222 |
+
const users = await response.json();
|
| 223 |
+
const tbody = document.getElementById('pendingUsersBody');
|
| 224 |
+
tbody.innerHTML = '';
|
| 225 |
+
users.forEach(user => {
|
| 226 |
+
const row = `
|
| 227 |
+
<tr>
|
| 228 |
+
<td data-label="ID">${user.id}</td>
|
| 229 |
+
<td data-label="Tên người dùng">${user.username}</td>
|
| 230 |
+
<td data-label="Email">${user.email}</td>
|
| 231 |
+
<td data-label="Số điện thoại">${user.phone}</td>
|
| 232 |
+
<td data-label="Loại tài khoản">
|
| 233 |
+
<span class="badge badge-pill ${user.account_type === 'limited' ? 'badge-warning' : 'badge-success'}">
|
| 234 |
+
${user.account_type === 'limited' ? 'Giới hạn' : 'Không giới hạn'}
|
| 235 |
+
</span>
|
| 236 |
+
</td>
|
| 237 |
+
<td data-label="Ngày đăng ký">${user.created_at}</td>
|
| 238 |
+
<td data-label="Hành động" class="action-buttons">
|
| 239 |
+
<button class="btn btn-success btn-sm" onclick="verifyUser('${user.id}', 'approve')">
|
| 240 |
+
<i class="fas fa-check"></i> Chấp nhận
|
| 241 |
+
</button>
|
| 242 |
+
<button class="btn btn-danger btn-sm" onclick="verifyUser('${user.id}', 'reject')">
|
| 243 |
+
<i class="fas fa-times"></i> Từ chối
|
| 244 |
+
</button>
|
| 245 |
+
</td>
|
| 246 |
+
</tr>`;
|
| 247 |
+
tbody.insertAdjacentHTML('beforeend', row);
|
| 248 |
+
});
|
| 249 |
+
} catch (error) {
|
| 250 |
+
alert('Lỗi khi tải danh sách người dùng chờ xác thực: ' + error.message);
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
// Verify user (approve/reject)
|
| 255 |
+
async function verifyUser(userId, action) {
|
| 256 |
+
if (!confirm(`Bạn có chắc muốn ${action === 'approve' ? 'chấp nhận' : 'từ chối'} người dùng này?`)) return;
|
| 257 |
+
try {
|
| 258 |
+
const response = await fetch(`/admin/user/${userId}/verify`, {
|
| 259 |
+
method: 'POST',
|
| 260 |
+
headers: { 'Content-Type': 'application/json' },
|
| 261 |
+
body: JSON.stringify({ action })
|
| 262 |
+
});
|
| 263 |
+
const result = await response.json();
|
| 264 |
+
alert(result.message || result.error);
|
| 265 |
+
loadPendingUsers(); // Refresh pending users list
|
| 266 |
+
} catch (error) {
|
| 267 |
+
alert('Lỗi khi xử lý: ' + error.message);
|
| 268 |
+
}
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
// Existing functions (deleteUser, resetQuery, openUpdateModal, etc.)
|
| 272 |
+
async function deleteUser(userId) {
|
| 273 |
+
if (confirm('Bạn có chắc muốn xóa người dùng này?')) {
|
| 274 |
+
const response = await fetch(`/admin/user/${userId}`, {
|
| 275 |
+
method: 'DELETE',
|
| 276 |
+
headers: { 'Content-Type': 'application/json' }
|
| 277 |
+
});
|
| 278 |
+
const result = await response.json();
|
| 279 |
+
alert(result.message || result.error);
|
| 280 |
+
location.reload();
|
| 281 |
+
}
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
async function resetQuery(userId) {
|
| 285 |
+
const response = await fetch(`/admin/user/${userId}/reset_query`, {
|
| 286 |
+
method: 'POST',
|
| 287 |
+
headers: { 'Content-Type': 'application/json' }
|
| 288 |
+
});
|
| 289 |
+
const result = await response.json();
|
| 290 |
+
alert(result.message || result.error);
|
| 291 |
+
location.reload();
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
function openUpdateModal(userId, accountType, isAdmin, queryLimit) {
|
| 295 |
+
document.getElementById('updateUserId').value = userId;
|
| 296 |
+
document.getElementById('updateAccountType').value = accountType;
|
| 297 |
+
document.getElementById('updateIsAdmin').checked = isAdmin;
|
| 298 |
+
document.getElementById('updateQueryLimit').value = queryLimit || '';
|
| 299 |
+
toggleQueryLimit();
|
| 300 |
+
const modal = new bootstrap.Modal(document.getElementById('updateModal'));
|
| 301 |
+
modal.show();
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
function closeUpdateModal() {
|
| 305 |
+
const modal = bootstrap.Modal.getInstance(document.getElementById('updateModal'));
|
| 306 |
+
modal.hide();
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
function toggleQueryLimit() {
|
| 310 |
+
const accountType = document.getElementById('updateAccountType').value;
|
| 311 |
+
const queryLimitInput = document.getElementById('updateQueryLimit');
|
| 312 |
+
queryLimitInput.disabled = accountType === 'unlimited';
|
| 313 |
+
if (accountType === 'unlimited') {
|
| 314 |
+
queryLimitInput.value = '';
|
| 315 |
+
}
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
async function logout() {
|
| 319 |
+
try {
|
| 320 |
+
const response = await fetch('/logout', {
|
| 321 |
+
method: 'POST',
|
| 322 |
+
headers: { 'Content-Type': 'application/json' }
|
| 323 |
+
});
|
| 324 |
+
if (response.ok) {
|
| 325 |
+
alert('Đăng xuất thành công');
|
| 326 |
+
window.location.href = '/';
|
| 327 |
+
} else {
|
| 328 |
+
alert('Lỗi khi đăng xuất');
|
| 329 |
+
}
|
| 330 |
+
} catch (error) {
|
| 331 |
+
alert('Lỗi khi đăng xuất: ' + error.message);
|
| 332 |
+
}
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
document.getElementById('updateForm').addEventListener('submit', async (e) => {
|
| 336 |
+
e.preventDefault();
|
| 337 |
+
const userId = document.getElementById('updateUserId').value;
|
| 338 |
+
const accountType = document.getElementById('updateAccountType').value;
|
| 339 |
+
const isAdmin = document.getElementById('updateIsAdmin').checked;
|
| 340 |
+
const queryLimit = document.getElementById('updateQueryLimit').value;
|
| 341 |
+
if (accountType === 'limited' && queryLimit && !/^\d+$/.test(queryLimit)) {
|
| 342 |
+
alert('Giới hạn hỏi đáp phải là một số nguyên dương.');
|
| 343 |
+
return;
|
| 344 |
+
}
|
| 345 |
+
const updates = { account_type: accountType, is_admin: isAdmin };
|
| 346 |
+
if (accountType === 'limited' && queryLimit) {
|
| 347 |
+
updates.query_limit = parseInt(queryLimit);
|
| 348 |
+
}
|
| 349 |
+
try {
|
| 350 |
+
const response = await fetch(`/admin/user/${userId}`, {
|
| 351 |
+
method: 'PUT',
|
| 352 |
+
headers: { 'Content-Type': 'application/json' },
|
| 353 |
+
body: JSON.stringify(updates)
|
| 354 |
+
});
|
| 355 |
+
const result = await response.json();
|
| 356 |
+
alert(result.message || result.error);
|
| 357 |
+
closeUpdateModal();
|
| 358 |
+
location.reload();
|
| 359 |
+
} catch (error) {
|
| 360 |
+
alert('Lỗi khi cập nhật người dùng: ' + error.message);
|
| 361 |
+
}
|
| 362 |
+
});
|
| 363 |
+
</script>
|
| 364 |
+
</body>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
</html>
|