File size: 25,711 Bytes
128909e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员面板</title>
    <link rel="stylesheet" href="../css/main.css">
    <link rel="stylesheet" href="../css/loader.css">
    <link rel="stylesheet" href="../css/bg_override.css">
    <link rel="stylesheet" href="../css/fa.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
    <style>
        .admin-container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 30px;
            background-color: var(--black80a);
            border-radius: 15px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
        }
        .users-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 25px;
            margin-top: 25px;
        }
        .user-card {
            background-color: var(--black60a);
            border-radius: 15px;
            padding: 25px;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        }
        .user-card:hover {
            background-color: var(--black90a);
            transform: translateY(-5px);
            box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
            border-color: rgba(255, 255, 255, 0.2);
        }
        .user-header {
            display: flex;
            align-items: center;
            gap: 20px;
            margin-bottom: 20px;
        }
        .user-avatar {
            width: 70px;
            height: 70px;
            border-radius: 50%;
            object-fit: cover;
            border: 3px solid var(--accent);
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
        }
        .user-card:hover .user-avatar {
            transform: scale(1.05);
            border-color: #e74c3c;
        }
        .user-info {
            flex-grow: 1;
        }
        .user-name {
            font-size: 1.3em;
            font-weight: bold;
            margin: 0 0 8px 0;
            color: var(--white95);
        }
        .user-handle {
            color: var(--grey70);
            font-size: 0.95em;
            margin-bottom: 12px;
            opacity: 0.8;
        }
        .badge {
            display: inline-block;
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.85em;
            margin-right: 8px;
            font-weight: 500;
            letter-spacing: 0.3px;
            text-transform: uppercase;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        .badge.admin {
            background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
            color: white;
            min-width: 70px;
            text-align: center;
        }
        .badge.user {
            background: linear-gradient(135deg, var(--black60a) 0%, var(--black90a) 100%);
            color: var(--grey70);
            min-width: 70px;
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        .badge.enabled {
            background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
            color: white;
        }
        .badge.disabled {
            background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
            color: white;
        }
        .user-actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }
        .action-btn {
            padding: 10px 15px;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            font-size: 0.9em;
            display: flex;
            align-items: center;
            gap: 8px;
            background-color: var(--black60a);
            color: var(--white90);
            transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
            border: 1px solid rgba(255, 255, 255, 0.1);
            flex: 1;
            justify-content: center;
            min-width: 100px;
        }
        .action-btn:hover {
            background-color: var(--black90a);
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
            border-color: rgba(255, 255, 255, 0.2);
        }
        .action-btn.danger {
            background: linear-gradient(135deg, var(--warning50a) 0%, var(--warning70a) 100%);
        }
        .action-btn.danger:hover {
            background: linear-gradient(135deg, var(--warning70a) 0%, var(--warning90a) 100%);
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 12px;
            margin-top: 40px;
            flex-wrap: wrap;
        }
        .page-btn {
            padding: 10px 18px;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            background-color: var(--black60a);
            color: var(--white90);
            transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
            border: 1px solid rgba(255, 255, 255, 0.1);
            font-weight: 500;
        }
        .page-btn:hover:not(.active):not(:disabled) {
            background-color: var(--black90a);
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
            border-color: rgba(255, 255, 255, 0.2);
        }
        .page-btn.active {
            background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
            font-weight: bold;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        .page-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        .filters {
            display: flex;
            gap: 20px;
            margin-bottom: 30px;
            flex-wrap: wrap;
            background-color: var(--black60a);
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        .search-box {
            flex-grow: 1;
            padding: 12px 20px;
            border-radius: 8px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            background-color: var(--black60a);
            color: var(--white90);
            font-size: 1em;
            transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
        }
        .search-box:focus {
            outline: none;
            border-color: var(--accent);
            background-color: var(--black90a);
            box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2);
        }
        .filter-select {
            padding: 12px 20px;
            border-radius: 8px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            background-color: var(--black60a);
            color: var(--white90);
            font-size: 1em;
            cursor: pointer;
            transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
            min-width: 150px;
        }
        .filter-select:focus {
            outline: none;
            border-color: var(--accent);
            background-color: var(--black90a);
            box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2);
        }
        #loading {
            display: none;
            text-align: center;
            margin: 40px 0;
            font-size: 1.2em;
            color: var(--grey70);
        }
        .page-title {
            color: var(--white95);
            margin-bottom: 40px;
            font-size: 2.2em;
            text-align: center;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
            font-weight: 700;
            letter-spacing: 0.5px;
        }
        .page-jump {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-left: 15px;
            background-color: var(--black60a);
            padding: 8px 15px;
            border-radius: 8px;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        .page-jump input {
            width: 70px;
            padding: 8px 12px;
            border-radius: 6px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            background-color: var(--black60a);
            color: var(--white90);
            text-align: center;
            font-size: 0.95em;
        }
        .page-jump input:focus {
            outline: none;
            border-color: var(--accent);
            box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2);
        }
        .page-jump button {
            padding: 8px 15px;
            border-radius: 6px;
            border: none;
            background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
            color: white;
            cursor: pointer;
            transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
            font-weight: 500;
        }
        .page-jump button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
        }
    </style>
</head>
<body>
    <div class="admin-container">
        <h1 class="page-title">管理员面板</h1>
        <div class="filters">
            <input type="text" class="search-box" id="searchInput" placeholder="搜索用户...">
            <select class="filter-select" id="roleFilter">
                <option value="">所有角色</option>
                <option value="admin">管理员</option>
                <option value="user">普通用户</option>
            </select>
            <select class="filter-select" id="statusFilter">
                <option value="">所有状态</option>
                <option value="enabled">已启用</option>
                <option value="disabled">已禁用</option>
            </select>
        </div>
        <div class="users-grid" id="usersGrid"></div>
        <div id="loading">加载中...</div>
        <div class="pagination" id="pagination"></div>
    </div>

    <script>
        // 配置toastr
        toastr.options = {
            "closeButton": true,
            "debug": false,
            "newestOnTop": true,
            "progressBar": true,
            "positionClass": "toast-top-right",
            "preventDuplicates": false,
            "onclick": null,
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        };

        const PAGE_SIZE = 12;
        let currentPage = 1;
        let totalPages = 1;
        let allUsers = [];
        let filteredUsers = [];
        let csrfToken = '';

        async function getCsrfToken() {
            try {
                const response = await fetch('/csrf-token');
                if (!response.ok) throw new Error('获取CSRF令牌失败');
                const data = await response.json();
                csrfToken = data.token;
            } catch (error) {
                console.error('获取CSRF令牌错误:', error);
                toastr.error('获取CSRF令牌失败。请刷新页面重试。');
            }
        }

        async function fetchUsers() {
            try {
                document.getElementById('loading').style.display = 'block';
                const response = await fetch('/api/users-internal/list', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRF-Token': csrfToken
                    }
                });
                
                if (!response.ok) {
                    throw new Error('获取用户列表失败');
                }

                allUsers = await response.json();
                applyFilters();
            } catch (error) {
                console.error('获取用户列表错误:', error);
                toastr.error('获取用户列表失败。请重试。');
            } finally {
                document.getElementById('loading').style.display = 'none';
            }
        }

        function applyFilters() {
            const searchTerm = document.getElementById('searchInput').value.toLowerCase();
            const roleFilter = document.getElementById('roleFilter').value;
            const statusFilter = document.getElementById('statusFilter').value;

            filteredUsers = allUsers.filter(user => {
                const matchesSearch = user.name.toLowerCase().includes(searchTerm) || 
                                   user.handle.toLowerCase().includes(searchTerm);
                const matchesRole = !roleFilter || 
                                  (roleFilter === 'admin' && user.admin) || 
                                  (roleFilter === 'user' && !user.admin);
                const matchesStatus = !statusFilter || 
                                    (statusFilter === 'enabled' && user.enabled) || 
                                    (statusFilter === 'disabled' && !user.enabled);
                
                return matchesSearch && matchesRole && matchesStatus;
            });

            totalPages = Math.ceil(filteredUsers.length / PAGE_SIZE);
            currentPage = Math.min(currentPage, totalPages);
            if (currentPage < 1) currentPage = 1;

            renderUsers();
            renderPagination();
        }

        function renderUsers() {
            const grid = document.getElementById('usersGrid');
            grid.innerHTML = '';

            const start = (currentPage - 1) * PAGE_SIZE;
            const end = start + PAGE_SIZE;
            const pageUsers = filteredUsers.slice(start, end);

            pageUsers.forEach(user => {
                const card = document.createElement('div');
                card.className = 'user-card';
                card.innerHTML = `
                    <div class="user-header">
                        <img src="${user.avatar || '../img/ai4.png'}" alt="头像" class="user-avatar">
                        <div class="user-info">
                            <h3 class="user-name">${user.name}</h3>
                            <div class="user-handle">${user.handle}</div>
                            <div>
                                <span class="badge ${user.admin ? 'admin' : 'user'}">${user.admin ? '管理员' : '普通用户'}</span>
                                <span class="badge ${user.enabled ? 'enabled' : 'disabled'}">${user.enabled ? '已启用' : '已禁用'}</span>
                            </div>
                        </div>
                    </div>
                    <div class="user-actions">
                        ${user.enabled ? 
                            `<button class="action-btn" onclick="disableUser('${user.handle}')">
                                <i class="fa-solid fa-ban"></i> 禁用
                            </button>` :
                            `<button class="action-btn" onclick="enableUser('${user.handle}')">
                                <i class="fa-solid fa-check"></i> 启用
                            </button>`
                        }
                        ${user.admin ?
                            `<button class="action-btn" onclick="demoteUser('${user.handle}')">
                                <i class="fa-solid fa-arrow-down"></i> 降级
                            </button>` :
                            `<button class="action-btn" onclick="promoteUser('${user.handle}')">
                                <i class="fa-solid fa-arrow-up"></i> 提升
                            </button>`
                        }
                        <button class="action-btn" onclick="changePassword('${user.handle}')">
                            <i class="fa-solid fa-key"></i> 密码
                        </button>
                        <button class="action-btn danger" onclick="deleteUser('${user.handle}')">
                            <i class="fa-solid fa-trash"></i> 删除
                        </button>
                    </div>
                `;
                grid.appendChild(card);
            });

            if (pageUsers.length === 0) {
                grid.innerHTML = '<div style="text-align: center; color: var(--grey70); padding: 40px;">没有找到匹配的用户</div>';
            }
        }

        function renderPagination() {
            const pagination = document.getElementById('pagination');
            pagination.innerHTML = '';

            if (totalPages <= 1) return;

            // Previous button
            const prevBtn = document.createElement('button');
            prevBtn.className = 'page-btn';
            prevBtn.innerHTML = '<i class="fa-solid fa-chevron-left"></i>';
            prevBtn.disabled = currentPage === 1;
            prevBtn.onclick = () => goToPage(currentPage - 1);
            pagination.appendChild(prevBtn);

            // First page
            addPageButton(1);

            // Dots after first page
            if (currentPage > 3) {
                addEllipsis();
            }

            // Pages around current page
            for (let i = Math.max(2, currentPage - 1); i <= Math.min(totalPages - 1, currentPage + 1); i++) {
                addPageButton(i);
            }

            // Dots before last page
            if (currentPage < totalPages - 2) {
                addEllipsis();
            }

            // Last page
            if (totalPages > 1) {
                addPageButton(totalPages);
            }

            // Next button
            const nextBtn = document.createElement('button');
            nextBtn.className = 'page-btn';
            nextBtn.innerHTML = '<i class="fa-solid fa-chevron-right"></i>';
            nextBtn.disabled = currentPage === totalPages;
            nextBtn.onclick = () => goToPage(currentPage + 1);
            pagination.appendChild(nextBtn);

            // Page jump
            const pageJump = document.createElement('div');
            pageJump.className = 'page-jump';
            pageJump.innerHTML = `
                <span>跳转到</span>
                <input type="number" min="1" max="${totalPages}" value="${currentPage}">
                <button>确定</button>
            `;
            pagination.appendChild(pageJump);

            // 添加跳转功能
            const jumpInput = pageJump.querySelector('input');
            const jumpButton = pageJump.querySelector('button');

            jumpInput.addEventListener('keypress', (e) => {
                if (e.key === 'Enter') {
                    const page = parseInt(jumpInput.value);
                    goToPage(page);
                }
            });

            jumpButton.addEventListener('click', () => {
                const page = parseInt(jumpInput.value);
                goToPage(page);
            });

            function addPageButton(pageNum) {
                const pageBtn = document.createElement('button');
                pageBtn.className = `page-btn ${pageNum === currentPage ? 'active' : ''}`;
                pageBtn.textContent = pageNum;
                pageBtn.onclick = () => goToPage(pageNum);
                pagination.appendChild(pageBtn);
            }

            function addEllipsis() {
                const dots = document.createElement('span');
                dots.textContent = '...';
                dots.style.margin = '0 5px';
                pagination.appendChild(dots);
            }
        }

        function goToPage(pageNum) {
            pageNum = Math.max(1, Math.min(pageNum, totalPages));
            if (pageNum >= 1 && pageNum <= totalPages) {
                currentPage = pageNum;
                renderUsers();
                renderPagination();
                window.scrollTo(0, 0);
            }
        }

        async function enableUser(handle) {
            try {
                const response = await fetch('/api/users-internal/enable', {
                    method: 'POST',
                    headers: { 
                        'Content-Type': 'application/json',
                        'X-CSRF-Token': csrfToken
                    },
                    body: JSON.stringify({ handle })
                });
                
                if (!response.ok) throw new Error('启用用户失败');
                await fetchUsers();
                toastr.success('用户已成功启用');
            } catch (error) {
                console.error('启用用户错误:', error);
                toastr.error('启用用户失败。请重试。');
            }
        }

        async function disableUser(handle) {
            try {
                const response = await fetch('/api/users-internal/disable', {
                    method: 'POST',
                    headers: { 
                        'Content-Type': 'application/json',
                        'X-CSRF-Token': csrfToken
                    },
                    body: JSON.stringify({ handle })
                });
                
                if (!response.ok) throw new Error('禁用用户失败');
                await fetchUsers();
                toastr.success('用户已成功禁用');
            } catch (error) {
                console.error('禁用用户错误:', error);
                toastr.error('禁用用户失败。请重试。');
            }
        }

        async function promoteUser(handle) {
            try {
                const response = await fetch('/api/users-internal/promote', {
                    method: 'POST',
                    headers: { 
                        'Content-Type': 'application/json',
                        'X-CSRF-Token': csrfToken
                    },
                    body: JSON.stringify({ handle })
                });
                
                if (!response.ok) throw new Error('提升用户失败');
                await fetchUsers();
                toastr.success('用户已成功提升为管理员');
            } catch (error) {
                console.error('提升用户错误:', error);
                toastr.error('提升用户失败。请重试。');
            }
        }

        async function demoteUser(handle) {
            try {
                const response = await fetch('/api/users-internal/demote', {
                    method: 'POST',
                    headers: { 
                        'Content-Type': 'application/json',
                        'X-CSRF-Token': csrfToken
                    },
                    body: JSON.stringify({ handle })
                });
                
                if (!response.ok) throw new Error('降级用户失败');
                await fetchUsers();
                toastr.success('用户已成功降级为普通用户');
            } catch (error) {
                console.error('降级用户错误:', error);
                toastr.error('降级用户失败。请重试。');
            }
        }

        async function deleteUser(handle) {
            if (!confirm(`确定要删除用户 ${handle} 吗?`)) {
                return;
            }

            try {
                const response = await fetch('/api/users-internal/delete', {
                    method: 'POST',
                    headers: { 
                        'Content-Type': 'application/json',
                        'X-CSRF-Token': csrfToken
                    },
                    body: JSON.stringify({ handle })
                });
                
                if (!response.ok) throw new Error('删除用户失败');
                await fetchUsers();
                toastr.success('用户已成功删除');
            } catch (error) {
                console.error('删除用户错误:', error);
                toastr.error('删除用户失败。请重试。');
            }
        }

        async function changePassword(handle) {
            const password = prompt('请输入新密码(留空则删除密码):');
            if (password === null) return; // 用户取消

            try {
                const response = await fetch('/api/users-internal/password', {
                    method: 'POST',
                    headers: { 
                        'Content-Type': 'application/json',
                        'X-CSRF-Token': csrfToken
                    },
                    body: JSON.stringify({ handle, password })
                });
                
                if (!response.ok) throw new Error('修改密码失败');
                toastr.success('密码已成功更新');
                await fetchUsers();
            } catch (error) {
                console.error('修改密码错误:', error);
                toastr.error('修改密码失败。请重试。');
            }
        }

        // 事件监听器
        document.getElementById('searchInput').addEventListener('input', applyFilters);
        document.getElementById('roleFilter').addEventListener('change', applyFilters);
        document.getElementById('statusFilter').addEventListener('change', applyFilters);

        // 初始加载
        getCsrfToken().then(() => fetchUsers());
    </script>
</body>
</html>