File size: 16,805 Bytes
2a06b2a
 
 
 
 
63a8f38
386e7a4
 
63a8f38
3dde316
7db85a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a377724
 
 
 
 
 
7db85a7
2a06b2a
 
7db85a7
 
 
 
 
 
 
 
 
 
 
 
386e7a4
63a8f38
 
 
 
 
2a06b2a
1f3ce0c
 
 
2a06b2a
1f3ce0c
 
 
 
b0a15a4
1f3ce0c
2a06b2a
1f3ce0c
b0a15a4
1f3ce0c
4307a66
1f3ce0c
b0a15a4
1f3ce0c
4307a66
1f3ce0c
b0a15a4
 
 
4307a66
1f3ce0c
 
 
4307a66
 
 
 
386e7a4
 
2a06b2a
 
 
1f3ce0c
 
386e7a4
29dd674
 
386e7a4
29dd674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386e7a4
2a06b2a
386e7a4
 
 
66f25db
 
 
 
 
386e7a4
08eba66
813fe6a
386e7a4
7672900
 
 
66f25db
813fe6a
 
 
 
 
 
9d5d9ea
 
1f3ce0c
9d5d9ea
29dd674
 
 
a377724
 
 
 
 
 
813fe6a
9d5d9ea
 
 
08eba66
29dd674
 
 
 
 
 
 
 
 
a377724
29dd674
 
9d5d9ea
 
08eba66
7672900
 
 
9d5d9ea
 
29dd674
0adb92f
29dd674
7672900
9d5d9ea
813fe6a
7672900
29dd674
9d5d9ea
 
7672900
386e7a4
7672900
 
386e7a4
66f25db
 
386e7a4
2a06b2a
 
7db85a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f3ce0c
 
 
7db85a7
4307a66
 
b0a15a4
 
 
 
 
 
386e7a4
0758969
1f3ce0c
b0a15a4
 
0012170
 
b0a15a4
 
1f3ce0c
 
 
b0a15a4
 
 
 
4307a66
0758969
4307a66
b0a15a4
 
0012170
 
b0a15a4
 
2a06b2a
 
1f3ce0c
0758969
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386e7a4
 
 
 
 
 
 
 
 
1f3ce0c
 
 
386e7a4
 
 
1f3ce0c
 
0758969
 
 
 
b0a15a4
 
0012170
eb8b128
b0a15a4
 
 
1f3ce0c
 
 
 
 
b0a15a4
 
1f3ce0c
 
b0a15a4
 
 
1f3ce0c
b0a15a4
 
 
eb8b128
b0a15a4
 
0012170
 
2a06b2a
386e7a4
0758969
 
 
1f3ce0c
b0a15a4
 
 
 
 
 
0012170
 
0758969
 
 
386e7a4
4307a66
2a06b2a
 
 
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
<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>蛇來蛇去 - 辦桌春酒大亂鬥暨天公生慶生祝願</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11.3.0/dist/sweetalert2.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
    <style>
        #restartContainer {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 9999;
            text-align: center;
            background: rgba(0, 0, 0, 0.9);
            padding: 2rem;
            border-radius: 10px;
            color: white;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
        }
        #restartContainer h3 {
            margin-bottom: 1rem;
        }
        #restartContainer p {
            margin-bottom: 1.5rem;
        }
        .sleeping-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 9998;
            display: none;
        }
        .table-name {
            color: #666;
            font-size: 0.9em;
            margin: -0.5rem 0 0.5rem 0;
            font-style: italic;
        }
    </style>
</head>
<body>
    <!-- 重啟系統對話框 -->
    <div id="restartContainer">
        <h3><i class="bi bi-moon-stars"></i> 系統休眠中</h3>
        <p>由於一段時間沒有活動,系統進入了休眠狀態。<br>請點擊下方按鈕重新啟動。</p>
        <button onclick="restartSpace()" class="btn btn-primary">
            <i class="bi bi-arrow-clockwise"></i> 重新啟動系統
        </button>
    </div>
    
    <!-- 遮罩層 -->
    <div id="sleepingOverlay" class="sleeping-overlay"></div>

    <div class="container">
        <h1 class="text-center my-4">蛇來蛇去 - 辦桌春酒大亂鬥暨天公生慶生祝願 
            <a href="https://fb.me/1LHAts3TlfxyvhM" target="_blank" class="btn btn-outline-primary btn-sm">
                <i class="bi bi-facebook"></i> 活動詳情
            </a>
        </h1>
        
        <div class="main-container">
            <div class="tables-section" id="tables">
                <!-- 桌子將由 JavaScript 動態生成 -->
            </div>
            
            <div class="registration-section">
                <h4 class="mb-3">登記資料</h4>
                <div class="mb-3">
                    <label for="nickname" class="form-label">暱稱 <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" id="nickname" required>
                </div>
                <div class="mb-3">
                    <label for="email" class="form-label">Email <span class="text-danger">*</span></label>
                    <input type="email" class="form-control" id="email" required>
                </div>
                <div class="mb-3">
                    <label for="fb_link" class="form-label">FB個人連結 <small class="text-muted">(選填)</small></label>
                    <input type="url" class="form-control" id="fb_link">
                </div>
                <div class="mb-3">
                    <label for="introduction" class="form-label">自我介紹 <small class="text-muted">(選填,300字元內)</small></label>
                    <textarea class="form-control" id="introduction" rows="3" maxlength="300"></textarea>
                    <div class="form-text text-end"><span id="charCount">0</span>/300</div>
                </div>
                <button class="btn btn-primary w-100" id="confirmBtn" disabled onclick="submitRegistration()">
                    確認選位
                </button>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.3.0/dist/sweetalert2.all.min.js"></script>
    <script>
        let selectedTableId = null;

        function selectTable(tableId) {
            if (!tableId) return;
            
            selectedTableId = tableId;
            document.getElementById('confirmBtn').disabled = false;
            
            // 更新所有按鈕的狀態
            document.querySelectorAll('.select-seat-btn').forEach(btn => {
                if (btn.getAttribute('data-table-id') === tableId.toString()) {
                    btn.classList.remove('btn-outline-primary');
                    btn.classList.add('btn-primary');
                } else {
                    btn.classList.remove('btn-primary');
                    btn.classList.add('btn-outline-primary');
                }
            });
        }

        function showParticipantInfo(participant) {
            Swal.fire({
                title: participant.nickname,
                html: `
                    ${participant.fb_link ? `<p><a href="${participant.fb_link}" target="_blank" class="btn btn-outline-primary btn-sm">
                        <i class="bi bi-facebook"></i> Facebook
                    </a></p>` : ''}
                    ${participant.introduction ? `
                    <div class="text-start">
                        <h6>自我介紹:</h6>
                        <p class="text-muted">${participant.introduction}</p>
                    </div>
                    ` : ''}
                    ${participant.is_table_leader ? '<div class="badge bg-success">桌長</div>' : ''}
                `,
                showConfirmButton: false,
                showCloseButton: true,
                customClass: {
                    container: 'participant-modal'
                }
            });
        }

        async function loadTables() {
            try {
                const response = await fetch('/api/tables');
                const data = await response.json();
                
                if (!data.tables) {
                    throw new Error('無效的桌次資料');
                }
                
                const tablesContainer = document.getElementById('tables');
                tablesContainer.innerHTML = '';
                
                // 使用 DocumentFragment 來優化 DOM 操作
                const fragment = document.createDocumentFragment();
                
                data.tables.forEach(table => {
                    const tableElement = document.createElement('div');
                    tableElement.className = 'table-container';
                    
                    const participantsCount = table.participants.length;
                    const isFull = participantsCount >= table.capacity;
                    
                    const circle = document.createElement('div');
                    circle.className = `table-circle${isFull ? ' full' : ''}`;
                    circle.setAttribute('data-table-id', table.id);
                    
                    const tableHeader = document.createElement('div');
                    tableHeader.className = 'table-header';
                    
                    const titleDiv = document.createElement('div');
                    titleDiv.className = 'table-title';
                    titleDiv.innerHTML = `
                        <h5 class="mb-2">第 ${table.id} 桌</h5>
                        ${table.name ? `<p class="table-name mb-3">${table.name}</p>` : ''}
                    `;
                    
                    const seatsInfo = document.createElement('div');
                    seatsInfo.className = 'seats-info';
                    seatsInfo.textContent = `${participantsCount}/${table.capacity} 個座位`;
                    
                    if (!isFull) {
                        const selectButton = document.createElement('button');
                        selectButton.className = `btn ${selectedTableId === table.id ? 'btn-primary' : 'btn-outline-primary'} btn-sm select-seat-btn`;
                        selectButton.textContent = '選擇此桌';
                        selectButton.setAttribute('data-table-id', table.id);
                        selectButton.onclick = () => selectTable(table.id);
                        tableHeader.appendChild(selectButton);
                    }
                    
                    tableHeader.appendChild(titleDiv);
                    tableHeader.appendChild(seatsInfo);
                    
                    const participantsList = document.createElement('div');
                    participantsList.className = 'participants-list';
                    
                    // 使用 DocumentFragment 來優化參與者列表的 DOM 操作
                    const participantsFragment = document.createDocumentFragment();
                    
                    table.participants.forEach(participant => {
                        const participantDiv = document.createElement('div');
                        participantDiv.className = `participant-item${participant.is_table_leader ? ' table-leader' : ''}`;
                        participantDiv.textContent = participant.is_table_leader ? `桌長:${participant.nickname}` : participant.nickname;
                        participantDiv.onclick = () => showParticipantInfo(participant);
                        participantsFragment.appendChild(participantDiv);
                    });
                    
                    participantsList.appendChild(participantsFragment);
                    circle.appendChild(tableHeader);
                    circle.appendChild(participantsList);
                    tableElement.appendChild(circle);
                    fragment.appendChild(tableElement);
                });
                
                tablesContainer.appendChild(fragment);
            } catch (error) {
                console.error('Error:', error);
                Swal.fire('錯誤', error.message || '載入桌次失敗', 'error');
            }
        }

        // 檢查系統狀態的函數
        async function checkSystemStatus() {
            try {
                const response = await fetch('/api/tables');
                if (!response.ok) {
                    showRestartButton();
                } else {
                    hideRestartButton();
                }
            } catch (error) {
                console.error('Error checking system status:', error);
                showRestartButton();
            }
        }

        // 顯示重啟按鈕
        function showRestartButton() {
            document.getElementById('restartContainer').style.display = 'block';
            document.getElementById('sleepingOverlay').style.display = 'block';
        }

        // 隱藏重啟按鈕
        function hideRestartButton() {
            document.getElementById('restartContainer').style.display = 'none';
            document.getElementById('sleepingOverlay').style.display = 'none';
        }

        // 重啟系統
        async function restartSpace() {
            const button = document.querySelector('#restartContainer button');
            const originalText = button.innerHTML;
            button.disabled = true;
            button.innerHTML = '<i class="bi bi-arrow-clockwise"></i> 正在重新啟動...';
            
            // 重新載入頁面
            window.location.reload();
        }

        // 定期檢查系統狀態
        setInterval(checkSystemStatus, 30000); // 每30秒檢查一次
        
        // 初始化頁面
        document.addEventListener('DOMContentLoaded', () => {
            loadTables();
            checkSystemStatus();
        });

        // 字數計數器
        document.getElementById('introduction').addEventListener('input', function() {
            const currentLength = this.value.length;
            document.getElementById('charCount').textContent = currentLength;
        });

        async function submitRegistration() {
            // 檢查是否已選擇座位
            if (!selectedTableId) {
                Swal.fire({
                    icon: 'error',
                    title: '無法報名',
                    text: '請先選擇座位',
                    confirmButtonText: '確定'
                });
                return;
            }
            
            const nickname = document.getElementById('nickname').value.trim();
            const email = document.getElementById('email').value.trim();
            const fb_link = document.getElementById('fb_link').value.trim();
            const introduction = document.getElementById('introduction').value.trim();
            
            // 檢查必填欄位
            if (!nickname || !email) {
                Swal.fire({
                    icon: 'error',
                    title: '無法報名',
                    text: '請填寫必要欄位(暱稱和Email)',
                    confirmButtonText: '確定'
                });
                return;
            }
            
            // 禁用提交按鈕,避免重複送出
            const confirmBtn = document.getElementById('confirmBtn');
            confirmBtn.disabled = true;
            
            // 顯示等待畫面
            const loadingSwal = Swal.fire({
                title: '處理中...',
                text: '正在為您報名,請稍候',
                allowOutsideClick: false,
                allowEscapeKey: false,
                allowEnterKey: false,
                showConfirmButton: false,
                didOpen: () => {
                    Swal.showLoading();
                }
            });
            
            try {
                const response = await fetch('/api/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        nickname,
                        email,
                        fb_link,
                        introduction,
                        table_id: selectedTableId
                    })
                });
                
                const result = await response.json();
                
                // 關閉等待畫面
                await loadingSwal.close();
                
                if (response.ok) {
                    await Swal.fire({
                        icon: 'success',
                        title: '報名成功',
                        text: result.message || '您已成功報名座位',
                        confirmButtonText: '確定'
                    });
                    
                    // 清除表單
                    document.getElementById('nickname').value = '';
                    document.getElementById('email').value = '';
                    document.getElementById('fb_link').value = '';
                    document.getElementById('introduction').value = '';
                    document.getElementById('charCount').textContent = '0';
                    
                    // 清除選擇的桌號
                    selectedTableId = null;
                    
                    // 重新載入桌次資料
                    await loadTables();
                } else {
                    Swal.fire({
                        icon: 'error',
                        title: '報名失敗',
                        text: result.error || '該座位可能已被其他人選走,請重新選擇',
                        confirmButtonText: '確定'
                    });
                    // 重新載入桌次資料以顯示最新狀態
                    await loadTables();
                }
            } catch (error) {
                // 關閉等待畫面
                await loadingSwal.close();
                
                console.error('Error:', error);
                Swal.fire({
                    icon: 'error',
                    title: '報名失敗',
                    text: '系統發生錯誤,請稍後再試',
                    confirmButtonText: '確定'
                });
                // 重新載入桌次資料以顯示最新狀態
                await loadTables();
            } finally {
                // 無論成功或失敗,都重新啟用提交按鈕
                confirmBtn.disabled = false;
            }
        }
    </script>
</body>
</html>