/* ============================================================ checkSchedule.init.js — หน้า #/check/<รหัสสาย> ------------------------------------------------------------ ดึงข้อมูลจริง: resource/data/routes.json ชื่อสาย / ตัวเลือก / station_ids / province_ids resource/data/stations.json ชื่อจุดจอด + จังหวัด + รหัสป้าย resource/data/schedules.json เวลาออกรถ (แยกตามจุดจอด/ทิศทาง) + หมายเหตุ การไฮไลต์ (ตามเวลาบนเครื่องผู้ใช้): - ไฮไลต์ "เที่ยวถัดไป" = เที่ยวแรกที่เวลายังไม่ผ่าน (>= เวลาปัจจุบัน) - เลยเที่ยวสุดท้ายของวันแล้ว → ไม่ไฮไลต์เลย - หลังเที่ยงคืน เวลาปัจจุบันน้อยกว่าเที่ยวแรก → เที่ยวแรกของวันถูกไฮไลต์เอง - ตรวจซ้ำทุก 30 วินาที (และตอนสลับสาย/กลับเข้าแท็บ) ============================================================ */ (function () { 'use strict'; var DATA = '../resource/data/'; var FALLBACK_ID = '101'; var REFRESH_MS = 30000; var TONES = { origin: 'tone-purple', transfer: 'tone-sky', pass: 'tone-mint', destination: 'tone-pink' }; /* นโยบายการแสดงจุดจอดบนหน้าเช็กเวลา false = แสดงทุกจุดใน routes.json → station_ids (จุดที่ยังไม่มีตารางจะขึ้นการ์ด "ยังไม่มีข้อมูลตารางเวลา") ← ค่าเริ่มต้น: ตรงกับดีไซน์เดิม (หน้าเดิมโชว์จุดที่ยังไม่มีตารางพร้อมข้อความนี้) true = แสดงเฉพาะจุดที่มี record ใน schedules.json (กระดานเวลาล้วน ๆ) */ var ONLY_STOPS_WITH_SCHEDULE = false; var sections = []; /* [{ chips:[], times:[], station }] */ var netRoute = null; /* routes.json ของสายที่แสดงอยู่ */ var timer = null; function $(id) { return document.getElementById(id); } function t(k, v) { return window.I18N ? I18N.t(k, v) : k; } function esc(s) { return String(s == null ? '' : s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, '''); } function fetchJSON(u) { return fetch(u, { cache: 'no-store' }).then(function (r) { return r.ok ? r.json() : null; }).catch(function () { return null; }); } function toMin(hhmm) { var a = String(hhmm).split(':'); return (parseInt(a[0], 10) || 0) * 60 + (parseInt(a[1], 10) || 0); } function nowMin() { var d = new Date(); return d.getHours() * 60 + d.getMinutes(); } function hhmm(min) { var h = Math.floor(min / 60), m = min % 60; return (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m; } function setStat(id, s) { var el = $(id); if (!el) return; var v = (s == null ? '' : String(s)).trim(); el.textContent = v || '–'; } /* ---------- จำแนกประเภทจุดจอดจากข้อมูล + ลำดับในเส้นทาง ---------- */ function kindOf(st, i, n) { if (i === 0) return { key: 'route.origin', icon: 'ic-map-pin', tone: TONES.origin }; if (i === n - 1) return { key: 'route.destination', icon: 'ic-map-pin', tone: TONES.destination }; if (st && st.station_type === 'main_terminal') return { key: 'route.transfer', icon: 'ic-refresh-cw', tone: TONES.transfer }; return { key: 'route.pass', icon: 'ic-map-pin', tone: TONES.pass }; } /* ---------- สร้างการ์ดจุดจอด ---------- */ function renderList(listEl, route, groups) { listEl.innerHTML = ''; sections = []; groups.forEach(function (g, i) { var kind = kindOf(g.st, i, groups.length); var li = document.createElement('li'); var det = document.createElement('details'); det.className = 'mu-stop-card'; det.open = true; var sum = document.createElement('summary'); sum.innerHTML = '' + ' ' + esc(t(kind.key)) + '' + '' + esc(g.name) + '' + esc(g.city) + '' + '\u2304' + ''; det.appendChild(sum); var body = document.createElement('div'); body.className = 'mu-stop-body'; if (!g.recs.length) { var none = document.createElement('p'); none.className = 'mu-stop-sub'; none.textContent = t('route.empty'); body.appendChild(none); } g.recs.forEach(function (r) { var times = (r.departure_times || []).slice(); /* ไม่มีเวลาเลย (เช่น สาย 999: departure_times = []) → บอกว่าไม่มีข้อมูล ไม่ต้องมีบรรทัดลอย */ if (!times.length) { var empty = document.createElement('p'); empty.className = 'mu-stop-sub'; empty.textContent = t('route.empty'); body.appendChild(empty); return; } var sub = document.createElement('p'); sub.className = 'mu-stop-sub'; sub.innerHTML = r.direction_note ? esc(r.direction_note) + ' ' + esc(t('check.direction')) + '' : esc(t('check.atStop')) + ' ' + esc(g.name); body.appendChild(sub); var grid = document.createElement('div'); grid.className = 'mu-time-grid'; var chips = []; times.forEach(function (tm) { var c = document.createElement('div'); c.className = 'mu-time-chip'; c.textContent = tm; grid.appendChild(c); chips.push(c); }); body.appendChild(grid); sections.push({ chips: chips, times: times, station: g.name }); if (r.schedule_note) { var note = document.createElement('p'); note.className = 'mu-schedule-note'; note.innerHTML = ' ' + esc(r.schedule_note); body.appendChild(note); } }); if (g.code) { var act = document.createElement('div'); act.className = 'mu-inline-actions'; var a = document.createElement('a'); a.className = 'mu-btn ghost'; a.href = '#/stops/' + encodeURIComponent(g.code) + '?route=' + encodeURIComponent(route.route_id); a.textContent = t('check.toStopDetail'); act.appendChild(a); body.appendChild(act); } det.appendChild(body); li.appendChild(det); listEl.appendChild(li); }); } /* ---------- ไฮไลต์เที่ยวถัดไป + บรรทัดเวลาปัจจุบัน ---------- */ function refreshHighlights() { var nowm = nowMin(); var next = null; /* เที่ยวถัดไปที่เร็วที่สุดในทุกจุดจอด */ sections.forEach(function (sec) { sec.chips.forEach(function (c) { c.classList.remove('next'); }); for (var i = 0; i < sec.times.length; i++) { var m = toMin(sec.times[i]); if (m >= nowm) { /* เที่ยวแรกที่ยังไม่ผ่าน */ sec.chips[i].classList.add('next'); if (next === null || m < next.m) next = { m: m, time: sec.times[i], station: sec.station }; break; } } }); setStat('check-stat-next', next ? next.time : ''); var line = $('check-now-line'); if (!line) return; var clock = ' '; var body = esc(t('check.now')) + ' ' + hhmm(nowm) + ''; if (next) body += ' · ' + esc(t('check.nextAt')) + ' ' + esc(next.time) + ' (' + esc(next.station) + ')'; else if (sections.length) body += ' · ' + esc(t('check.noMoreToday')) + ''; line.innerHTML = clock + body; } /* ---------- โหลด + แสดงผล ---------- */ function init() { var params = (window.__route && window.__route.params) || {}; var query = (window.__route && window.__route.query) || {}; var want = String(params.routeId || query.route || ''); Promise.all([ fetchJSON(DATA + 'routes.json'), fetchJSON(DATA + 'stations.json'), fetchJSON(DATA + 'schedules.json') ]).then(function (res) { var routes = (res[0] || {}).routes || []; var sts = (res[1] || {}).stations || []; var recs = (res[2] || {}).schedules || res[2] || []; if (!routes.length) return; var route = routes.filter(function (r) { return String(r.route_id) === want; })[0] || routes.filter(function (r) { return String(r.route_id) === FALLBACK_ID; })[0] || routes[0]; var rid = String(route.route_id); netRoute = route; var byId = {}; sts.forEach(function (s) { byId[s.station_id] = s; }); /* ตัวเลือกสายรถ (จาก routes.json) */ var sel = $('check-route-select'); if (sel) { sel.innerHTML = ''; routes.forEach(function (r) { var o = document.createElement('option'); o.value = r.route_id; o.textContent = 'สาย ' + r.route_id + (r.route_name_th ? ' - ' + r.route_name_th : ''); sel.appendChild(o); }); sel.value = rid; sel.addEventListener('change', function () { window.location.hash = '#/check/' + encodeURIComponent(sel.value); }); } /* Hero + คำอธิบายโซน */ var hero = $('check-hero-route'); if (hero) hero.textContent = 'สาย ' + rid + (route.route_name_th ? ' · ' + route.route_name_th : ''); /* จัดกลุ่มตามลำดับจุดจอดของสาย แล้วต่อด้วยจุดที่เหลือ (ถ้ามีข้อมูล) */ var order = (route.station_ids || []).slice(); recs.forEach(function (r) { if (String(r.route_id) === rid && order.indexOf(r.station_id) === -1) order.push(r.station_id); }); var groups = order.map(function (sid) { var st = byId[sid] || null; return { sid: sid, st: st, name: st ? st.station_name_th : 'จุดจอด ' + sid, city: st ? st.city_th : '', code: st ? st.station_code : '', recs: recs.filter(function (r) { return String(r.route_id) === rid && r.station_id === sid; }) }; }).filter(function (g) { return !ONLY_STOPS_WITH_SCHEDULE || g.recs.length; }); var listEl = $('check-schedule-list'); if (listEl) renderList(listEl, route, groups); var desc = $('check-zone-desc'); if (desc) { var names = groups.map(function (g) { return g.name; }); desc.textContent = 'สาย ' + rid + ' · ' + (names.length > 1 ? names[0] + ' \u2192 ' + names[names.length - 1] + ' · ' : '') + groups.length + ' ' + t('check.stopsCount'); } /* KPI */ setStat('check-stat-stops', groups.length); /* นับจุดจอดที่แสดงจริง (ให้ตรงกับรายการด้านล่าง) */ setStat('check-stat-provinces', route.province_ids ? route.province_ids.length : ''); var trips = 0; recs.forEach(function (r) { if (String(r.route_id) === rid) trips += (r.departure_times || []).length; }); setStat('check-stat-trips', trips ? String(trips) : ''); refreshHighlights(); if (timer) clearInterval(timer); timer = setInterval(refreshHighlights, REFRESH_MS); }); } document.addEventListener('visibilitychange', function () { if (!document.hidden) refreshHighlights(); }); init(); })();