(() => { const $ = (sel) => document.querySelector(sel); const listEl = $('#list'); const detailEl = $('#detail'); const qEl = $('#q'); const btn = $('#searchBtn'); const api = { searchTickets: async (q) => { const r = await fetch(`/api/public/tickets?q=${encodeURIComponent(q || '')}`); return r.json(); }, ticketDetail: async (id) => { const r = await fetch(`/api/public/tickets/${encodeURIComponent(id)}`); return r.json(); }, popular: async () => { const r = await fetch('/api/public/popular'); return r.json(); } }; const formatTime = (value) => { if (value == null || value === '') return '---'; let ts = Number(value); if (Number.isFinite(ts)) { if (ts > 0 && ts < 1000000000000) ts *= 1000; const date = new Date(ts); if (!Number.isNaN(date.getTime())) { return date.toLocaleString('zh-CN', { hour12: false }); } } const date = new Date(value); if (!Number.isNaN(date.getTime())) { return date.toLocaleString('zh-CN', { hour12: false }); } return String(value); }; const formatTrainType = (type) => { if (!type) return '普通'; const t = type.toLowerCase(); if (t === 'local') return '普通'; if (t === 'ltd.exp' || t === 'express') return '特急'; return type; }; const getTicketId = (obj) => (obj && (obj.ticket_id || obj["车票编号"] || obj.id)) || ''; const isValidStatus = (status) => { const s = String(status || '').toLowerCase(); return s === '有效' || s === 'valid' || s === 'unused' || s === 'active' || s.includes('有效') || s.includes('未使用'); }; const formatStatusText = (status) => { const s = String(status || '').toLowerCase(); if (s === '有效' || s === 'valid' || s === 'unused' || s === 'active' || s.includes('有效') || s.includes('未使用')) return '有效'; if (s === '已使用' || s === 'used') return '已使用'; if (!s) return '未知'; if (s === 'expired') return '失效'; if (s === 'refunded') return '已退票'; return String(status); }; const getEventType = (event) => String(event.type || event["类型"] || '').toLowerCase(); const formatEventTitle = (event) => { const type = getEventType(event); const action = String(event.action || event["动作"] || '').toLowerCase(); if (type === 'sale' || type === '售票') return '售票成功'; if (type === 'entry' || action === 'entry') return '进站成功'; if (type === 'exit' || action === 'exit') return '出站成功'; if (type === 'status' || type === '状态') return '状态变更'; return event.type || event["类型"] || '状态更新'; }; const formatEventLocation = (event) => { const type = getEventType(event); const stationName = event.station_name || event["售票站"] || event["发生站"] || ''; const stationCode = event.station_code || event["站点编号"] || ''; if (type === 'sale' || type === '售票') { return stationName || '线上售票'; } if (!stationName && !stationCode) return '---'; return [stationName, stationName && stationCode ? stationCode : ''].filter(Boolean).join(' '); }; const formatEventExtra = (event) => { const type = getEventType(event); if (type === 'sale' || type === '售票') { const amount = event.amount ?? event["售票额"]; if (amount != null && amount !== '') return `票价:¥ ${amount}`; } const stationEn = event.station_en || event["站点英文"] || ''; const deviceId = event["设备编号"] || event.device_id || ''; if (stationEn && deviceId) return `${stationEn} (${deviceId})`; if (deviceId) return `设备:${deviceId}`; return stationEn; }; function renderList(items) { listEl.innerHTML = ''; if (items.length === 0) { listEl.innerHTML = '
未找到匹配结果。
正在加载详情...
未找到车票详情。
加载详情失败。
正在搜索...
搜索失败。