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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<template>
  <div class="backend-manager">
    <div class="header">
      <h3>后端实例管理</h3>
      <button @click="showAddModal = true" class="btn btn-primary">
        <span class="icon">+</span> 添加实例
      </button>
    </div>

    <div class="stats">
      <div class="stat-item">
        <span class="label">总实例数:</span>
        <span class="value">{{ backendStore.backends.length }}</span>
      </div>
      <div class="stat-item">
        <span class="label">已连接:</span>
        <span class="value connected">{{ backendStore.connectedBackendsCount }}</span>
      </div>
      <div class="stat-item">
        <span class="label">当前活跃:</span>
        <span class="value active">{{ backendStore.activeBackend?.name || '无' }}</span>
      </div>
    </div>

    <div class="backend-list">
      <div
        v-for="backend in backendStore.backends"
        :key="backend.id"
        class="backend-item"
        :class="{
          active: backend.isActive,
          connected: backend.isConnected,
          disconnected: !backend.isConnected
        }"
      >
        <div class="backend-info">
          <div class="name-row">
            <h4>{{ backend.name }}</h4>
            <div class="badges">
              <span v-if="backend.isActive" class="badge active">活跃</span>
              <span v-if="backend.isConnected" class="badge connected">已连接</span>
              <span v-else class="badge disconnected">未连接</span>
            </div>
          </div>
          
          <div class="details">
            <p class="url">{{ backend.baseUrl }}</p>
            <p v-if="backend.description" class="description">{{ backend.description }}</p>
            <p v-if="backend.lastConnected" class="last-connected">
              最后连接:{{ formatTime(backend.lastConnected) }}
            </p>
          </div>
        </div>

        <div class="backend-actions">
          <button
            @click="switchBackend(backend.id)"
            :disabled="backend.isActive"
            class="btn btn-sm"
            :class="backend.isActive ? 'btn-secondary' : 'btn-primary'"
          >
            {{ backend.isActive ? '当前活跃' : '切换' }}
          </button>
          
          <button
            @click="testConnection(backend.id)"
            :disabled="testing[backend.id]"
            class="btn btn-sm btn-outline"
          >
            {{ testing[backend.id] ? '测试中...' : '测试连接' }}
          </button>
          
          <button
            @click="editBackend(backend)"
            class="btn btn-sm btn-outline"
          >
            编辑
          </button>
          
          <button
            v-if="backend.id !== 'local'"
            @click="confirmDelete(backend)"
            class="btn btn-sm btn-danger"
          >
            删除
          </button>
        </div>
      </div>
    </div>

    <div class="actions">
      <button @click="testAllConnections" :disabled="testingAll" class="btn btn-outline">
        {{ testingAll ? '测试中...' : '测试所有连接' }}
      </button>
      <button @click="refreshAll" class="btn btn-outline">
        刷新状态
      </button>
    </div>

    <!-- 添加/编辑模态框 -->
    <div v-if="showAddModal || showEditModal" class="modal-overlay" @click="closeModal">
      <div class="modal" @click.stop>
        <div class="modal-header">
          <h4>{{ showAddModal ? '添加后端实例' : '编辑后端实例' }}</h4>
          <button @click="closeModal" class="close-btn">×</button>
        </div>
        
        <div class="modal-body">
          <div class="form-group">
            <label>实例名称</label>
            <input
              v-model="formData.name"
              type="text"
              placeholder="例如:生产服务器"
              class="form-control"
            />
          </div>
          
          <div class="form-group">
            <label>服务器地址</label>
            <input
              v-model="formData.baseUrl"
              type="url"
              placeholder="https://your-hajimi-instance.com"
              class="form-control"
            />
          </div>
          
          <div class="form-group">
            <label>访问密码(可选)</label>
            <input
              v-model="formData.password"
              type="password"
              placeholder="留空如果不需要密码"
              class="form-control"
            />
          </div>
          
          <div class="form-group">
            <label>描述(可选)</label>
            <textarea
              v-model="formData.description"
              placeholder="描述这个实例的用途"
              class="form-control"
              rows="3"
            ></textarea>
          </div>
        </div>
        
        <div class="modal-footer">
          <button @click="closeModal" class="btn btn-secondary">取消</button>
          <button @click="saveBackend" class="btn btn-primary">
            {{ showAddModal ? '添加' : '保存' }}
          </button>
        </div>
      </div>
    </div>

    <!-- 删除确认模态框 -->
    <div v-if="showDeleteModal" class="modal-overlay" @click="showDeleteModal = false">
      <div class="modal" @click.stop>
        <div class="modal-header">
          <h4>确认删除</h4>
          <button @click="showDeleteModal = false" class="close-btn">×</button>
        </div>
        
        <div class="modal-body">
          <p>确定要删除后端实例 "{{ deleteTarget?.name }}" 吗?</p>
          <p class="warning">此操作不可恢复。</p>
        </div>
        
        <div class="modal-footer">
          <button @click="showDeleteModal = false" class="btn btn-secondary">取消</button>
          <button @click="deleteBackend" class="btn btn-danger">删除</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useBackendStore } from '@/stores/backend'

const backendStore = useBackendStore()

// 状态
const showAddModal = ref(false)
const showEditModal = ref(false)
const showDeleteModal = ref(false)
const testing = reactive({})
const testingAll = ref(false)
const editingBackend = ref(null)
const deleteTarget = ref(null)

// 表单数据
const formData = reactive({
  name: '',
  baseUrl: '',
  password: '',
  description: ''
})

// 切换后端实例
function switchBackend(backendId) {
  if (backendStore.switchBackend(backendId)) {
    backendStore.saveToStorage()
  }
}

// 测试单个连接
async function testConnection(backendId) {
  testing[backendId] = true
  try {
    const result = await backendStore.testBackendConnection(backendId)
    if (result.success) {
      console.log(`连接测试成功: ${result.message}`)
    } else {
      console.error(`连接测试失败: ${result.message}`)
    }
  } catch (error) {
    console.error('连接测试出错:', error)
  } finally {
    testing[backendId] = false
  }
}

// 测试所有连接
async function testAllConnections() {
  testingAll.value = true
  try {
    const results = await backendStore.testAllConnections()
    console.log('所有连接测试完成:', results)
  } catch (error) {
    console.error('批量测试连接出错:', error)
  } finally {
    testingAll.value = false
  }
}

// 刷新所有状态
function refreshAll() {
  testAllConnections()
}

// 编辑后端
function editBackend(backend) {
  editingBackend.value = backend
  formData.name = backend.name
  formData.baseUrl = backend.baseUrl
  formData.password = backend.password
  formData.description = backend.description
  showEditModal.value = true
}

// 确认删除
function confirmDelete(backend) {
  deleteTarget.value = backend
  showDeleteModal.value = true
}

// 删除后端
function deleteBackend() {
  if (deleteTarget.value) {
    backendStore.removeBackend(deleteTarget.value.id)
    backendStore.saveToStorage()
    showDeleteModal.value = false
    deleteTarget.value = null
  }
}

// 保存后端
function saveBackend() {
  if (!formData.name.trim() || !formData.baseUrl.trim()) {
    alert('请填写实例名称和服务器地址')
    return
  }

  if (showAddModal.value) {
    // 添加新实例
    backendStore.addBackend({
      name: formData.name.trim(),
      baseUrl: formData.baseUrl.trim(),
      password: formData.password.trim(),
      description: formData.description.trim()
    })
  } else if (showEditModal.value && editingBackend.value) {
    // 更新现有实例
    backendStore.updateBackend(editingBackend.value.id, {
      name: formData.name.trim(),
      baseUrl: formData.baseUrl.trim(),
      password: formData.password.trim(),
      description: formData.description.trim()
    })
  }

  backendStore.saveToStorage()
  closeModal()
}

// 关闭模态框
function closeModal() {
  showAddModal.value = false
  showEditModal.value = false
  editingBackend.value = null
  
  // 重置表单
  formData.name = ''
  formData.baseUrl = ''
  formData.password = ''
  formData.description = ''
}

// 格式化时间
function formatTime(isoString) {
  if (!isoString) return ''
  const date = new Date(isoString)
  return date.toLocaleString('zh-CN')
}

// 初始化
onMounted(() => {
  testAllConnections()
})
</script>

<style scoped>
.backend-manager {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h3 {
  margin: 0;
  color: #333;
}

.stats {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.stat-item .label {
  font-size: 12px;
  color: #666;
}

.stat-item .value {
  font-weight: bold;
  font-size: 16px;
}

.stat-item .value.connected {
  color: #28a745;
}

.stat-item .value.active {
  color: #007bff;
}

.backend-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 20px;
}

.backend-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 20px;
  border: 2px solid #e9ecef;
  border-radius: 10px;
  background: white;
  transition: all 0.3s ease;
}

.backend-item.active {
  border-color: #007bff;
  background: #f8f9ff;
}

.backend-item.connected:not(.active) {
  border-color: #28a745;
}

.backend-item.disconnected:not(.active) {
  border-color: #dc3545;
  background: #fff5f5;
}

.backend-info {
  flex: 1;
  min-width: 0;
}

.name-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.name-row h4 {
  margin: 0;
  color: #333;
}

.badges {
  display: flex;
  gap: 8px;
}

.badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
  text-transform: uppercase;
}

.badge.active {
  background: #007bff;
  color: white;
}

.badge.connected {
  background: #28a745;
  color: white;
}

.badge.disconnected {
  background: #dc3545;
  color: white;
}

.details p {
  margin: 5px 0;
  font-size: 14px;
}

.details .url {
  color: #007bff;
  font-family: monospace;
}

.details .description {
  color: #666;
}

.details .last-connected {
  color: #999;
  font-size: 12px;
}

.backend-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-left: 20px;
}

.actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 20px;
}

/* 按钮样式 */
.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: #007bff;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: #0056b3;
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn-secondary:hover:not(:disabled) {
  background: #545b62;
}

.btn-outline {
  background: transparent;
  color: #007bff;
  border: 1px solid #007bff;
}

.btn-outline:hover:not(:disabled) {
  background: #007bff;
  color: white;
}

.btn-danger {
  background: #dc3545;
  color: white;
}

.btn-danger:hover:not(:disabled) {
  background: #c82333;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 10px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow: auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e9ecef;
}

.modal-header h4 {
  margin: 0;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #666;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #333;
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 20px;
  border-top: 1px solid #e9ecef;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.form-control {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  box-sizing: border-box;
}

.form-control:focus {
  outline: none;
  border-color: #007bff;
  box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}

.warning {
  color: #dc3545;
  font-weight: 500;
}

.icon {
  font-size: 16px;
}

@media (max-width: 768px) {
  .backend-item {
    flex-direction: column;
    gap: 15px;
  }
  
  .backend-actions {
    flex-direction: row;
    margin-left: 0;
    flex-wrap: wrap;
  }
  
  .name-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .stats {
    flex-direction: column;
    gap: 10px;
  }
}
</style>