eventinvite / templates /index.html
ichieh
修改index api
66f25db
Raw History Blame Contribute Delete
16.8 kB
<!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>