ichieh commited on
Commit
7a04ee7
·
1 Parent(s): b54e9ca

Add table sorting and improve admin UI

Browse files
.history/sync_settings_20250117183153.gs ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function doPost(e) {
2
+ var sheet = SpreadsheetApp.getActiveSpreadsheet();
3
+ var data = JSON.parse(e.postData.contents);
4
+ var action = data.action;
5
+
6
+ if (action === 'backupParticipants') {
7
+ // 備份參與者資料
8
+ var participantsSheet = sheet.getSheetByName('Participants') || sheet.insertSheet('Participants');
9
+ participantsSheet.clear();
10
+ participantsSheet.getRange('A1:K1').setValues([[
11
+ 'ID', '暱稱', 'Email', 'FB連結', '自我介紹', '桌次', '座位號', '是否為桌長', '報名時間', '付款狀態', '備註'
12
+ ]]);
13
+
14
+ var participants = data.participants;
15
+ if (participants && participants.length > 0) {
16
+ var values = participants.map(function(p) {
17
+ return [
18
+ p.id,
19
+ p.nickname,
20
+ p.email,
21
+ p.fb_link || '',
22
+ p.introduction || '',
23
+ p.table_id,
24
+ p.seat_number,
25
+ p.is_table_leader || false,
26
+ p.created_at,
27
+ p.has_paid || false,
28
+ p.note || ''
29
+ ];
30
+ });
31
+ participantsSheet.getRange(2, 1, values.length, 11).setValues(values);
32
+ }
33
+
34
+ return ContentService.createTextOutput(JSON.stringify({success: true}))
35
+ .setMimeType(ContentService.MimeType.JSON);
36
+ }
37
+
38
+ return ContentService.createTextOutput(JSON.stringify({error: 'Invalid action'}))
39
+ .setMimeType(ContentService.MimeType.JSON);
40
+ }
41
+
42
+ function doGet() {
43
+ var sheet = SpreadsheetApp.getActiveSpreadsheet();
44
+ var participantsSheet = sheet.getSheetByName('Participants');
45
+
46
+ if (!participantsSheet) {
47
+ return ContentService.createTextOutput(JSON.stringify({
48
+ success: false,
49
+ error: '找不到參與者資料表'
50
+ })).setMimeType(ContentService.MimeType.JSON);
51
+ }
52
+
53
+ var range = participantsSheet.getDataRange();
54
+ var values = range.getValues();
55
+ var headers = values[0];
56
+ var participants = [];
57
+
58
+ // 獲取欄位索引
59
+ var idIndex = headers.indexOf('ID');
60
+ var nicknameIndex = headers.indexOf('暱稱');
61
+ var emailIndex = headers.indexOf('Email');
62
+ var fbLinkIndex = headers.indexOf('FB連結');
63
+ var introIndex = headers.indexOf('自我介紹');
64
+ var tableIndex = headers.indexOf('桌次');
65
+ var seatIndex = headers.indexOf('座位號');
66
+ var leaderIndex = headers.indexOf('是否為桌長');
67
+ var timeIndex = headers.indexOf('報名時間');
68
+ var paidIndex = headers.indexOf('付款狀態');
69
+ var noteIndex = headers.indexOf('備註');
70
+
71
+ // 跳過標題列
72
+ for (var i = 1; i < values.length; i++) {
73
+ var row = values[i];
74
+ if (!row[nicknameIndex]) continue; // 跳過空行
75
+
76
+ participants.push({
77
+ id: row[idIndex],
78
+ nickname: row[nicknameIndex],
79
+ email: row[emailIndex],
80
+ fb_link: row[fbLinkIndex] || '',
81
+ introduction: row[introIndex] || '',
82
+ table_id: parseInt(row[tableIndex]) || 0,
83
+ seat_number: parseInt(row[seatIndex]) || 0,
84
+ is_table_leader: row[leaderIndex] === true || row[leaderIndex] === 'TRUE',
85
+ created_at: row[timeIndex],
86
+ has_paid: paidIndex !== -1 ? (row[paidIndex] === true || row[paidIndex] === 'TRUE') : false,
87
+ note: noteIndex !== -1 ? (row[noteIndex] || '') : ''
88
+ });
89
+ }
90
+
91
+ return ContentService.createTextOutput(JSON.stringify({
92
+ success: true,
93
+ participants: participants
94
+ })).setMimeType(ContentService.MimeType.JSON);
95
+ }
.history/sync_settings_20250117190528.gs ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function doPost(e) {
2
+ var sheet = SpreadsheetApp.getActiveSpreadsheet();
3
+ var data = JSON.parse(e.postData.contents);
4
+ var action = data.action;
5
+
6
+ if (action === 'backupParticipants') {
7
+ // 備份參與者資料
8
+ var participantsSheet = sheet.getSheetByName('Participants') || sheet.insertSheet('Participants');
9
+ participantsSheet.clear();
10
+ participantsSheet.getRange('A1:K1').setValues([[
11
+ 'ID', '暱稱', 'Email', 'FB連結', '自我介紹', '桌次', '座位號', '是否為桌長', '報名時間', '付款狀態', '備註'
12
+ ]]);
13
+
14
+ var participants = data.participants;
15
+ if (participants && participants.length > 0) {
16
+ var values = participants.map(function(p) {
17
+ return [
18
+ p.id,
19
+ p.nickname,
20
+ p.email,
21
+ p.fb_link || '',
22
+ p.introduction || '',
23
+ p.table_id,
24
+ p.seat_number,
25
+ p.is_table_leader || false,
26
+ p.created_at,
27
+ p.has_paid || false,
28
+ p.note || ''
29
+ ];
30
+ });
31
+ participantsSheet.getRange(2, 1, values.length, 11).setValues(values);
32
+ }
33
+
34
+ return ContentService.createTextOutput(JSON.stringify({success: true}))
35
+ .setMimeType(ContentService.MimeType.JSON);
36
+ }
37
+
38
+ return ContentService.createTextOutput(JSON.stringify({error: 'Invalid action'}))
39
+ .setMimeType(ContentService.MimeType.JSON);
40
+ }
41
+
42
+ function doGet() {
43
+ var sheet = SpreadsheetApp.getActiveSpreadsheet();
44
+ var participantsSheet = sheet.getSheetByName('Participants');
45
+
46
+ if (!participantsSheet) {
47
+ return ContentService.createTextOutput(JSON.stringify({
48
+ success: false,
49
+ error: '找不到參與者資料表'
50
+ })).setMimeType(ContentService.MimeType.JSON);
51
+ }
52
+
53
+ var range = participantsSheet.getDataRange();
54
+ var values = range.getValues();
55
+ var headers = values[0];
56
+ var participants = [];
57
+
58
+ // 獲取欄位索引
59
+ var idIndex = headers.indexOf('ID');
60
+ var nicknameIndex = headers.indexOf('暱稱');
61
+ var emailIndex = headers.indexOf('Email');
62
+ var fbLinkIndex = headers.indexOf('FB連結');
63
+ var introIndex = headers.indexOf('自我介紹');
64
+ var tableIndex = headers.indexOf('桌次');
65
+ var seatIndex = headers.indexOf('座位號');
66
+ var leaderIndex = headers.indexOf('是否為桌長');
67
+ var timeIndex = headers.indexOf('報名時間');
68
+ var paidIndex = headers.indexOf('付款狀態');
69
+ var noteIndex = headers.indexOf('備註');
70
+
71
+ // 跳過標題列
72
+ for (var i = 1; i < values.length; i++) {
73
+ var row = values[i];
74
+ if (!row[nicknameIndex]) continue; // 跳過空行
75
+
76
+ participants.push({
77
+ id: row[idIndex],
78
+ nickname: row[nicknameIndex],
79
+ email: row[emailIndex],
80
+ fb_link: row[fbLinkIndex] || '',
81
+ introduction: row[introIndex] || '',
82
+ table_id: parseInt(row[tableIndex]) || 0,
83
+ seat_number: parseInt(row[seatIndex]) || 0,
84
+ is_table_leader: row[leaderIndex] === true || row[leaderIndex] === 'TRUE',
85
+ created_at: row[timeIndex],
86
+ has_paid: paidIndex !== -1 ? (row[paidIndex] === true || row[paidIndex] === 'TRUE') : false,
87
+ note: noteIndex !== -1 ? (row[noteIndex] || '') : ''
88
+ });
89
+ }
90
+
91
+ return ContentService.createTextOutput(JSON.stringify({
92
+ success: true,
93
+ participants: participants
94
+ })).setMimeType(ContentService.MimeType.JSON);
95
+ }
templates/admin.html CHANGED
@@ -177,7 +177,22 @@
177
  try {
178
  const response = await fetch('/api/admin/participants');
179
  const data = await response.json();
180
- participants = data.participants;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  tables = data.tables;
182
  renderParticipants();
183
  } catch (error) {
@@ -188,65 +203,79 @@
188
 
189
  // 渲染參與者列表
190
  function renderParticipants() {
191
- const tbody = document.getElementById('participantsTable');
192
  tbody.innerHTML = '';
193
 
194
- participants.forEach(p => {
195
- const table = tables.find(t => t.id === p.table_id);
196
- const row = document.createElement('tr');
197
 
198
- // 截短的自我介紹
199
- const shortIntro = p.introduction ?
200
- (p.introduction.length > 10 ?
201
- `${p.introduction.substring(0, 10)}... <a href="#" onclick="viewFullIntro('${p.introduction}')">查看更多</a>` :
202
- p.introduction) :
203
- '';
204
-
205
- row.innerHTML = `
206
- <td>${table ? table.name : '未分配'}</td>
207
- <td>${p.seat_number || '-'}</td>
208
- <td>${p.nickname}</td>
209
- <td>${p.email}</td>
210
- <td>${p.fb_link ? `<a href="${p.fb_link}" target="_blank">FB連結</a>` : '-'}</td>
211
- <td>${shortIntro || '-'}</td>
212
  <td>
213
- <button
214
- class="btn btn-sm ${p.has_paid ? 'btn-success' : 'btn-danger'}"
215
- onclick="togglePayment(${p.id})"
216
- >
217
- ${p.has_paid ? '已付款' : '未付款'}
 
 
 
 
 
 
 
 
218
  </button>
219
  </td>
220
  <td>
221
- <div class="d-flex align-items-center">
222
- <span onclick="showNote('${p.note || ''}')" style="cursor: pointer; ${p.note ? 'color: #007bff;' : ''}">
223
- ${p.note ? (p.note.length > 10 ? p.note.substring(0, 10) + '...' : p.note) : '-'}
224
- </span>
225
- <i class="fas fa-edit ms-2" style="cursor: pointer; color: #6c757d;"
226
- onclick="editNote('${p.id}', '${p.note || ''}')"></i>
227
- </div>
228
  </td>
229
  <td>
230
- ${p.is_table_leader ?
231
- '<span class="badge bg-success">桌長</span>' :
232
- '<span class="badge bg-secondary">參與者</span>'}
233
  </td>
234
- <td>${p.created_at}</td>
235
  <td>
236
- <button class="btn btn-sm btn-primary mb-1" onclick="showMoveModal(${p.id})">移動座位</button>
237
- <button class="btn btn-sm ${p.is_table_leader ? 'btn-warning' : 'btn-success'} mb-1"
238
- onclick="toggleLeader(${p.id})">
239
- ${p.is_table_leader ? '取消桌長' : '設為桌長'}
240
- </button>
241
- <button class="btn btn-sm btn-danger mb-1" onclick="deleteParticipant(${p.id})">刪除</button>
 
 
 
 
 
 
 
 
 
 
242
  </td>
243
  `;
244
- tbody.appendChild(row);
245
  });
246
  }
247
 
248
  // 顯示移動座位的 Modal
249
- function showMoveModal(participantId) {
250
  document.getElementById('participantId').value = participantId;
251
  const select = document.getElementById('newTableSelect');
252
  select.innerHTML = '';
@@ -317,7 +346,7 @@
317
  }
318
 
319
  // 查看完整自我介紹
320
- function viewFullIntro(intro) {
321
  document.getElementById('fullIntroText').textContent = intro;
322
  new bootstrap.Modal(document.getElementById('viewIntroModal')).show();
323
  }
 
177
  try {
178
  const response = await fetch('/api/admin/participants');
179
  const data = await response.json();
180
+
181
+ // 依照桌次和座位號排序
182
+ data.sort((a, b) => {
183
+ // 先比較桌次
184
+ const tableA = parseInt(a.table_id) || 0;
185
+ const tableB = parseInt(b.table_id) || 0;
186
+ if (tableA !== tableB) {
187
+ return tableA - tableB;
188
+ }
189
+ // 如果桌次相同,再比較座位號
190
+ const seatA = parseInt(a.seat_number) || 0;
191
+ const seatB = parseInt(b.seat_number) || 0;
192
+ return seatA - seatB;
193
+ });
194
+
195
+ participants = data;
196
  tables = data.tables;
197
  renderParticipants();
198
  } catch (error) {
 
203
 
204
  // 渲染參與者列表
205
  function renderParticipants() {
206
+ const tbody = document.querySelector('#participantsTable tbody');
207
  tbody.innerHTML = '';
208
 
209
+ participants.forEach(participant => {
210
+ const tr = document.createElement('tr');
211
+ tr.className = participant.is_table_leader ? 'table-leader' : '';
212
 
213
+ // 找到對應的桌子
214
+ const table = tables.find(t => t.id === participant.table_id);
215
+ const tableName = table ? table.name : '未分配';
216
+
217
+ tr.innerHTML = `
218
+ <td>${tableName}</td>
219
+ <td>${participant.seat_number || '-'}</td>
220
+ <td>${participant.nickname}</td>
221
+ <td>${participant.email}</td>
222
+ <td>${participant.fb_link ? `<a href="${participant.fb_link}" target="_blank">FB連結</a>` : '-'}</td>
 
 
 
 
223
  <td>
224
+ ${participant.introduction ?
225
+ `${participant.introduction.length > 10 ?
226
+ participant.introduction.substring(0, 10) + '...' :
227
+ participant.introduction}
228
+ <button class="btn btn-link btn-sm" onclick="showFullIntroduction('${participant.introduction.replace(/'/g, "\\'")}')">
229
+ 查看更多
230
+ </button>`
231
+ : '-'}
232
+ </td>
233
+ <td>
234
+ <button class="btn ${participant.has_paid ? 'btn-success' : 'btn-danger'} btn-sm"
235
+ onclick="togglePayment(${participant.id})">
236
+ ${participant.has_paid ? '已付款' : '未付款'}
237
  </button>
238
  </td>
239
  <td>
240
+ ${participant.note ?
241
+ `<span class="note-preview" onclick="editNote(${participant.id}, '${participant.note.replace(/'/g, "\\'")}')">
242
+ ${participant.note.length > 10 ? participant.note.substring(0, 10) + '...' : participant.note}
243
+ </span>`
244
+ : `<button class="btn btn-outline-secondary btn-sm" onclick="editNote(${participant.id}, '')">
245
+ 新增備註
246
+ </button>`}
247
  </td>
248
  <td>
249
+ <span class="${participant.is_table_leader ? 'badge bg-success' : 'badge bg-secondary'}">
250
+ ${participant.is_table_leader ? '桌長' : '參與者'}
251
+ </span>
252
  </td>
253
+ <td>${new Date(participant.created_at).toLocaleString('zh-TW', { timeZone: 'Asia/Taipei' })}</td>
254
  <td>
255
+ <div class="btn-group" role="group">
256
+ <button class="btn btn-primary btn-sm" onclick="showMoveDialog(${participant.id})">
257
+ 移動座位
258
+ </button>
259
+ ${participant.is_table_leader ?
260
+ `<button class="btn btn-warning btn-sm" onclick="toggleLeader(${participant.id})">
261
+ 取消桌長
262
+ </button>` :
263
+ `<button class="btn btn-success btn-sm" onclick="toggleLeader(${participant.id})">
264
+ 設為桌長
265
+ </button>`
266
+ }
267
+ <button class="btn btn-danger btn-sm" onclick="deleteParticipant(${participant.id})">
268
+ 刪除
269
+ </button>
270
+ </div>
271
  </td>
272
  `;
273
+ tbody.appendChild(tr);
274
  });
275
  }
276
 
277
  // 顯示移動座位的 Modal
278
+ function showMoveDialog(participantId) {
279
  document.getElementById('participantId').value = participantId;
280
  const select = document.getElementById('newTableSelect');
281
  select.innerHTML = '';
 
346
  }
347
 
348
  // 查看完整自我介紹
349
+ function showFullIntroduction(intro) {
350
  document.getElementById('fullIntroText').textContent = intro;
351
  new bootstrap.Modal(document.getElementById('viewIntroModal')).show();
352
  }