File size: 31,688 Bytes
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
497686c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69b897d
 
 
 
 
 
 
 
75031b4
69b897d
75031b4
69b897d
 
 
 
 
 
75031b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75031b4
69b897d
75031b4
69b897d
75031b4
69b897d
 
 
 
75031b4
 
69b897d
75031b4
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75031b4
497686c
69b897d
75031b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69b897d
75031b4
69b897d
 
 
 
75031b4
69b897d
 
75031b4
 
 
 
 
 
 
 
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75031b4
69b897d
 
 
 
75031b4
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75031b4
69b897d
 
 
 
 
 
 
 
75031b4
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75031b4
 
 
 
 
 
69b897d
75031b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69b897d
 
497686c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69b897d
 
 
497686c
69b897d
 
 
497686c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
<template>
  <Teleport to="body">
    <div v-if="show" class="modal fixed inset-0 z-[60] flex items-center justify-center p-3 sm:p-4">
      <div
        class="modal-content custom-scrollbar mx-auto max-h-[90vh] w-full max-w-4xl overflow-y-auto p-4 sm:p-6 md:p-8"
      >
        <div class="mb-4 flex items-center justify-between sm:mb-6">
          <div class="flex items-center gap-2 sm:gap-3">
            <div
              class="flex h-8 w-8 items-center justify-center rounded-lg bg-gradient-to-br from-purple-500 to-purple-600 sm:h-10 sm:w-10 sm:rounded-xl"
            >
              <i class="fas fa-key text-sm text-white sm:text-base" />
            </div>
            <div>
              <h3 class="text-lg font-bold text-gray-900 dark:text-gray-100 sm:text-xl">
                API Key 管理
              </h3>
              <p class="text-xs text-gray-500 dark:text-gray-400 sm:text-sm">
                {{ accountName }}
              </p>
            </div>
          </div>
          <div class="flex items-center gap-2">
            <button
              class="flex items-center gap-2 rounded-lg border border-purple-200 bg-white/90 px-3 py-1.5 text-xs font-semibold text-purple-600 shadow-sm transition-all duration-200 hover:border-purple-300 hover:bg-purple-50 hover:text-purple-700 focus:outline-none focus:ring-2 focus:ring-purple-200 disabled:cursor-not-allowed disabled:opacity-60 dark:border-purple-600/60 dark:bg-purple-900/20 dark:text-purple-200 dark:hover:border-purple-500 dark:hover:bg-purple-900/40 dark:hover:text-purple-100 dark:focus:ring-purple-500/40 sm:text-sm"
              :disabled="loading || apiKeys.length === 0 || copyingAll"
              @click="copyAllApiKeys"
            >
              <i
                :class="[
                  'text-sm sm:text-base',
                  copyingAll ? 'fas fa-spinner fa-spin' : 'fas fa-clipboard-list'
                ]"
              />
              <span>复制全部 Key</span>
            </button>
            <button
              class="flex h-9 w-9 items-center justify-center rounded-full bg-gray-100 text-gray-400 transition-colors hover:text-gray-600 dark:bg-gray-800 dark:text-gray-400 dark:hover:text-gray-200 sm:h-10 sm:w-10"
              title="关闭"
              @click="$emit('close')"
            >
              <i class="fas fa-times text-base sm:text-lg" />
            </button>
          </div>
        </div>

        <!-- 加载状态 -->
        <div v-if="loading" class="py-8 text-center">
          <div class="loading-spinner-lg mx-auto mb-4" />
          <p class="text-gray-500 dark:text-gray-400">加载中...</p>
        </div>

        <!-- 空状态:没有加载且没有 API Key -->
        <div
          v-if="!loading && apiKeys.length === 0"
          class="rounded-lg bg-gray-50 py-8 text-center dark:bg-gray-800"
        >
          <i class="fas fa-key mb-4 text-4xl text-gray-300 dark:text-gray-600" />
          <p class="text-gray-500 dark:text-gray-400">暂无 API Key</p>
        </div>

        <!-- 有 API Key 时显示菜单和列表 -->
        <div v-if="!loading && apiKeys.length > 0">
          <!-- 菜单栏 -->
          <div class="mb-4 space-y-3">
            <!-- 工具栏:筛选、搜索和操作 -->
            <div
              class="rounded-lg border border-gray-200 bg-white p-3 shadow-sm dark:border-gray-700 dark:bg-gray-800"
            >
              <!-- 第一行:筛选和搜索 -->
              <div class="mb-3 flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
                <!-- 左侧:状态筛选 -->
                <div class="flex items-center gap-2">
                  <i class="fas fa-filter text-gray-400 dark:text-gray-500" />
                  <span class="text-sm font-medium text-gray-700 dark:text-gray-300">筛选:</span>
                  <div class="flex gap-1">
                    <button
                      :class="[
                        'rounded-md px-3 py-1.5 text-xs font-medium transition-colors',
                        statusFilter === 'all'
                          ? 'bg-purple-500 text-white shadow-sm'
                          : 'bg-gray-100 text-gray-600 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-400 dark:hover:bg-gray-600'
                      ]"
                      @click="statusFilter = 'all'"
                    >
                      全部 ({{ apiKeys.length }})
                    </button>
                    <button
                      :class="[
                        'rounded-md px-3 py-1.5 text-xs font-medium transition-colors',
                        statusFilter === 'active'
                          ? 'bg-green-500 text-white shadow-sm'
                          : 'bg-gray-100 text-gray-600 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-400 dark:hover:bg-gray-600'
                      ]"
                      @click="statusFilter = 'active'"
                    >
                      <i class="fas fa-check-circle mr-1" />
                      正常 ({{ activeKeysCount }})
                    </button>
                    <button
                      :class="[
                        'rounded-md px-3 py-1.5 text-xs font-medium transition-colors',
                        statusFilter === 'error'
                          ? 'bg-red-500 text-white shadow-sm'
                          : 'bg-gray-100 text-gray-600 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-400 dark:hover:bg-gray-600'
                      ]"
                      @click="statusFilter = 'error'"
                    >
                      <i class="fas fa-exclamation-triangle mr-1" />
                      异常 ({{ errorKeysCount }})
                    </button>
                  </div>
                </div>

                <!-- 右侧:搜索框 -->
                <div class="flex flex-1 items-center gap-2 lg:max-w-md">
                  <div class="relative flex-1">
                    <input
                      v-model="searchQuery"
                      class="w-full rounded-md border border-gray-300 bg-gray-50 py-2 pl-10 pr-3 text-sm text-gray-700 transition-colors placeholder:text-gray-400 focus:border-purple-500 focus:bg-white focus:outline-none focus:ring-2 focus:ring-purple-500/20 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-300 dark:placeholder:text-gray-500 dark:focus:border-purple-400 dark:focus:bg-gray-800"
                      placeholder="搜索 API Key..."
                      type="text"
                    />
                    <i
                      class="fas fa-search absolute left-3 top-1/2 -translate-y-1/2 text-gray-400 dark:text-gray-500"
                    />
                  </div>
                  <div class="flex gap-1">
                    <button
                      :class="[
                        'rounded-md px-2.5 py-2 text-xs font-medium transition-colors',
                        searchMode === 'fuzzy'
                          ? 'bg-purple-500 text-white shadow-sm'
                          : 'bg-gray-100 text-gray-600 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-400 dark:hover:bg-gray-600'
                      ]"
                      title="模糊搜索:包含查询字符串即可"
                      @click="searchMode = 'fuzzy'"
                    >
                      <i class="fas fa-search mr-1" />
                      模糊
                    </button>
                    <button
                      :class="[
                        'rounded-md px-2.5 py-2 text-xs font-medium transition-colors',
                        searchMode === 'exact'
                          ? 'bg-purple-500 text-white shadow-sm'
                          : 'bg-gray-100 text-gray-600 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-400 dark:hover:bg-gray-600'
                      ]"
                      title="精确搜索:完全匹配完整 Key"
                      @click="searchMode = 'exact'"
                    >
                      <i class="fas fa-equals mr-1" />
                      精确
                    </button>
                  </div>
                </div>
              </div>

              <!-- 分隔线 -->
              <div class="my-3 border-t border-gray-200 dark:border-gray-700"></div>

              <!-- 第二行:批量操作 -->
              <div class="flex flex-wrap items-center justify-between gap-2">
                <!-- 左侧:操作按钮 -->
                <div class="flex flex-wrap items-center gap-2">
                  <span class="text-xs font-medium text-gray-500 dark:text-gray-400"
                    >批量操作:</span
                  >
                  <button
                    class="group rounded-md bg-gradient-to-r from-red-500 to-red-600 px-3 py-1.5 text-xs font-medium text-white shadow-sm transition-all hover:from-red-600 hover:to-red-700 hover:shadow disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:shadow-sm"
                    :disabled="errorKeysCount === 0 || batchDeleting"
                    title="删除所有异常状态的 API Key"
                    @click="deleteAllErrorKeys"
                  >
                    <i class="fas fa-trash-alt mr-1" />
                    删除异常
                  </button>
                  <button
                    class="group rounded-md bg-gradient-to-r from-red-600 to-red-700 px-3 py-1.5 text-xs font-medium text-white shadow-sm transition-all hover:from-red-700 hover:to-red-800 hover:shadow disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:shadow-sm"
                    :disabled="apiKeys.length === 0 || batchDeleting"
                    title="删除所有 API Key"
                    @click="deleteAllKeys"
                  >
                    <i class="fas fa-trash mr-1" />
                    删除全部
                  </button>
                  <div class="mx-1 h-5 w-px bg-gray-300 dark:bg-gray-600"></div>
                  <button
                    class="rounded-md bg-gradient-to-r from-blue-500 to-blue-600 px-3 py-1.5 text-xs font-medium text-white shadow-sm transition-all hover:from-blue-600 hover:to-blue-700 hover:shadow disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:shadow-sm"
                    :disabled="errorKeysCount === 0"
                    title="导出所有异常状态的 API Key"
                    @click="exportKeys('error')"
                  >
                    <i class="fas fa-download mr-1" />
                    导出异常
                  </button>
                  <button
                    class="rounded-md bg-gradient-to-r from-blue-600 to-blue-700 px-3 py-1.5 text-xs font-medium text-white shadow-sm transition-all hover:from-blue-700 hover:to-blue-800 hover:shadow disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:shadow-sm"
                    :disabled="apiKeys.length === 0"
                    title="导出所有 API Key"
                    @click="exportKeys('all')"
                  >
                    <i class="fas fa-file-export mr-1" />
                    导出全部
                  </button>
                </div>

                <!-- 右侧:统计信息 -->
                <div
                  class="flex items-center gap-2 rounded-md bg-purple-50 px-3 py-1.5 dark:bg-purple-900/20"
                >
                  <i class="fas fa-info-circle text-purple-500 dark:text-purple-400" />
                  <span class="text-xs font-medium text-purple-700 dark:text-purple-300">
                    显示 <strong>{{ filteredApiKeys.length }}</strong> 个
                  </span>
                </div>
              </div>
            </div>
          </div>
          <!-- API Key 网格布局 -->
          <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
            <div
              v-for="(apiKey, index) in paginatedApiKeys"
              :key="index"
              class="relative rounded-lg border border-gray-200 bg-white p-4 transition-all hover:shadow-md dark:border-gray-700 dark:bg-gray-800"
            >
              <!-- 左上角错误状态码角标 -->
              <div
                v-if="
                  (apiKey.status === 'error' || apiKey.status === 'disabled') && apiKey.errorMessage
                "
                class="absolute -left-2 -top-2 z-10"
              >
                <span
                  class="inline-flex items-center justify-center rounded-full px-2 py-0.5 text-[10px] font-semibold shadow-sm"
                  :class="[
                    apiKey.status === 'error'
                      ? 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400'
                      : 'bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400'
                  ]"
                  :title="`错误状态码: ${apiKey.errorMessage}`"
                >
                  {{ apiKey.errorMessage }}
                </span>
              </div>

              <div class="flex flex-col gap-3">
                <!-- API Key 信息 -->
                <div class="flex items-start justify-between gap-2">
                  <span
                    class="flex-1 break-all font-mono text-xs font-medium text-gray-900 dark:text-gray-100"
                    :title="apiKey.key"
                  >
                    {{ maskApiKey(apiKey.key) }}
                  </span>
                  <div class="flex items-center gap-1">
                    <button
                      class="text-xs text-gray-500 transition-colors hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
                      title="复制 API Key"
                      @click="copyApiKey(apiKey.key)"
                    >
                      <i class="fas fa-copy" />
                    </button>
                    <button
                      v-if="apiKey.status === 'error' || apiKey.status === 'disabled'"
                      class="text-xs transition-colors disabled:cursor-not-allowed disabled:opacity-50"
                      :class="[
                        apiKey.status === 'error'
                          ? 'text-orange-500 hover:text-orange-700 dark:text-orange-400 dark:hover:text-orange-300'
                          : 'text-yellow-500 hover:text-yellow-700 dark:text-yellow-400 dark:hover:text-yellow-300'
                      ]"
                      :disabled="resetting === apiKey.key"
                      title="重置状态"
                      @click="resetApiKeyStatus(apiKey)"
                    >
                      <div v-if="resetting === apiKey.key" class="loading-spinner-sm" />
                      <i v-else class="fas fa-redo"></i>
                    </button>
                    <button
                      class="text-xs text-red-500 transition-colors hover:text-red-700 disabled:cursor-not-allowed disabled:opacity-50 dark:text-red-400 dark:hover:text-red-600"
                      :disabled="deleting === apiKey.key"
                      @click="deleteApiKey(apiKey)"
                    >
                      <div v-if="deleting === apiKey.key" class="loading-spinner-sm" />
                      <i v-else class="fas fa-trash" />
                    </button>
                  </div>
                </div>

                <!-- 统计信息(一行显示) -->
                <div
                  class="flex flex-wrap items-center gap-3 text-xs text-gray-600 dark:text-gray-400"
                >
                  <div>
                    <span
                      :class="[
                        apiKey.status === 'active'
                          ? 'text-green-600 dark:text-green-400'
                          : apiKey.status === 'error'
                            ? 'text-red-600 dark:text-red-400'
                            : 'text-yellow-600 dark:text-yellow-400'
                      ]"
                    >
                      <i
                        class="mr-1"
                        :class="[
                          apiKey.status === 'active'
                            ? 'fas fa-check-circle'
                            : apiKey.status === 'error'
                              ? 'fas fa-exclamation-triangle'
                              : 'fas fa-exclamation-circle'
                        ]"
                      />
                      {{
                        apiKey.status === 'active'
                          ? '正常'
                          : apiKey.status === 'error'
                            ? '异常'
                            : apiKey.status === 'disabled'
                              ? '禁用'
                              : apiKey.status || '未知'
                      }}
                    </span>
                  </div>
                  <div>
                    <span
                      >使用: <strong>{{ apiKey.usageCount || 0 }}</strong
                      >次</span
                    >
                  </div>
                  <div v-if="apiKey.lastUsedAt">
                    <span>{{ formatTime(apiKey.lastUsedAt) }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 分页控制(底部) -->
          <div v-if="totalPages > 1" class="mt-4 flex items-center justify-between">
            <div class="text-sm text-gray-600 dark:text-gray-400">
              显示 {{ (currentPage - 1) * pageSize + 1 }}-{{
                Math.min(currentPage * pageSize, totalItems)
              }}
              项,共 {{ totalItems }} 项
            </div>
            <div class="flex items-center gap-2">
              <button
                class="rounded-lg bg-gray-100 px-3 py-1.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-200 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600"
                :disabled="currentPage === 1"
                @click="currentPage = 1"
              >
                <i class="fas fa-angle-double-left" />
              </button>
              <button
                class="rounded-lg bg-gray-100 px-3 py-1.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-200 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600"
                :disabled="currentPage === 1"
                @click="currentPage--"
              >
                <i class="fas fa-angle-left" />
              </button>
              <span class="px-3 py-1.5 text-sm font-medium text-gray-700 dark:text-gray-300">
                {{ currentPage }} / {{ totalPages }}
              </span>
              <button
                class="rounded-lg bg-gray-100 px-3 py-1.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-200 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600"
                :disabled="currentPage === totalPages"
                @click="currentPage++"
              >
                <i class="fas fa-angle-right" />
              </button>
              <button
                class="rounded-lg bg-gray-100 px-3 py-1.5 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-200 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600"
                :disabled="currentPage === totalPages"
                @click="currentPage = totalPages"
              >
                <i class="fas fa-angle-double-right" />
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </Teleport>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { showToast } from '@/utils/toast'
import { apiClient } from '@/config/api'

const props = defineProps({
  accountId: {
    type: String,
    required: true
  },
  accountName: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['close', 'refresh'])

const show = ref(true)
const loading = ref(false)
const deleting = ref(null)
const resetting = ref(null)
const apiKeys = ref([])
const currentPage = ref(1)
const pageSize = ref(15)
const copyingAll = ref(false)

// 新增:筛选和搜索相关状态
const statusFilter = ref('all') // 'all' | 'active' | 'error'
const searchQuery = ref('')
const searchMode = ref('fuzzy') // 'fuzzy' | 'exact'
const batchDeleting = ref(false)

// 掩码显示 API Key(提前声明供 computed 使用)
const maskApiKey = (key) => {
  if (!key || key.length < 12) {
    return key
  }
  return `${key.substring(0, 8)}...${key.substring(key.length - 4)}`
}

// 计算属性:筛选后的 API Keys
const filteredApiKeys = computed(() => {
  let filtered = apiKeys.value

  // 状态筛选
  if (statusFilter.value !== 'all') {
    filtered = filtered.filter((key) => key.status === statusFilter.value)
  }

  // 搜索筛选(使用完整的 key 进行搜索)
  if (searchQuery.value.trim()) {
    const query = searchQuery.value.trim()
    filtered = filtered.filter((key) => {
      const fullKey = key.key // 使用完整的 key
      if (searchMode.value === 'exact') {
        // 精确搜索:完全匹配完整的 key
        return fullKey === query
      } else {
        // 模糊搜索:完整 key 包含查询字符串(不区分大小写)
        return fullKey.toLowerCase().includes(query.toLowerCase())
      }
    })
  }

  return filtered
})

// 计算属性
const totalItems = computed(() => filteredApiKeys.value.length)
const totalPages = computed(() => Math.ceil(totalItems.value / pageSize.value))
const paginatedApiKeys = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return filteredApiKeys.value.slice(start, end)
})

// 统计数量
const activeKeysCount = computed(() => {
  return apiKeys.value.filter((key) => key.status === 'active').length
})

const errorKeysCount = computed(() => {
  return apiKeys.value.filter((key) => key.status === 'error').length
})

// 加载 API Keys
const loadApiKeys = async () => {
  loading.value = true
  try {
    const response = await apiClient.get(`/admin/droid-accounts/${props.accountId}`)
    const account = response.data

    // 解析 apiKeys
    let parsedKeys = []
    if (Array.isArray(account.apiKeys)) {
      parsedKeys = account.apiKeys
    } else if (typeof account.apiKeys === 'string') {
      try {
        parsedKeys = JSON.parse(account.apiKeys)
      } catch (error) {
        console.error('Failed to parse apiKeys:', error)
      }
    }

    // 转换为统一格式
    const formattedKeys = parsedKeys.map((item) => {
      if (typeof item === 'string') {
        // 对于字符串类型的API Key,保持默认状态为active
        return {
          key: item,
          usageCount: 0,
          status: 'active',
          lastUsedAt: null,
          errorMessage: ''
        }
      } else if (typeof item === 'object' && item !== null) {
        // 对于对象类型的API Key,保留所有状态信息
        return {
          key: item.key || item.apiKey || '',
          usageCount: item.usageCount || item.count || 0,
          status: item.status || 'active', // 保留后端返回的状态
          lastUsedAt: item.lastUsedAt || item.lastUsed || null,
          errorMessage: item.errorMessage || '' // 保留后端返回的错误信息
        }
      }
      // 其他情况,默认为active状态
      return {
        key: String(item),
        usageCount: 0,
        status: 'active',
        lastUsedAt: null,
        errorMessage: ''
      }
    })

    // 按最新使用时间排序(最近使用的在前,未使用的在后)
    apiKeys.value = formattedKeys.sort((a, b) => {
      // 如果都有 lastUsedAt,按时间降序排序
      if (a.lastUsedAt && b.lastUsedAt) {
        return new Date(b.lastUsedAt) - new Date(a.lastUsedAt)
      }
      // 如果 a 有时间,b 没有,a 排在前面
      if (a.lastUsedAt && !b.lastUsedAt) {
        return -1
      }
      // 如果 b 有时间,a 没有,b 排在前面
      if (!a.lastUsedAt && b.lastUsedAt) {
        return 1
      }
      // 如果都没有时间,按使用次数降序排序
      return (b.usageCount || 0) - (a.usageCount || 0)
    })
  } catch (error) {
    console.error('Failed to load API keys:', error)
    showToast('加载 API Key 失败', 'error')
  } finally {
    loading.value = false
    // 重置到第一页
    currentPage.value = 1
  }
}

// 删除 API Key
const deleteApiKey = async (apiKey) => {
  if (!confirm(`确定要删除 API Key "${maskApiKey(apiKey.key)}" 吗?`)) {
    return
  }

  deleting.value = apiKey.key
  try {
    // 准备更新数据:删除指定的 key
    const updateData = {
      removeApiKeys: [apiKey.key],
      apiKeyUpdateMode: 'delete'
    }

    await apiClient.put(`/admin/droid-accounts/${props.accountId}`, updateData)

    showToast('API Key 已删除', 'success')
    await loadApiKeys()
    emit('refresh')
  } catch (error) {
    console.error('Failed to delete API key:', error)
    showToast(error.response?.data?.error || '删除 API Key 失败', 'error')
  } finally {
    deleting.value = null
  }
}

// 重置 API Key 状态
const resetApiKeyStatus = async (apiKey) => {
  if (
    !confirm(
      `确定要重置 API Key "${maskApiKey(apiKey.key)}" 的状态吗?这将清除错误信息并恢复为正常状态。`
    )
  ) {
    return
  }

  resetting.value = apiKey.key
  try {
    // 准备更新数据:重置指定 key 的状态
    const updateData = {
      apiKeys: [
        {
          key: apiKey.key,
          status: 'active',
          errorMessage: ''
        }
      ],
      apiKeyUpdateMode: 'update'
    }

    await apiClient.put(`/admin/droid-accounts/${props.accountId}`, updateData)

    showToast('API Key 状态已重置', 'success')
    await loadApiKeys()
    emit('refresh')
  } catch (error) {
    console.error('Failed to reset API key status:', error)
    showToast(error.response?.data?.error || '重置 API Key 状态失败', 'error')
  } finally {
    resetting.value = null
  }
}

// 批量删除所有异常状态的 Key
const deleteAllErrorKeys = async () => {
  const errorKeys = apiKeys.value.filter((key) => key.status === 'error')
  if (errorKeys.length === 0) {
    showToast('没有异常状态的 API Key', 'warning')
    return
  }

  if (!confirm(`确定要删除所有 ${errorKeys.length} 个异常状态的 API Key 吗?此操作不可恢复!`)) {
    return
  }

  batchDeleting.value = true
  try {
    const keysToDelete = errorKeys.map((key) => key.key)
    const updateData = {
      removeApiKeys: keysToDelete,
      apiKeyUpdateMode: 'delete'
    }

    await apiClient.put(`/admin/droid-accounts/${props.accountId}`, updateData)

    showToast(`成功删除 ${errorKeys.length} 个异常 API Key`, 'success')
    await loadApiKeys()
    emit('refresh')
  } catch (error) {
    console.error('Failed to delete error API keys:', error)
    showToast(error.response?.data?.error || '批量删除失败', 'error')
  } finally {
    batchDeleting.value = false
  }
}

// 批量删除所有 Key
const deleteAllKeys = async () => {
  if (apiKeys.value.length === 0) {
    showToast('没有可删除的 API Key', 'warning')
    return
  }

  if (
    !confirm(
      `确定要删除所有 ${apiKeys.value.length} 个 API Key 吗?此操作不可恢复!\n\n请再次确认:这将删除该账户下的所有 API Key。`
    )
  ) {
    return
  }

  // 二次确认
  if (!confirm('最后确认:真的要删除所有 API Key 吗?')) {
    return
  }

  batchDeleting.value = true
  try {
    const keysToDelete = apiKeys.value.map((key) => key.key)
    const updateData = {
      removeApiKeys: keysToDelete,
      apiKeyUpdateMode: 'delete'
    }

    await apiClient.put(`/admin/droid-accounts/${props.accountId}`, updateData)

    showToast(`成功删除所有 ${keysToDelete.length} 个 API Key`, 'success')
    await loadApiKeys()
    emit('refresh')
  } catch (error) {
    console.error('Failed to delete all API keys:', error)
    showToast(error.response?.data?.error || '批量删除失败', 'error')
  } finally {
    batchDeleting.value = false
  }
}

// 导出 Key
const exportKeys = (type) => {
  let keysToExport = []
  let filename = ''

  if (type === 'error') {
    keysToExport = apiKeys.value.filter((key) => key.status === 'error')
    filename = `error_api_keys_${props.accountName}_${new Date().toISOString().split('T')[0]}.txt`
  } else {
    keysToExport = apiKeys.value
    filename = `all_api_keys_${props.accountName}_${new Date().toISOString().split('T')[0]}.txt`
  }

  if (keysToExport.length === 0) {
    showToast('没有可导出的 API Key', 'warning')
    return
  }

  // 生成 TXT 内容(每行一个完整的 key)
  const content = keysToExport.map((key) => key.key).join('\n')

  // 创建下载
  const blob = new Blob([content], { type: 'text/plain;charset=utf-8' })
  const url = URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = filename
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  URL.revokeObjectURL(url)

  showToast(`成功导出 ${keysToExport.length} 个 API Key`, 'success')
}

// 写入剪贴板(带回退逻辑)
const writeToClipboard = async (text) => {
  const canUseClipboardApi =
    typeof navigator !== 'undefined' &&
    navigator.clipboard &&
    typeof navigator.clipboard.writeText === 'function' &&
    (typeof window === 'undefined' || window.isSecureContext !== false)

  if (canUseClipboardApi) {
    await navigator.clipboard.writeText(text)
    return
  }

  if (typeof document === 'undefined') {
    throw new Error('clipboard unavailable')
  }

  const textarea = document.createElement('textarea')
  textarea.value = text
  textarea.setAttribute('readonly', '')
  textarea.style.position = 'fixed'
  textarea.style.opacity = '0'
  textarea.style.pointerEvents = 'none'
  document.body.appendChild(textarea)
  textarea.select()

  try {
    const success = document.execCommand('copy')
    document.body.removeChild(textarea)
    if (!success) {
      throw new Error('execCommand failed')
    }
  } catch (error) {
    document.body.removeChild(textarea)
    throw error
  }
}

// 复制 API Key
const copyApiKey = async (key) => {
  try {
    await writeToClipboard(key)
    showToast('API Key 已复制', 'success')
  } catch (error) {
    console.error('Failed to copy:', error)
    showToast('复制失败,请手动复制', 'error')
  }
}

// 复制全部 API Key
const copyAllApiKeys = async () => {
  if (!apiKeys.value.length || copyingAll.value) {
    return
  }

  copyingAll.value = true
  try {
    const allKeysText = apiKeys.value.map((item) => item.key).join('\n')
    await writeToClipboard(allKeysText)
    showToast(`已复制 ${apiKeys.value.length} 条 API Key`, 'success')
  } catch (error) {
    console.error('Failed to copy all keys:', error)
    showToast('复制全部 API Key 失败,请手动复制', 'error')
  } finally {
    copyingAll.value = false
  }
}

// 格式化时间
const formatTime = (timestamp) => {
  if (!timestamp) return '-'
  try {
    const date = new Date(timestamp)
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    })
  } catch (error) {
    return '-'
  }
}

onMounted(() => {
  loadApiKeys()
})
</script>