ichieh commited on
Commit
a377724
·
1 Parent(s): 7db85a7

增加桌次命名

Browse files
.history/templates/admin_20250117221129.html ADDED
@@ -0,0 +1,653 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>活動座位管理</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
8
+ <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.css" rel="stylesheet">
9
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
10
+ <style>
11
+ #restartContainer {
12
+ display: none;
13
+ position: fixed;
14
+ top: 50%;
15
+ left: 50%;
16
+ transform: translate(-50%, -50%);
17
+ z-index: 9999;
18
+ text-align: center;
19
+ background: rgba(0, 0, 0, 0.9);
20
+ padding: 2rem;
21
+ border-radius: 10px;
22
+ color: white;
23
+ box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
24
+ }
25
+ #restartContainer h3 {
26
+ margin-bottom: 1rem;
27
+ }
28
+ #restartContainer p {
29
+ margin-bottom: 1.5rem;
30
+ }
31
+ .sleeping-overlay {
32
+ position: fixed;
33
+ top: 0;
34
+ left: 0;
35
+ right: 0;
36
+ bottom: 0;
37
+ background: rgba(0, 0, 0, 0.5);
38
+ z-index: 9998;
39
+ display: none;
40
+ }
41
+ </style>
42
+ </head>
43
+ <body>
44
+ <!-- 重啟系統對話框 -->
45
+ <div id="restartContainer">
46
+ <h3><i class="bi bi-moon-stars"></i> 系統休眠中</h3>
47
+ <p>由於一段時間沒有活動,系統進入了休眠狀態。<br>請點擊下方按鈕重新啟動。</p>
48
+ <button onclick="restartSpace()" class="btn btn-primary">
49
+ <i class="bi bi-arrow-clockwise"></i> 重新啟動系統
50
+ </button>
51
+ </div>
52
+
53
+ <!-- 遮罩層 -->
54
+ <div id="sleepingOverlay" class="sleeping-overlay"></div>
55
+
56
+ <div class="container mt-4">
57
+ <h2>管理員後台</h2>
58
+
59
+ <!-- 桌次設定 -->
60
+ <div class="card mb-4">
61
+ <div class="card-header">
62
+ <h5 class="mb-0">桌次設定</h5>
63
+ </div>
64
+ <div class="card-body">
65
+ <form id="tableSettingsForm">
66
+ <div class="row">
67
+ <div class="col-md-6">
68
+ <div class="mb-3">
69
+ <label for="totalTables" class="form-label">總桌數</label>
70
+ <input type="number" class="form-control" id="totalTables" min="1" required>
71
+ </div>
72
+ </div>
73
+ <div class="col-md-6">
74
+ <div class="mb-3">
75
+ <label for="seatsPerTable" class="form-label">每桌人數</label>
76
+ <input type="number" class="form-control" id="seatsPerTable" min="1" required>
77
+ </div>
78
+ </div>
79
+ </div>
80
+ <button type="submit" class="btn btn-primary">更新設定</button>
81
+ </form>
82
+ </div>
83
+ </div>
84
+
85
+ <!-- 備份和恢復按鈕 -->
86
+ <div class="row mb-4">
87
+ <div class="col">
88
+ <button class="btn btn-success me-2" onclick="backupData()">
89
+ <i class="bi bi-cloud-upload"></i> 備份資料到 Sheets
90
+ </button>
91
+ <button class="btn btn-warning" onclick="restoreData()">
92
+ <i class="bi bi-cloud-download"></i> 從 Sheets 恢復資料
93
+ </button>
94
+ </div>
95
+ </div>
96
+
97
+ <!-- 參與者列表 -->
98
+ <div class="row">
99
+ <div class="col">
100
+ <div class="table-responsive">
101
+ <table class="table table-striped">
102
+ <thead>
103
+ <tr>
104
+ <th>桌次</th>
105
+ <th>座位</th>
106
+ <th>暱稱</th>
107
+ <th>Email</th>
108
+ <th>FB連結</th>
109
+ <th>自我介紹</th>
110
+ <th>付款狀態</th>
111
+ <th>備註</th>
112
+ <th>身份</th>
113
+ <th>報名時間</th>
114
+ <th>操作</th>
115
+ </tr>
116
+ </thead>
117
+ <tbody id="participantsTable">
118
+ <!-- 參與者資料會動態插入這裡 -->
119
+ </tbody>
120
+ </table>
121
+ </div>
122
+ </div>
123
+ </div>
124
+ </div>
125
+
126
+ <!-- 移動座位的 Modal -->
127
+ <div class="modal fade" id="moveParticipantModal" tabindex="-1">
128
+ <div class="modal-dialog">
129
+ <div class="modal-content">
130
+ <div class="modal-header">
131
+ <h5 class="modal-title">移動座位</h5>
132
+ <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
133
+ </div>
134
+ <div class="modal-body">
135
+ <form id="moveParticipantForm">
136
+ <input type="hidden" id="participantId">
137
+ <div class="mb-3">
138
+ <label for="newTableSelect" class="form-label">選擇新桌次</label>
139
+ <select class="form-select" id="newTableSelect">
140
+ <!-- 桌子選項會動態插入這裡 -->
141
+ </select>
142
+ </div>
143
+ </form>
144
+ </div>
145
+ <div class="modal-footer">
146
+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
147
+ <button type="button" class="btn btn-primary" onclick="moveParticipant()">確認移動</button>
148
+ </div>
149
+ </div>
150
+ </div>
151
+ </div>
152
+
153
+ <!-- 查看完整自我介紹的 Modal -->
154
+ <div class="modal fade" id="viewIntroModal" tabindex="-1">
155
+ <div class="modal-dialog">
156
+ <div class="modal-content">
157
+ <div class="modal-header">
158
+ <h5 class="modal-title">自我介紹</h5>
159
+ <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
160
+ </div>
161
+ <div class="modal-body">
162
+ <p id="fullIntroText"></p>
163
+ </div>
164
+ </div>
165
+ </div>
166
+ </div>
167
+
168
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
169
+ <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>
170
+ <script>
171
+ let participants = [];
172
+ let tables = [];
173
+
174
+ // 載入目前設定
175
+ async function loadCurrentSettings() {
176
+ try {
177
+ const response = await fetch('/api/admin/settings');
178
+ const data = await response.json();
179
+ document.getElementById('totalTables').value = data.total_tables;
180
+ document.getElementById('seatsPerTable').value = data.seats_per_table;
181
+ } catch (error) {
182
+ console.error('Error:', error);
183
+ Swal.fire('錯誤', '載入設定失敗', 'error');
184
+ }
185
+ }
186
+
187
+ // 更新桌次設定
188
+ document.getElementById('tableSettingsForm').addEventListener('submit', async (e) => {
189
+ e.preventDefault();
190
+ const totalTables = document.getElementById('totalTables').value;
191
+ const seatsPerTable = document.getElementById('seatsPerTable').value;
192
+
193
+ try {
194
+ const response = await fetch('/api/admin/settings/update', {
195
+ method: 'POST',
196
+ headers: {
197
+ 'Content-Type': 'application/json'
198
+ },
199
+ body: JSON.stringify({
200
+ action: 'update_tables',
201
+ total_tables: parseInt(totalTables),
202
+ seats_per_table: parseInt(seatsPerTable)
203
+ })
204
+ });
205
+
206
+ if (!response.ok) {
207
+ const data = await response.json();
208
+ throw new Error(data.error || '更新設定失敗');
209
+ }
210
+
211
+ const data = await response.json();
212
+ Swal.fire('成功', '桌次設定已更新', 'success');
213
+ loadParticipants(); // 重新載入參與者列表以反映新的桌次設定
214
+ } catch (error) {
215
+ console.error('Error:', error);
216
+ Swal.fire('錯誤', error.message || '更新設定失敗', 'error');
217
+ }
218
+ });
219
+
220
+ // 載入參與者資料
221
+ async function loadParticipants() {
222
+ try {
223
+ const response = await fetch('/api/admin/participants');
224
+ const data = await response.json();
225
+
226
+ if (data.error) {
227
+ throw new Error(data.error);
228
+ }
229
+
230
+ participants = data.participants;
231
+ tables = data.tables;
232
+
233
+ // 依照桌次和座位號排序
234
+ participants.sort((a, b) => {
235
+ // 先比較桌次
236
+ const tableA = parseInt(a.table_id) || 0;
237
+ const tableB = parseInt(b.table_id) || 0;
238
+ if (tableA !== tableB) {
239
+ return tableA - tableB;
240
+ }
241
+ // 如果桌次相同,再比較座位號
242
+ const seatA = parseInt(a.seat_number) || 0;
243
+ const seatB = parseInt(b.seat_number) || 0;
244
+ return seatA - seatB;
245
+ });
246
+
247
+ renderParticipants();
248
+ } catch (error) {
249
+ console.error('Error:', error);
250
+ Swal.fire('錯誤', '載入參與者資料失敗:' + error.message, 'error');
251
+ }
252
+ }
253
+
254
+ // 渲染參與者列表
255
+ function renderParticipants() {
256
+ const tbody = document.getElementById('participantsTable');
257
+ if (!tbody) {
258
+ console.error('找不到參與者列表元素');
259
+ return;
260
+ }
261
+
262
+ tbody.innerHTML = '';
263
+
264
+ participants.forEach(participant => {
265
+ const tr = document.createElement('tr');
266
+ tr.className = participant.is_table_leader ? 'table-leader' : '';
267
+
268
+ // 找到對應的桌子
269
+ const table = tables.find(t => t.id === participant.table_id);
270
+ const tableName = table ? table.name : '未分配';
271
+
272
+ tr.innerHTML = `
273
+ <td>${tableName}</td>
274
+ <td>${participant.seat_number || '-'}</td>
275
+ <td>${participant.nickname}</td>
276
+ <td>${participant.email}</td>
277
+ <td>${participant.fb_link ? `<a href="${participant.fb_link}" target="_blank">FB連結</a>` : '-'}</td>
278
+ <td>
279
+ ${participant.introduction ?
280
+ `${participant.introduction.length > 5 ?
281
+ participant.introduction.substring(0, 5) + '...' :
282
+ participant.introduction}
283
+ <button class="btn btn-link btn-sm" onclick="showFullIntroduction('${participant.introduction.replace(/'/g, "\\'")}')">
284
+ <i class="bi bi-eye"></i>
285
+ </button>`
286
+ : '-'}
287
+ </td>
288
+ <td>
289
+ <button class="btn ${participant.has_paid ? 'btn-success' : 'btn-danger'} btn-sm"
290
+ onclick="togglePayment(${participant.id})">
291
+ ${participant.has_paid ? '已付款' : '未付款'}
292
+ </button>
293
+ </td>
294
+ <td>
295
+ ${participant.note ?
296
+ `<span class="note-preview d-inline-block text-truncate" style="max-width: 100px;" onclick="editNote(${participant.id}, '${participant.note.replace(/'/g, "\\'")}')">
297
+ ${participant.note}
298
+ </span>`
299
+ : `<button class="btn btn-outline-secondary btn-sm" onclick="editNote(${participant.id}, '')">
300
+ <i class="bi bi-pencil"></i>
301
+ </button>`}
302
+ </td>
303
+ <td>
304
+ <span class="${participant.is_table_leader ? 'badge bg-success' : 'badge bg-secondary'}">
305
+ ${participant.is_table_leader ? '桌長' : '參與者'}
306
+ </span>
307
+ </td>
308
+ <td>${new Date(participant.created_at).toLocaleString('zh-TW', { timeZone: 'Asia/Taipei' })}</td>
309
+ <td>
310
+ <div class="btn-group" role="group">
311
+ <button class="btn btn-primary btn-sm" onclick="showMoveDialog(${participant.id})">
312
+ 移動座位
313
+ </button>
314
+ ${participant.is_table_leader ?
315
+ `<button class="btn btn-warning btn-sm" onclick="toggleLeader(${participant.id})">
316
+ 取消桌長
317
+ </button>` :
318
+ `<button class="btn btn-success btn-sm" onclick="toggleLeader(${participant.id})">
319
+ 設為桌長
320
+ </button>`
321
+ }
322
+ <button class="btn btn-danger btn-sm" onclick="deleteParticipant(${participant.id})">
323
+ 刪除
324
+ </button>
325
+ </div>
326
+ </td>
327
+ `;
328
+ tbody.appendChild(tr);
329
+ });
330
+ }
331
+
332
+ // 顯示移動座位的 Modal
333
+ function showMoveDialog(participantId) {
334
+ document.getElementById('participantId').value = participantId;
335
+ const select = document.getElementById('newTableSelect');
336
+ select.innerHTML = '';
337
+
338
+ tables.forEach(table => {
339
+ if (table.current_seats < table.capacity) {
340
+ const option = document.createElement('option');
341
+ option.value = table.id;
342
+ option.textContent = `${table.name} (${table.current_seats}/${table.capacity})`;
343
+ select.appendChild(option);
344
+ }
345
+ });
346
+
347
+ new bootstrap.Modal(document.getElementById('moveParticipantModal')).show();
348
+ }
349
+
350
+ // 移動參與者座位
351
+ async function moveParticipant() {
352
+ const participantId = document.getElementById('participantId').value;
353
+ const newTableId = document.getElementById('newTableSelect').value;
354
+
355
+ try {
356
+ const response = await fetch(`/api/admin/participant/${participantId}/move`, {
357
+ method: 'POST',
358
+ headers: {
359
+ 'Content-Type': 'application/json'
360
+ },
361
+ body: JSON.stringify({ new_table_id: newTableId })
362
+ });
363
+
364
+ const data = await response.json();
365
+
366
+ if (response.ok) {
367
+ Swal.fire('成功', '已移動座位', 'success');
368
+ bootstrap.Modal.getInstance(document.getElementById('moveParticipantModal')).hide();
369
+ loadParticipants();
370
+ } else {
371
+ throw new Error(data.error);
372
+ }
373
+ } catch (error) {
374
+ console.error('Error:', error);
375
+ Swal.fire('錯誤', error.message || '移動座位失敗', 'error');
376
+ }
377
+ }
378
+
379
+ // 切換桌長狀態
380
+ async function toggleLeader(participantId) {
381
+ try {
382
+ const response = await fetch(`/api/admin/participant/${participantId}/toggle-leader`, {
383
+ method: 'POST',
384
+ headers: {
385
+ 'Content-Type': 'application/json'
386
+ }
387
+ });
388
+
389
+ const data = await response.json();
390
+
391
+ if (response.ok) {
392
+ Swal.fire('成功', data.message, 'success');
393
+ loadParticipants();
394
+ } else {
395
+ throw new Error(data.error);
396
+ }
397
+ } catch (error) {
398
+ console.error('Error:', error);
399
+ Swal.fire('錯誤', error.message || '操作失敗', 'error');
400
+ }
401
+ }
402
+
403
+ // 查看完整自我介紹
404
+ function showFullIntroduction(intro) {
405
+ document.getElementById('fullIntroText').textContent = intro;
406
+ new bootstrap.Modal(document.getElementById('viewIntroModal')).show();
407
+ }
408
+
409
+ // 刪除報名者
410
+ async function deleteParticipant(participantId) {
411
+ const result = await Swal.fire({
412
+ title: '確定要刪除嗎?',
413
+ text: "此操作無法復原!",
414
+ icon: 'warning',
415
+ showCancelButton: true,
416
+ confirmButtonColor: '#d33',
417
+ cancelButtonColor: '#3085d6',
418
+ confirmButtonText: '是的,刪除!',
419
+ cancelButtonText: '取消'
420
+ });
421
+
422
+ if (result.isConfirmed) {
423
+ try {
424
+ const response = await fetch(`/api/admin/participants/${participantId}`, {
425
+ method: 'DELETE'
426
+ });
427
+
428
+ if (response.ok) {
429
+ Swal.fire('已刪除!', '該報名者已被刪除。', 'success');
430
+ loadParticipants();
431
+ } else {
432
+ throw new Error('刪除失敗');
433
+ }
434
+ } catch (error) {
435
+ console.error('Error:', error);
436
+ Swal.fire('錯誤', error.message, 'error');
437
+ }
438
+ }
439
+ }
440
+
441
+ // 切換付款狀態
442
+ async function togglePayment(participantId) {
443
+ try {
444
+ const response = await fetch(`/api/admin/participant/${participantId}/payment`, {
445
+ method: 'POST',
446
+ headers: {
447
+ 'Content-Type': 'application/json'
448
+ }
449
+ });
450
+
451
+ const data = await response.json();
452
+
453
+ if (response.ok) {
454
+ Swal.fire({
455
+ icon: 'success',
456
+ title: '成功',
457
+ text: data.message,
458
+ showConfirmButton: false,
459
+ timer: 1500
460
+ });
461
+ await loadParticipants();
462
+ } else {
463
+ throw new Error(data.error);
464
+ }
465
+ } catch (error) {
466
+ console.error('Error:', error);
467
+ Swal.fire('錯誤', error.message || '操作失敗', 'error');
468
+ }
469
+ }
470
+
471
+ // 編輯備註
472
+ async function editNote(participantId, currentNote) {
473
+ const { value: note } = await Swal.fire({
474
+ title: '編輯備註',
475
+ input: 'textarea',
476
+ inputValue: currentNote,
477
+ inputPlaceholder: '請輸入備註內容...',
478
+ showCancelButton: true,
479
+ confirmButtonText: '儲存',
480
+ cancelButtonText: '取消'
481
+ });
482
+
483
+ if (note !== undefined) { // 用戶點擊了儲存
484
+ try {
485
+ const response = await fetch(`/api/admin/participant/${participantId}/note`, {
486
+ method: 'POST',
487
+ headers: {
488
+ 'Content-Type': 'application/json'
489
+ },
490
+ body: JSON.stringify({ note })
491
+ });
492
+
493
+ if (response.ok) {
494
+ await loadParticipants(); // 重新載入參與者列表
495
+ Swal.fire({
496
+ icon: 'success',
497
+ title: '備註已更新',
498
+ showConfirmButton: false,
499
+ timer: 1500
500
+ });
501
+ } else {
502
+ throw new Error('更新備註失敗');
503
+ }
504
+ } catch (error) {
505
+ console.error('Error:', error);
506
+ Swal.fire('錯誤', error.message, 'error');
507
+ }
508
+ }
509
+ }
510
+
511
+ // 顯示完整備註
512
+ function showNote(note) {
513
+ if (note) {
514
+ Swal.fire({
515
+ title: '備註內容',
516
+ text: note,
517
+ confirmButtonText: '關閉'
518
+ });
519
+ }
520
+ }
521
+
522
+ // 從 Google Sheets 恢復資料
523
+ async function restoreData() {
524
+ try {
525
+ const result = await Swal.fire({
526
+ title: '確認恢復資料?',
527
+ text: '這將會覆蓋目前的所有資料!',
528
+ icon: 'warning',
529
+ showCancelButton: true,
530
+ confirmButtonColor: '#3085d6',
531
+ cancelButtonColor: '#d33',
532
+ confirmButtonText: '是,恢復資料',
533
+ cancelButtonText: '取消'
534
+ });
535
+
536
+ if (result.isConfirmed) {
537
+ // 顯示載入中
538
+ Swal.fire({
539
+ title: '處理中...',
540
+ text: '正在從 Google Sheets 恢復資料',
541
+ allowOutsideClick: false,
542
+ allowEscapeKey: false,
543
+ allowEnterKey: false,
544
+ showConfirmButton: false,
545
+ didOpen: () => {
546
+ Swal.showLoading();
547
+ }
548
+ });
549
+
550
+ const response = await fetch('/api/admin/settings/restore', {
551
+ method: 'POST',
552
+ headers: {
553
+ 'Content-Type': 'application/json'
554
+ }
555
+ });
556
+
557
+ const data = await response.json();
558
+
559
+ // 關閉載入中
560
+ await Swal.close();
561
+
562
+ if (response.ok) {
563
+ await Swal.fire({
564
+ icon: 'success',
565
+ title: '成功',
566
+ text: data.message,
567
+ showConfirmButton: false,
568
+ timer: 1500
569
+ });
570
+ await loadParticipants();
571
+ } else {
572
+ throw new Error(data.error || '恢復資料失敗');
573
+ }
574
+ }
575
+ } catch (error) {
576
+ console.error('Error:', error);
577
+ Swal.fire('錯誤', error.message || '恢復資料失敗', 'error');
578
+ }
579
+ }
580
+
581
+ // 備份資料到 Google Sheets
582
+ async function backupData() {
583
+ try {
584
+ const response = await fetch('/api/admin/settings/backup', {
585
+ method: 'POST',
586
+ headers: {
587
+ 'Content-Type': 'application/json'
588
+ }
589
+ });
590
+
591
+ const data = await response.json();
592
+
593
+ if (response.ok) {
594
+ Swal.fire('成功', '資料已成功備份到 Google Sheets', 'success');
595
+ } else {
596
+ throw new Error(data.error);
597
+ }
598
+ } catch (error) {
599
+ console.error('Error:', error);
600
+ Swal.fire('錯誤', error.message || '備份失敗', 'error');
601
+ }
602
+ }
603
+
604
+ // 檢查系統狀態的函數
605
+ async function checkSystemStatus() {
606
+ try {
607
+ const response = await fetch('/api/admin/participants');
608
+ if (!response.ok) {
609
+ showRestartButton();
610
+ } else {
611
+ hideRestartButton();
612
+ }
613
+ } catch (error) {
614
+ console.error('Error checking system status:', error);
615
+ showRestartButton();
616
+ }
617
+ }
618
+
619
+ // 顯示重啟按鈕
620
+ function showRestartButton() {
621
+ document.getElementById('restartContainer').style.display = 'block';
622
+ document.getElementById('sleepingOverlay').style.display = 'block';
623
+ }
624
+
625
+ // 隱藏重啟按鈕
626
+ function hideRestartButton() {
627
+ document.getElementById('restartContainer').style.display = 'none';
628
+ document.getElementById('sleepingOverlay').style.display = 'none';
629
+ }
630
+
631
+ // 重啟系統
632
+ async function restartSpace() {
633
+ const button = document.querySelector('#restartContainer button');
634
+ const originalText = button.innerHTML;
635
+ button.disabled = true;
636
+ button.innerHTML = '<i class="bi bi-arrow-clockwise"></i> 正在重新啟動...';
637
+
638
+ // 重新載入頁面
639
+ window.location.reload();
640
+ }
641
+
642
+ // 定期檢查系統狀態
643
+ setInterval(checkSystemStatus, 30000); // 每30秒檢查一次
644
+
645
+ // 初始化頁面時也檢查一次
646
+ document.addEventListener('DOMContentLoaded', () => {
647
+ loadCurrentSettings();
648
+ loadParticipants();
649
+ checkSystemStatus();
650
+ });
651
+ </script>
652
+ </body>
653
+ </html>
.history/templates/admin_20250118083123.html ADDED
@@ -0,0 +1,668 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>活動座位管理</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
8
+ <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.css" rel="stylesheet">
9
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
10
+ <style>
11
+ #restartContainer {
12
+ display: none;
13
+ position: fixed;
14
+ top: 50%;
15
+ left: 50%;
16
+ transform: translate(-50%, -50%);
17
+ z-index: 9999;
18
+ text-align: center;
19
+ background: rgba(0, 0, 0, 0.9);
20
+ padding: 2rem;
21
+ border-radius: 10px;
22
+ color: white;
23
+ box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
24
+ }
25
+ #restartContainer h3 {
26
+ margin-bottom: 1rem;
27
+ }
28
+ #restartContainer p {
29
+ margin-bottom: 1.5rem;
30
+ }
31
+ .sleeping-overlay {
32
+ position: fixed;
33
+ top: 0;
34
+ left: 0;
35
+ right: 0;
36
+ bottom: 0;
37
+ background: rgba(0, 0, 0, 0.5);
38
+ z-index: 9998;
39
+ display: none;
40
+ }
41
+ </style>
42
+ </head>
43
+ <body>
44
+ <!-- 重啟系統對話框 -->
45
+ <div id="restartContainer">
46
+ <h3><i class="bi bi-moon-stars"></i> 系統休眠中</h3>
47
+ <p>由於一段時間沒有活動,系統進入了休眠狀態。<br>請點擊下方按鈕重新啟動。</p>
48
+ <button onclick="restartSpace()" class="btn btn-primary">
49
+ <i class="bi bi-arrow-clockwise"></i> 重新啟動系統
50
+ </button>
51
+ </div>
52
+
53
+ <!-- 遮罩層 -->
54
+ <div id="sleepingOverlay" class="sleeping-overlay"></div>
55
+
56
+ <div class="container mt-4">
57
+ <h2>管理員後台</h2>
58
+
59
+ <!-- 桌次設定 -->
60
+ <div class="card mb-4">
61
+ <div class="card-header">
62
+ <h5 class="mb-0">桌次設定</h5>
63
+ </div>
64
+ <div class="card-body">
65
+ <form id="tableSettingsForm">
66
+ <div class="row">
67
+ <div class="col-md-6">
68
+ <div class="mb-3">
69
+ <label for="totalTables" class="form-label">總桌數</label>
70
+ <input type="number" class="form-control" id="totalTables" min="1" required>
71
+ </div>
72
+ </div>
73
+ <div class="col-md-6">
74
+ <div class="mb-3">
75
+ <label for="seatsPerTable" class="form-label">每桌人數</label>
76
+ <input type="number" class="form-control" id="seatsPerTable" min="1" required>
77
+ </div>
78
+ </div>
79
+ </div>
80
+ <button type="submit" class="btn btn-primary">更新設定</button>
81
+ </form>
82
+ </div>
83
+ </div>
84
+
85
+ <!-- 桌次命名設定 -->
86
+ <div class="card mb-4">
87
+ <div class="card-header d-flex justify-content-between align-items-center">
88
+ <h5 class="mb-0">桌次命名</h5>
89
+ <button type="button" class="btn btn-outline-primary btn-sm" onclick="loadTableNames()">
90
+ <i class="bi bi-arrow-clockwise"></i> 重新載入
91
+ </button>
92
+ </div>
93
+ <div class="card-body">
94
+ <div class="row" id="tableNameSettings">
95
+ <!-- 桌次命名表單將由 JavaScript 動態生成 -->
96
+ </div>
97
+ </div>
98
+ </div>
99
+
100
+ <!-- 備份和恢復按鈕 -->
101
+ <div class="row mb-4">
102
+ <div class="col">
103
+ <button class="btn btn-success me-2" onclick="backupData()">
104
+ <i class="bi bi-cloud-upload"></i> 備份資料到 Sheets
105
+ </button>
106
+ <button class="btn btn-warning" onclick="restoreData()">
107
+ <i class="bi bi-cloud-download"></i> 從 Sheets 恢復資料
108
+ </button>
109
+ </div>
110
+ </div>
111
+
112
+ <!-- 參與者列表 -->
113
+ <div class="row">
114
+ <div class="col">
115
+ <div class="table-responsive">
116
+ <table class="table table-striped">
117
+ <thead>
118
+ <tr>
119
+ <th>桌次</th>
120
+ <th>座位</th>
121
+ <th>暱稱</th>
122
+ <th>Email</th>
123
+ <th>FB連結</th>
124
+ <th>自我介紹</th>
125
+ <th>付款狀態</th>
126
+ <th>備註</th>
127
+ <th>身份</th>
128
+ <th>報名時間</th>
129
+ <th>操作</th>
130
+ </tr>
131
+ </thead>
132
+ <tbody id="participantsTable">
133
+ <!-- 參與者資料會動態插入這裡 -->
134
+ </tbody>
135
+ </table>
136
+ </div>
137
+ </div>
138
+ </div>
139
+ </div>
140
+
141
+ <!-- 移動座位的 Modal -->
142
+ <div class="modal fade" id="moveParticipantModal" tabindex="-1">
143
+ <div class="modal-dialog">
144
+ <div class="modal-content">
145
+ <div class="modal-header">
146
+ <h5 class="modal-title">移動座位</h5>
147
+ <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
148
+ </div>
149
+ <div class="modal-body">
150
+ <form id="moveParticipantForm">
151
+ <input type="hidden" id="participantId">
152
+ <div class="mb-3">
153
+ <label for="newTableSelect" class="form-label">選擇新桌次</label>
154
+ <select class="form-select" id="newTableSelect">
155
+ <!-- 桌子選項會動態插入這裡 -->
156
+ </select>
157
+ </div>
158
+ </form>
159
+ </div>
160
+ <div class="modal-footer">
161
+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
162
+ <button type="button" class="btn btn-primary" onclick="moveParticipant()">確認移動</button>
163
+ </div>
164
+ </div>
165
+ </div>
166
+ </div>
167
+
168
+ <!-- 查看完整自我介紹的 Modal -->
169
+ <div class="modal fade" id="viewIntroModal" tabindex="-1">
170
+ <div class="modal-dialog">
171
+ <div class="modal-content">
172
+ <div class="modal-header">
173
+ <h5 class="modal-title">自我介紹</h5>
174
+ <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
175
+ </div>
176
+ <div class="modal-body">
177
+ <p id="fullIntroText"></p>
178
+ </div>
179
+ </div>
180
+ </div>
181
+ </div>
182
+
183
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
184
+ <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>
185
+ <script>
186
+ let participants = [];
187
+ let tables = [];
188
+
189
+ // 載入目前設定
190
+ async function loadCurrentSettings() {
191
+ try {
192
+ const response = await fetch('/api/admin/settings');
193
+ const data = await response.json();
194
+ document.getElementById('totalTables').value = data.total_tables;
195
+ document.getElementById('seatsPerTable').value = data.seats_per_table;
196
+ } catch (error) {
197
+ console.error('Error:', error);
198
+ Swal.fire('錯誤', '載入設定失敗', 'error');
199
+ }
200
+ }
201
+
202
+ // 更新桌次設定
203
+ document.getElementById('tableSettingsForm').addEventListener('submit', async (e) => {
204
+ e.preventDefault();
205
+ const totalTables = document.getElementById('totalTables').value;
206
+ const seatsPerTable = document.getElementById('seatsPerTable').value;
207
+
208
+ try {
209
+ const response = await fetch('/api/admin/settings/update', {
210
+ method: 'POST',
211
+ headers: {
212
+ 'Content-Type': 'application/json'
213
+ },
214
+ body: JSON.stringify({
215
+ action: 'update_tables',
216
+ total_tables: parseInt(totalTables),
217
+ seats_per_table: parseInt(seatsPerTable)
218
+ })
219
+ });
220
+
221
+ if (!response.ok) {
222
+ const data = await response.json();
223
+ throw new Error(data.error || '更新設定失敗');
224
+ }
225
+
226
+ const data = await response.json();
227
+ Swal.fire('成功', '桌次設定已更新', 'success');
228
+ loadParticipants(); // 重新載入參與者列表以反映新的桌次設定
229
+ } catch (error) {
230
+ console.error('Error:', error);
231
+ Swal.fire('錯誤', error.message || '更新設定失敗', 'error');
232
+ }
233
+ });
234
+
235
+ // 載入參與者資料
236
+ async function loadParticipants() {
237
+ try {
238
+ const response = await fetch('/api/admin/participants');
239
+ const data = await response.json();
240
+
241
+ if (data.error) {
242
+ throw new Error(data.error);
243
+ }
244
+
245
+ participants = data.participants;
246
+ tables = data.tables;
247
+
248
+ // 依照桌次和座位號排序
249
+ participants.sort((a, b) => {
250
+ // 先比較桌次
251
+ const tableA = parseInt(a.table_id) || 0;
252
+ const tableB = parseInt(b.table_id) || 0;
253
+ if (tableA !== tableB) {
254
+ return tableA - tableB;
255
+ }
256
+ // 如果桌次相同,再比較座位號
257
+ const seatA = parseInt(a.seat_number) || 0;
258
+ const seatB = parseInt(b.seat_number) || 0;
259
+ return seatA - seatB;
260
+ });
261
+
262
+ renderParticipants();
263
+ } catch (error) {
264
+ console.error('Error:', error);
265
+ Swal.fire('錯誤', '載入參與者資料失敗:' + error.message, 'error');
266
+ }
267
+ }
268
+
269
+ // 渲染參與者列表
270
+ function renderParticipants() {
271
+ const tbody = document.getElementById('participantsTable');
272
+ if (!tbody) {
273
+ console.error('找不到參與者列表元素');
274
+ return;
275
+ }
276
+
277
+ tbody.innerHTML = '';
278
+
279
+ participants.forEach(participant => {
280
+ const tr = document.createElement('tr');
281
+ tr.className = participant.is_table_leader ? 'table-leader' : '';
282
+
283
+ // 找到對應的桌子
284
+ const table = tables.find(t => t.id === participant.table_id);
285
+ const tableName = table ? table.name : '未分配';
286
+
287
+ tr.innerHTML = `
288
+ <td>${tableName}</td>
289
+ <td>${participant.seat_number || '-'}</td>
290
+ <td>${participant.nickname}</td>
291
+ <td>${participant.email}</td>
292
+ <td>${participant.fb_link ? `<a href="${participant.fb_link}" target="_blank">FB連結</a>` : '-'}</td>
293
+ <td>
294
+ ${participant.introduction ?
295
+ `${participant.introduction.length > 5 ?
296
+ participant.introduction.substring(0, 5) + '...' :
297
+ participant.introduction}
298
+ <button class="btn btn-link btn-sm" onclick="showFullIntroduction('${participant.introduction.replace(/'/g, "\\'")}')">
299
+ <i class="bi bi-eye"></i>
300
+ </button>`
301
+ : '-'}
302
+ </td>
303
+ <td>
304
+ <button class="btn ${participant.has_paid ? 'btn-success' : 'btn-danger'} btn-sm"
305
+ onclick="togglePayment(${participant.id})">
306
+ ${participant.has_paid ? '已付款' : '未付款'}
307
+ </button>
308
+ </td>
309
+ <td>
310
+ ${participant.note ?
311
+ `<span class="note-preview d-inline-block text-truncate" style="max-width: 100px;" onclick="editNote(${participant.id}, '${participant.note.replace(/'/g, "\\'")}')">
312
+ ${participant.note}
313
+ </span>`
314
+ : `<button class="btn btn-outline-secondary btn-sm" onclick="editNote(${participant.id}, '')">
315
+ <i class="bi bi-pencil"></i>
316
+ </button>`}
317
+ </td>
318
+ <td>
319
+ <span class="${participant.is_table_leader ? 'badge bg-success' : 'badge bg-secondary'}">
320
+ ${participant.is_table_leader ? '桌長' : '參與者'}
321
+ </span>
322
+ </td>
323
+ <td>${new Date(participant.created_at).toLocaleString('zh-TW', { timeZone: 'Asia/Taipei' })}</td>
324
+ <td>
325
+ <div class="btn-group" role="group">
326
+ <button class="btn btn-primary btn-sm" onclick="showMoveDialog(${participant.id})">
327
+ 移動座位
328
+ </button>
329
+ ${participant.is_table_leader ?
330
+ `<button class="btn btn-warning btn-sm" onclick="toggleLeader(${participant.id})">
331
+ 取消桌長
332
+ </button>` :
333
+ `<button class="btn btn-success btn-sm" onclick="toggleLeader(${participant.id})">
334
+ 設為桌長
335
+ </button>`
336
+ }
337
+ <button class="btn btn-danger btn-sm" onclick="deleteParticipant(${participant.id})">
338
+ 刪除
339
+ </button>
340
+ </div>
341
+ </td>
342
+ `;
343
+ tbody.appendChild(tr);
344
+ });
345
+ }
346
+
347
+ // 顯示移動座位的 Modal
348
+ function showMoveDialog(participantId) {
349
+ document.getElementById('participantId').value = participantId;
350
+ const select = document.getElementById('newTableSelect');
351
+ select.innerHTML = '';
352
+
353
+ tables.forEach(table => {
354
+ if (table.current_seats < table.capacity) {
355
+ const option = document.createElement('option');
356
+ option.value = table.id;
357
+ option.textContent = `${table.name} (${table.current_seats}/${table.capacity})`;
358
+ select.appendChild(option);
359
+ }
360
+ });
361
+
362
+ new bootstrap.Modal(document.getElementById('moveParticipantModal')).show();
363
+ }
364
+
365
+ // 移動參與者座位
366
+ async function moveParticipant() {
367
+ const participantId = document.getElementById('participantId').value;
368
+ const newTableId = document.getElementById('newTableSelect').value;
369
+
370
+ try {
371
+ const response = await fetch(`/api/admin/participant/${participantId}/move`, {
372
+ method: 'POST',
373
+ headers: {
374
+ 'Content-Type': 'application/json'
375
+ },
376
+ body: JSON.stringify({ new_table_id: newTableId })
377
+ });
378
+
379
+ const data = await response.json();
380
+
381
+ if (response.ok) {
382
+ Swal.fire('成功', '已移動座位', 'success');
383
+ bootstrap.Modal.getInstance(document.getElementById('moveParticipantModal')).hide();
384
+ loadParticipants();
385
+ } else {
386
+ throw new Error(data.error);
387
+ }
388
+ } catch (error) {
389
+ console.error('Error:', error);
390
+ Swal.fire('錯誤', error.message || '移動座位失敗', 'error');
391
+ }
392
+ }
393
+
394
+ // 切換桌長狀態
395
+ async function toggleLeader(participantId) {
396
+ try {
397
+ const response = await fetch(`/api/admin/participant/${participantId}/toggle-leader`, {
398
+ method: 'POST',
399
+ headers: {
400
+ 'Content-Type': 'application/json'
401
+ }
402
+ });
403
+
404
+ const data = await response.json();
405
+
406
+ if (response.ok) {
407
+ Swal.fire('成功', data.message, 'success');
408
+ loadParticipants();
409
+ } else {
410
+ throw new Error(data.error);
411
+ }
412
+ } catch (error) {
413
+ console.error('Error:', error);
414
+ Swal.fire('錯誤', error.message || '操作失敗', 'error');
415
+ }
416
+ }
417
+
418
+ // 查看完整自我介紹
419
+ function showFullIntroduction(intro) {
420
+ document.getElementById('fullIntroText').textContent = intro;
421
+ new bootstrap.Modal(document.getElementById('viewIntroModal')).show();
422
+ }
423
+
424
+ // 刪除報名者
425
+ async function deleteParticipant(participantId) {
426
+ const result = await Swal.fire({
427
+ title: '確定要刪除嗎?',
428
+ text: "此操作無法復原!",
429
+ icon: 'warning',
430
+ showCancelButton: true,
431
+ confirmButtonColor: '#d33',
432
+ cancelButtonColor: '#3085d6',
433
+ confirmButtonText: '是的,刪除!',
434
+ cancelButtonText: '取消'
435
+ });
436
+
437
+ if (result.isConfirmed) {
438
+ try {
439
+ const response = await fetch(`/api/admin/participants/${participantId}`, {
440
+ method: 'DELETE'
441
+ });
442
+
443
+ if (response.ok) {
444
+ Swal.fire('已刪除!', '該報名者已被刪除。', 'success');
445
+ loadParticipants();
446
+ } else {
447
+ throw new Error('刪除失敗');
448
+ }
449
+ } catch (error) {
450
+ console.error('Error:', error);
451
+ Swal.fire('錯誤', error.message, 'error');
452
+ }
453
+ }
454
+ }
455
+
456
+ // 切換付款狀態
457
+ async function togglePayment(participantId) {
458
+ try {
459
+ const response = await fetch(`/api/admin/participant/${participantId}/payment`, {
460
+ method: 'POST',
461
+ headers: {
462
+ 'Content-Type': 'application/json'
463
+ }
464
+ });
465
+
466
+ const data = await response.json();
467
+
468
+ if (response.ok) {
469
+ Swal.fire({
470
+ icon: 'success',
471
+ title: '成功',
472
+ text: data.message,
473
+ showConfirmButton: false,
474
+ timer: 1500
475
+ });
476
+ await loadParticipants();
477
+ } else {
478
+ throw new Error(data.error);
479
+ }
480
+ } catch (error) {
481
+ console.error('Error:', error);
482
+ Swal.fire('錯誤', error.message || '操作失敗', 'error');
483
+ }
484
+ }
485
+
486
+ // 編輯備註
487
+ async function editNote(participantId, currentNote) {
488
+ const { value: note } = await Swal.fire({
489
+ title: '編輯備註',
490
+ input: 'textarea',
491
+ inputValue: currentNote,
492
+ inputPlaceholder: '請輸入備註內容...',
493
+ showCancelButton: true,
494
+ confirmButtonText: '儲存',
495
+ cancelButtonText: '取消'
496
+ });
497
+
498
+ if (note !== undefined) { // 用戶點擊了儲存
499
+ try {
500
+ const response = await fetch(`/api/admin/participant/${participantId}/note`, {
501
+ method: 'POST',
502
+ headers: {
503
+ 'Content-Type': 'application/json'
504
+ },
505
+ body: JSON.stringify({ note })
506
+ });
507
+
508
+ if (response.ok) {
509
+ await loadParticipants(); // 重新載入參與者列表
510
+ Swal.fire({
511
+ icon: 'success',
512
+ title: '備註已更新',
513
+ showConfirmButton: false,
514
+ timer: 1500
515
+ });
516
+ } else {
517
+ throw new Error('更新備註失敗');
518
+ }
519
+ } catch (error) {
520
+ console.error('Error:', error);
521
+ Swal.fire('錯誤', error.message, 'error');
522
+ }
523
+ }
524
+ }
525
+
526
+ // 顯示完整備註
527
+ function showNote(note) {
528
+ if (note) {
529
+ Swal.fire({
530
+ title: '備註內容',
531
+ text: note,
532
+ confirmButtonText: '關閉'
533
+ });
534
+ }
535
+ }
536
+
537
+ // 從 Google Sheets 恢復資料
538
+ async function restoreData() {
539
+ try {
540
+ const result = await Swal.fire({
541
+ title: '確認恢復資料?',
542
+ text: '這將會覆蓋目前的所有資料!',
543
+ icon: 'warning',
544
+ showCancelButton: true,
545
+ confirmButtonColor: '#3085d6',
546
+ cancelButtonColor: '#d33',
547
+ confirmButtonText: '是,恢復資料',
548
+ cancelButtonText: '取消'
549
+ });
550
+
551
+ if (result.isConfirmed) {
552
+ // 顯示載入中
553
+ Swal.fire({
554
+ title: '處理中...',
555
+ text: '正在從 Google Sheets 恢復資料',
556
+ allowOutsideClick: false,
557
+ allowEscapeKey: false,
558
+ allowEnterKey: false,
559
+ showConfirmButton: false,
560
+ didOpen: () => {
561
+ Swal.showLoading();
562
+ }
563
+ });
564
+
565
+ const response = await fetch('/api/admin/settings/restore', {
566
+ method: 'POST',
567
+ headers: {
568
+ 'Content-Type': 'application/json'
569
+ }
570
+ });
571
+
572
+ const data = await response.json();
573
+
574
+ // 關閉載入中
575
+ await Swal.close();
576
+
577
+ if (response.ok) {
578
+ await Swal.fire({
579
+ icon: 'success',
580
+ title: '成功',
581
+ text: data.message,
582
+ showConfirmButton: false,
583
+ timer: 1500
584
+ });
585
+ await loadParticipants();
586
+ } else {
587
+ throw new Error(data.error || '恢復資料失敗');
588
+ }
589
+ }
590
+ } catch (error) {
591
+ console.error('Error:', error);
592
+ Swal.fire('錯誤', error.message || '恢復資料失敗', 'error');
593
+ }
594
+ }
595
+
596
+ // 備份資料到 Google Sheets
597
+ async function backupData() {
598
+ try {
599
+ const response = await fetch('/api/admin/settings/backup', {
600
+ method: 'POST',
601
+ headers: {
602
+ 'Content-Type': 'application/json'
603
+ }
604
+ });
605
+
606
+ const data = await response.json();
607
+
608
+ if (response.ok) {
609
+ Swal.fire('成功', '資料已成功備份到 Google Sheets', 'success');
610
+ } else {
611
+ throw new Error(data.error);
612
+ }
613
+ } catch (error) {
614
+ console.error('Error:', error);
615
+ Swal.fire('錯誤', error.message || '備份失敗', 'error');
616
+ }
617
+ }
618
+
619
+ // 檢查系統狀態的函數
620
+ async function checkSystemStatus() {
621
+ try {
622
+ const response = await fetch('/api/admin/participants');
623
+ if (!response.ok) {
624
+ showRestartButton();
625
+ } else {
626
+ hideRestartButton();
627
+ }
628
+ } catch (error) {
629
+ console.error('Error checking system status:', error);
630
+ showRestartButton();
631
+ }
632
+ }
633
+
634
+ // 顯示重啟按鈕
635
+ function showRestartButton() {
636
+ document.getElementById('restartContainer').style.display = 'block';
637
+ document.getElementById('sleepingOverlay').style.display = 'block';
638
+ }
639
+
640
+ // 隱藏重啟按鈕
641
+ function hideRestartButton() {
642
+ document.getElementById('restartContainer').style.display = 'none';
643
+ document.getElementById('sleepingOverlay').style.display = 'none';
644
+ }
645
+
646
+ // 重啟系統
647
+ async function restartSpace() {
648
+ const button = document.querySelector('#restartContainer button');
649
+ const originalText = button.innerHTML;
650
+ button.disabled = true;
651
+ button.innerHTML = '<i class="bi bi-arrow-clockwise"></i> 正在重新啟動...';
652
+
653
+ // 重新載入頁面
654
+ window.location.reload();
655
+ }
656
+
657
+ // 定期檢查系統狀態
658
+ setInterval(checkSystemStatus, 30000); // 每30秒檢查一次
659
+
660
+ // 初始化頁面時也檢查一次
661
+ document.addEventListener('DOMContentLoaded', () => {
662
+ loadCurrentSettings();
663
+ loadParticipants();
664
+ checkSystemStatus();
665
+ });
666
+ </script>
667
+ </body>
668
+ </html>
app.py CHANGED
@@ -47,7 +47,7 @@ db = SQLAlchemy(app)
47
 
48
  class Table(db.Model):
49
  id = db.Column(db.Integer, primary_key=True)
50
- name = db.Column(db.String(50), nullable=False)
51
  capacity = db.Column(db.Integer, nullable=False)
52
  participants = db.relationship('Participant', backref='table', lazy=True)
53
 
@@ -316,6 +316,30 @@ def update_settings():
316
  print("本地資料庫更新成功")
317
  return jsonify({'message': '設定已更新', 'sync_result': sync_result})
318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  else:
320
  return jsonify({'error': '無效的操作'}), 400
321
 
@@ -548,7 +572,7 @@ def get_tables():
548
 
549
  result = [{
550
  'id': table.id,
551
- 'name': table.name,
552
  'capacity': table.capacity,
553
  'participants': [{
554
  'id': p.id,
 
47
 
48
  class Table(db.Model):
49
  id = db.Column(db.Integer, primary_key=True)
50
+ name = db.Column(db.String(100)) # 新增桌次名稱欄位
51
  capacity = db.Column(db.Integer, nullable=False)
52
  participants = db.relationship('Participant', backref='table', lazy=True)
53
 
 
316
  print("本地資料庫更新成功")
317
  return jsonify({'message': '設定已更新', 'sync_result': sync_result})
318
 
319
+ elif action == 'update_table_name':
320
+ table_id = data.get('table_id')
321
+ name = data.get('name', '')
322
+
323
+ if table_id is None:
324
+ return jsonify({'error': '缺少桌次 ID'}), 400
325
+
326
+ try:
327
+ table = Table.query.get(table_id)
328
+ if not table:
329
+ return jsonify({'error': '找不到指定的桌次'}), 404
330
+
331
+ table.name = name
332
+ db.session.commit()
333
+ sync_result = sync_settings_to_sheets('update_table_name', {
334
+ 'table_id': table_id,
335
+ 'name': name
336
+ })
337
+ return jsonify(sync_result), 200 if sync_result.get('success') else 500
338
+
339
+ except Exception as e:
340
+ db.session.rollback()
341
+ return jsonify({'error': str(e)}), 500
342
+
343
  else:
344
  return jsonify({'error': '無效的操作'}), 400
345
 
 
572
 
573
  result = [{
574
  'id': table.id,
575
+ 'name': table.name, # 添加桌次名稱
576
  'capacity': table.capacity,
577
  'participants': [{
578
  'id': p.id,
sync_settings.gs CHANGED
@@ -81,6 +81,25 @@ function doPost(e) {
81
  } else if (action === 'update_tables' || action === 'updateSettings') {
82
  var settings = action === 'update_tables' ? data : data.settings;
83
  return updateSettings(sheet, settings);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  }
85
 
86
  return ContentService.createTextOutput(JSON.stringify({
@@ -97,6 +116,17 @@ function doPost(e) {
97
  }
98
  }
99
 
 
 
 
 
 
 
 
 
 
 
 
100
  function backupParticipants(sheet, participants) {
101
  if (!participants) {
102
  return ContentService.createTextOutput(JSON.stringify({
 
81
  } else if (action === 'update_tables' || action === 'updateSettings') {
82
  var settings = action === 'update_tables' ? data : data.settings;
83
  return updateSettings(sheet, settings);
84
+ } else if (action === 'update_table_name') {
85
+ var tableId = data.table_id;
86
+ var name = data.name;
87
+
88
+ // 更新桌次名稱
89
+ var tablesSheet = sheet.getSheetByName('Tables');
90
+ var tableRow = findTableRow(tableId, tablesSheet);
91
+ if (tableRow > 0) {
92
+ tablesSheet.getRange(tableRow, 2).setValue(name); // 假設名稱在第二列
93
+ return ContentService.createTextOutput(JSON.stringify({
94
+ success: true,
95
+ message: '桌次名稱已更新'
96
+ })).setMimeType(ContentService.MimeType.JSON);
97
+ } else {
98
+ return ContentService.createTextOutput(JSON.stringify({
99
+ success: false,
100
+ error: '找不到指定的桌次'
101
+ })).setMimeType(ContentService.MimeType.JSON);
102
+ }
103
  }
104
 
105
  return ContentService.createTextOutput(JSON.stringify({
 
116
  }
117
  }
118
 
119
+ // 尋找桌次在表格中的行數
120
+ function findTableRow(tableId, sheet) {
121
+ var data = sheet.getDataRange().getValues();
122
+ for (var i = 0; i < data.length; i++) {
123
+ if (data[i][0] == tableId) { // 假設 ID 在第一列
124
+ return i + 1; // 轉換為 1-based 索引
125
+ }
126
+ }
127
+ return -1;
128
+ }
129
+
130
  function backupParticipants(sheet, participants) {
131
  if (!participants) {
132
  return ContentService.createTextOutput(JSON.stringify({
templates/admin.html CHANGED
@@ -56,31 +56,46 @@
56
  <div class="container mt-4">
57
  <h2>管理員後台</h2>
58
 
59
- <!-- 桌次設定 -->
60
- <div class="card mb-4">
61
- <div class="card-header">
62
- <h5 class="mb-0">桌次設定</h5>
63
- </div>
64
- <div class="card-body">
65
- <form id="tableSettingsForm">
66
- <div class="row">
67
- <div class="col-md-6">
68
- <div class="mb-3">
69
- <label for="totalTables" class="form-label">總桌數</label>
70
- <input type="number" class="form-control" id="totalTables" min="1" required>
71
- </div>
72
- </div>
73
- <div class="col-md-6">
74
- <div class="mb-3">
75
- <label for="seatsPerTable" class="form-label">每桌人數</label>
76
- <input type="number" class="form-control" id="seatsPerTable" min="1" required>
 
 
77
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  </div>
79
  </div>
80
- <button type="submit" class="btn btn-primary">更新設定</button>
81
- </form>
82
- </div>
83
- </div>
84
 
85
  <!-- 備份和恢復按鈕 -->
86
  <div class="row mb-4">
@@ -178,12 +193,88 @@
178
  const data = await response.json();
179
  document.getElementById('totalTables').value = data.total_tables;
180
  document.getElementById('seatsPerTable').value = data.seats_per_table;
 
 
 
181
  } catch (error) {
182
  console.error('Error:', error);
183
  Swal.fire('錯誤', '載入設定失敗', 'error');
184
  }
185
  }
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  // 更新桌次設定
188
  document.getElementById('tableSettingsForm').addEventListener('submit', async (e) => {
189
  e.preventDefault();
 
56
  <div class="container mt-4">
57
  <h2>管理員後台</h2>
58
 
59
+ <!-- 桌次設定 -->
60
+ <div class="card mb-4">
61
+ <div class="card-header">
62
+ <h5 class="mb-0">桌次設定</h5>
63
+ </div>
64
+ <div class="card-body">
65
+ <form id="tableSettingsForm">
66
+ <div class="row">
67
+ <div class="col-md-6">
68
+ <div class="mb-3">
69
+ <label for="totalTables" class="form-label">總桌數</label>
70
+ <input type="number" class="form-control" id="totalTables" min="1" required>
71
+ </div>
72
+ </div>
73
+ <div class="col-md-6">
74
+ <div class="mb-3">
75
+ <label for="seatsPerTable" class="form-label">每桌人數</label>
76
+ <input type="number" class="form-control" id="seatsPerTable" min="1" required>
77
+ </div>
78
+ </div>
79
  </div>
80
+ <button type="submit" class="btn btn-primary">更新設定</button>
81
+ </form>
82
+ </div>
83
+ </div>
84
+
85
+ <!-- 桌次命名設定 -->
86
+ <div class="card mb-4">
87
+ <div class="card-header d-flex justify-content-between align-items-center">
88
+ <h5 class="mb-0">桌次命名</h5>
89
+ <button type="button" class="btn btn-outline-primary btn-sm" onclick="loadTableNames()">
90
+ <i class="bi bi-arrow-clockwise"></i> 重新載入
91
+ </button>
92
+ </div>
93
+ <div class="card-body">
94
+ <div class="row" id="tableNameSettings">
95
+ <!-- 桌次命名表單將由 JavaScript 動態生成 -->
96
  </div>
97
  </div>
98
+ </div>
 
 
 
99
 
100
  <!-- 備份和恢復按鈕 -->
101
  <div class="row mb-4">
 
193
  const data = await response.json();
194
  document.getElementById('totalTables').value = data.total_tables;
195
  document.getElementById('seatsPerTable').value = data.seats_per_table;
196
+
197
+ // 載入桌次名稱設定
198
+ loadTableNames();
199
  } catch (error) {
200
  console.error('Error:', error);
201
  Swal.fire('錯誤', '載入設定失敗', 'error');
202
  }
203
  }
204
 
205
+ // 載入桌次名稱設定
206
+ async function loadTableNames() {
207
+ try {
208
+ const response = await fetch('/api/tables');
209
+ const data = await response.json();
210
+ const container = document.getElementById('tableNameSettings');
211
+ container.innerHTML = '';
212
+
213
+ data.tables.forEach(table => {
214
+ const col = document.createElement('div');
215
+ col.className = 'col-md-4 mb-3';
216
+ col.innerHTML = `
217
+ <div class="input-group">
218
+ <span class="input-group-text">第 ${table.id} 桌</span>
219
+ <input type="text" class="form-control"
220
+ id="tableName_${table.id}"
221
+ value="${table.name || ''}"
222
+ placeholder="輸入桌次名稱"
223
+ onchange="updateTableName(${table.id}, this.value)">
224
+ ${table.name ? `
225
+ <button class="btn btn-outline-secondary" type="button"
226
+ onclick="updateTableName(${table.id}, '')">
227
+ <i class="bi bi-x"></i>
228
+ </button>
229
+ ` : ''}
230
+ </div>
231
+ `;
232
+ container.appendChild(col);
233
+ });
234
+ } catch (error) {
235
+ console.error('Error:', error);
236
+ Swal.fire('錯誤', '載入桌次名稱失敗', 'error');
237
+ }
238
+ }
239
+
240
+ // 更新桌次名稱
241
+ async function updateTableName(tableId, name) {
242
+ try {
243
+ const response = await fetch('/api/admin/settings/update', {
244
+ method: 'POST',
245
+ headers: {
246
+ 'Content-Type': 'application/json'
247
+ },
248
+ body: JSON.stringify({
249
+ action: 'update_table_name',
250
+ table_id: tableId,
251
+ name: name
252
+ })
253
+ });
254
+
255
+ if (!response.ok) {
256
+ const data = await response.json();
257
+ throw new Error(data.error || '更新桌次名稱失敗');
258
+ }
259
+
260
+ // 重新載入桌次名稱
261
+ loadTableNames();
262
+
263
+ Swal.fire({
264
+ icon: 'success',
265
+ title: '成功',
266
+ text: '桌次名稱已更新',
267
+ toast: true,
268
+ position: 'top-end',
269
+ showConfirmButton: false,
270
+ timer: 3000
271
+ });
272
+ } catch (error) {
273
+ console.error('Error:', error);
274
+ Swal.fire('錯誤', error.message, 'error');
275
+ }
276
+ }
277
+
278
  // 更新桌次設定
279
  document.getElementById('tableSettingsForm').addEventListener('submit', async (e) => {
280
  e.preventDefault();
templates/index.html CHANGED
@@ -39,6 +39,12 @@
39
  z-index: 9998;
40
  display: none;
41
  }
 
 
 
 
 
 
42
  </style>
43
  </head>
44
  <body>
@@ -163,9 +169,12 @@
163
  const tableHeader = document.createElement('div');
164
  tableHeader.className = 'table-header';
165
 
166
- const tableName = document.createElement('div');
167
- tableName.className = 'table-name';
168
- tableName.textContent = table.name;
 
 
 
169
 
170
  const seatsInfo = document.createElement('div');
171
  seatsInfo.className = 'seats-info';
@@ -180,7 +189,7 @@
180
  tableHeader.appendChild(selectButton);
181
  }
182
 
183
- tableHeader.appendChild(tableName);
184
  tableHeader.appendChild(seatsInfo);
185
 
186
  const participantsList = document.createElement('div');
 
39
  z-index: 9998;
40
  display: none;
41
  }
42
+ .table-name {
43
+ color: #666;
44
+ font-size: 0.9em;
45
+ margin: -0.5rem 0 0.5rem 0;
46
+ font-style: italic;
47
+ }
48
  </style>
49
  </head>
50
  <body>
 
169
  const tableHeader = document.createElement('div');
170
  tableHeader.className = 'table-header';
171
 
172
+ const titleDiv = document.createElement('div');
173
+ titleDiv.className = 'table-title';
174
+ titleDiv.innerHTML = `
175
+ <h5 class="mb-2">第 ${table.id} 桌</h5>
176
+ ${table.name ? `<p class="table-name mb-3">${table.name}</p>` : ''}
177
+ `;
178
 
179
  const seatsInfo = document.createElement('div');
180
  seatsInfo.className = 'seats-info';
 
189
  tableHeader.appendChild(selectButton);
190
  }
191
 
192
+ tableHeader.appendChild(titleDiv);
193
  tableHeader.appendChild(seatsInfo);
194
 
195
  const participantsList = document.createElement('div');