/* ============================================================ 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; var OTHER_ENTRIES = []; var ROUTE_IDS = []; /* 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 || '–'; } /* ---------- จำแนกประเภทจุดจอดจากข้อมูล + ลำดับในเส้นทาง ---------- */ /* บทบาทของจุดในการ์ด: index 0 = ต้นทาง · index สุดท้าย = ปลายทาง ระหว่างนั้น: จุดที่เป็น "สถานีขนส่งหลัก" = กลางทาง (จุดพักรถที่คนขึ้น/ลงได้) จุดป้ายริมทางทั่วไป = ผ่านทาง ⚠️ ใช้กับ "ลำดับที่แสดงในหน้านี้" เท่านั้น — ลำดับมาอยู่ที่ station_order ไม่ใช่บทบาทถาวร */ 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-map-pin', 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); } /* มีอย่างน้อย 1 ทิศที่มีตารางจริงไหม — ถ้ามี ห้ามขึ้น "ยังไม่มีข้อมูล" ของทิศที่ว่าง (จุดเดียวมีได้หลายทิศ · ทิศว่างไม่ควรทำให้การ์ดที่ตารางขึ้นแล้วดูเหมือนไม่มีข้อมูล) */ var hasAny = g.recs.some(function (x) { return (x.departure_times || []).length > 0; }); g.recs.forEach(function (r) { var times = (r.departure_times || []).slice(); /* ทิศนี้ยังไม่มีเวลา → ข้ามไปเลยถ้าทิศอื่นมีการ์ดข้อมูลอยู่แล้ว */ if (!times.length) { if (hasAny) return; var empty = document.createElement('p'); empty.className = 'mu-stop-sub'; empty.textContent = t('route.empty'); body.appendChild(empty); return; } /* หัวแถว = ชื่อทิศ (direction_label) → ไม่มีค่อยใช้ direction_note → ไม่มีอีกค่อยบอกว่าอยู่ที่จุดนี้ ทิศที่ไปคนละทางจากจุดเดียวกัน (ป่าโมกไปอ่างทอง / ป่าโมกไปอยุธยา) จึงแยกให้เห็นชัด */ var sub = document.createElement('p'); sub.className = 'mu-stop-sub'; if ((r.direction_label || r.dir_label)) { sub.innerHTML = esc((r.direction_label || r.dir_label)) + (r.direction_note ? ' ' + esc(r.direction_note) + '' : ''); } else if (r.direction_note) { sub.innerHTML = esc(r.direction_note) + ' ' + esc(t('check.direction')) + ''; } else { sub.textContent = t('check.atStop') + ' ' + g.name; } body.appendChild(sub); var grid = document.createElement('div'); grid.className = 'mu-time-grid' + (r.estimated ? ' is-est' : ''); /* เวลาที่คำนวณจากต้นทาง */ 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); if (r.estimated) { var est = document.createElement('p'); est.className = 'mu-est-note'; /* มาจากกฎ "จุดนี้ = เวลาจุดอ้างอิง ± n นาที" → บอกจุดอ้างอิง + เตือนเรื่องการจราจร */ est.textContent = r.estimateFrom && window.Schedules.offsetLabel ? t('time.derived', { base: r.estimateFrom.name || ('#' + r.estimateFrom.station_id), n: Schedules.offsetLabel(r.estimateFrom.offsetMin) }) : t('time.estimated'); body.appendChild(est); } 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); }); } /* ---------- แถวข้อมูลแบบ "ชิ้น" (segment) ---------- แต่ละชิ้นเป็น flex item → ตัดบรรทัดเป็นหน่วย ไม่ตัดกลางคำ (สำคัญกับข้อความไทยที่ไม่มีเว้นวรรค เช่น "บขส.อ่างทอง → สถานีเดินรถโดยสารขนาดเล็ก (จตุจักร)" จะไม่ถูกตัดกลางชื่อสถานี) · ตัวคั่น "·" วาดด้วย CSS (.mu-zone-seg + .mu-zone-seg::before) */ function icSvg(name) { return ''; } /* seg = "ข้อความล้วน" หรือ { html: "...", icon: "ic-xxx" } (html ต้อง esc() มาก่อน) */ function setSegments(host, segs) { if (!host) return; host.innerHTML = ''; segs.forEach(function (s) { var seg = document.createElement('span'); seg.className = 'mu-zone-seg'; if (s && typeof s === 'object') { seg.innerHTML = (s.icon ? icSvg(s.icon) : '') + (s.html || ''); } else { seg.textContent = String(s == null ? '' : s); /* ข้อความล้วน — ปลอดภัยจาก HTML */ } host.appendChild(seg); }); } /* ---------- "อัปเดตล่าสุด N วันที่แล้ว" จากฟิลด์ updated (ISO) ---------- เทียบ "วันนี้" กับวันที่ในฟิลด์ updated (ไม่ใช้เวลา → ไม่มีปัญหา timezone) วันเดียวกัน → "วันนี้" · เมื่อวาน → "เมื่อวาน" < 7 วัน → N วันที่แล้ว · < 30 วัน → N สัปดาห์ที่แล้ว < 365 วัน → N เดือนที่แล้ว · มากกว่า → N ปี M เดือนที่แล้ว หมายเหตุ: ใช้ mtime/Last-Modified จากเบราว์เซอร์ไม่ได้ (fetch ข้ามโดเมนไม่ให้อ่าน) → ค่ามาจาก resource/tools/stamp-schedules.py ที่เติมจาก mtime จริง */ function daysAgo(iso) { var m = /^(\d{4})-(\d{2})-(\d{2})/.exec(String(iso || '').trim()); if (!m) return null; var then = new Date(Date.UTC(+m[1], +m[2] - 1, +m[3])); if (isNaN(then.getTime())) return null; var now = new Date(); var today = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate())); return Math.round((today - then) / 86400000); } function agoLabel(days) { if (days == null) return ''; if (days <= 0) return t('check.agoToday'); if (days === 1) return t('check.agoYesterday'); if (days < 7) return t('check.agoDays', { n: days }); if (days < 30) return t('check.agoWeeks', { n: Math.floor(days / 7) }); if (days < 365) return t('check.agoMonths', { n: Math.floor(days / 30) }); var y = Math.floor(days / 365), mo = Math.floor((days % 365) / 30); return mo ? t('check.agoYearsMonths', { y: y, m: mo }) : t('check.agoYears', { y: y }); } function appendSubStopCards(listEl, rid, groups, schedDoc, recs) { if (!window.OtherStop || !OtherStop.allStops || !window.Schedules) return; /* allStops() คืน "รายการแบน": [{ index, name, city_th, province_th, lat, lng, offsetMin, hasCoord, entry }] */ var sub = OtherStop.allStops(OTHER_ENTRIES, rid, ROUTE_IDS); if (!sub || sub.length < 2) return; /* มีจุดเดียว = การ์ดหลักแสดงอยู่แล้ว */ var first = sub[0].name; /* จุดอ้างอิง = จุดแรกของจุดย่อย (index 0 · offset = 0) ซึ่งตรงกับการ์ดหลัก (station 8) แต่ละทิศมี "จุดอ้างอิงของตัวเอง" — ทิศที่มีข้อมูล (station_id = จุดอ้างอิง) ให้ offset คงที่ (+0) สายที่วิ่ง 2 ทาง (ไป-กลับ) จึงแสดงครบทั้ง 2 ทิศในทุกจุดย่อย ทิศที่ยังไม่มีตาราง (แค่ชื่อทิศ) → ไม่แสดง (ห้ามเดาเวลาที่ไม่มีต้นทาง) */ /* จุดอ้างอิง = "จุดขึ้นรถประจำทางอื่นๆ" (station 8) — จุดแรกของ other_stop คือจุดที่มีตารางจริง ไม่ใช้ "record แรก" เพราะ recs เรียงตาม station_order (บขส.อ่างทองมาก่อน) จะได้จุดอ้างอิงผิด */ var hubSid = null, hubDir = null; /* hubDir = ชื่อทิศ "ของจุดอ้างอิง" (ตัวตั้งเวลา) */ groups.forEach(function (g) { if (String(g.sid) === '8') hubSid = '8'; }); if (hubSid === null) { /* ไม่มี station 8 → ใช้จุดแรกที่มีเวลาตามเดิม */ groups.forEach(function (g) { g.recs.forEach(function (r) { if ((r.departure_times || []).length && hubSid === null) hubSid = String(r.station_id); }); }); } /* ทิศตั้งต้น = ทิศที่ "เริ่มจากจุดอ้างอิง" (จุดแรกของ other_stop = วัดสีบัวทอง) ตัวชี้ที่ใช้ได้: ชื่อทิศตรงกับ "จาก<ชื่อจุดแรก>" หรือ note ของจุดย่อย */ var hubName = sub[0].name; recs.forEach(function (r) { if (String(r.route_id) !== rid) return; if (hubDir !== null || !(r.departure_times || []).length) return; var lbl = String((r.direction_label || r.dir_label) || ''); var note = String((r.direction_note || r.dir_note) || ''); if (lbl.indexOf(hubName) !== -1 || note.indexOf(hubName) === 0) hubDir = (r.direction_label || r.dir_label); }); if (hubSid === null) return; /* ไม่มีตารางจริงเลย → คำนวณไม่ได้ */ /* ทุกทิศของสายนี้: ทิศของจุดอ้างอิง = เวลาต้นทาง · ทิศอื่น = จุดอ้างอิง + offset ของจุดนี้ */ var legs = []; recs.forEach(function (r) { if (String(r.route_id) !== rid) return; /* เฉพาะสายนี้ — recs มีทุกสาย */ if (!(r.departure_times || []).length) return; /* ทิศนี้ยังไม่มีตาราง → ข้าม */ legs.push({ rec: r, fixed: ((r.direction_label || r.dir_label) === hubDir) ? 0 : null }); }); if (!legs.length) return; /* ไม่มีทิศที่คำนวณได้ → ไม่แสดงการ์ดย่อย */ /* จุดแรกของจุดย่อย = จุดอ้างอิงเอง (แสดงเป็นการ์ดหลักอยู่แล้ว) → เริ่มที่จุดที่ 2 และข้ามจุดที่พิกัด/ชื่อซ้ำกับจุดอ้างอิง */ sub.slice(1).forEach(function (x) { if (x.name === hubName) return; /* ชื่อซ้ำกับการ์ดต้นทาง → ไม่แสดงซ้ำ */ var off = (x.offsetMin == null ? null : Number(x.offsetMin)); var li = document.createElement('li'); var det = document.createElement('details'); det.className = 'mu-stop-card'; det.open = true; var where = [x.city_th, x.province_th].filter(Boolean).join(' · '); var sum = document.createElement('summary'); sum.innerHTML = '' + ' ' + esc(t('stops.subStop')) + '' + '' + esc(x.name) + '' + esc(where) + '' + '\u2304' + ''; det.appendChild(sum); var body = document.createElement('div'); body.className = 'mu-stop-body'; if (off == null) { /* ยังไม่ระบุเวลาเดินทาง → คำนวณไม่ได้ */ var na = document.createElement('p'); na.className = 'mu-stop-sub'; na.textContent = t('stops.noOffset'); body.appendChild(na); } else { legs.forEach(function (leg) { var r = leg.rec; var raw = (r.departure_times || []).length ? r.departure_times : []; if (!raw.length) return; /* ทิศนี้ยังไม่มีตารางจริง → ข้าม */ /* ทิศของจุดอ้างอิงเอง (fixed = 0) → เวลาเท่าต้นทาง · ทิศอื่น → บวก offset ของจุดนี้ */ var o = (leg.fixed === 0) ? 0 : off; var times = (o === 0) ? raw.slice() : Schedules.shiftTimes(raw, o); var head = document.createElement('p'); head.className = 'mu-stop-sub'; head.textContent = (r.direction_label || r.dir_label) || r.direction_note || t('check.fromName', { name: first }); body.appendChild(head); var grid = document.createElement('div'); grid.className = 'mu-time-grid' + (o === 0 ? '' : ' is-est'); var chips = []; times.forEach(function (tm) { var chip = document.createElement('div'); chip.className = 'mu-time-chip'; chip.textContent = tm; grid.appendChild(chip); chips.push(chip); }); body.appendChild(grid); /* ให้จุดย่อยอยู่ในระบบ "รอบถัดไป" ด้วย (refreshHighlights วน sections[]) ไม่งั้นชิปของจุดย่อยไม่ถูกไฮไลต์ และรอบถัดไปมองไม่เห็นเวลาของจุดนี้ */ sections.push({ chips: chips, times: times, station: x.name }); if (o !== 0) { var note = document.createElement('p'); note.className = 'mu-est-note'; note.textContent = t('time.derived', { base: first, n: Schedules.offsetLabel(o) }); body.appendChild(note); } }); } det.appendChild(body); li.appendChild(det); listEl.appendChild(li); }); } /* ---------- ไฮไลต์เที่ยวถัดไป + บรรทัดเวลาปัจจุบัน ---------- */ function refreshHighlights() { var nowm = nowMin(); var next = null; /* เที่ยวถัดไปที่เร็วที่สุดในทุกจุดจอด */ /* เลือกเที่ยวถัดไปของจุดหนึ่ง ๆ - ถ้ามีเที่ยวที่ยังไม่ผ่านวันนี้ → ใช้ตัวแรกนั้น - ถ้าหมดทุกเที่ยวของวันแล้ว → วนไปเที่ยวแรกของ "วันถัดไป" (+1440 เพื่อเทียบลำดับข้ามวันได้) หน้าจอแสดงแค่ HH:MM (ไม่ติดป้าย "พรุ่งนี้" — เจ้าของข้อมูลกำหนด) แต่ไฮไลต์ชิปแบบเดียวกัน */ function pick(sec, idx, m) { if (idx < 0 || !sec.chips[idx]) return; sec.chips[idx].classList.add('next'); if (next === null || m < next.m) next = { m: m, time: sec.times[idx], station: sec.station }; } sections.forEach(function (sec) { sec.chips.forEach(function (c) { c.classList.remove('next'); }); if (!sec.times.length) return; for (var i = 0; i < sec.times.length; i++) { var m = toMin(sec.times[i]); if (m >= nowm) { pick(sec, i, m); return; } } pick(sec, 0, toMin(sec.times[0]) + 1440); /* หมดวัน → เที่ยวแรกพรุ่งนี้ */ }); setStat('check-stat-next', next ? next.time : ''); var line = $('check-now-line'); if (!line) return; var nowSegs = [{ icon: 'ic-clock', html: esc(t('check.now')) + ' ' + hhmm(nowm) + '' }]; if (next) { nowSegs.push({ html: esc(t('check.nextAt')) + ' ' + esc(next.time) + ' ' + '(' + esc(next.station) + ')' }); } else if (sections.length) { nowSegs.push({ html: '' + esc(t('check.noMoreToday')) + '' }); } setSegments(line, nowSegs); } /* ---------- โหลด + แสดงผล ---------- */ 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'), Schedules.load().then(function (sd) { return Schedules.normSub(sd); }), fetchJSON(DATA + '8 other_stop.json').catch(function () { return null; }) /* จุดย่อยของ "จุดขึ้นรถอื่นๆ" */ ]).then(function (res) { var routes = (res[0] || {}).routes || []; OTHER_ENTRIES = (res[3] || {}).other_stop || []; ROUTE_IDS = routes.map(function (r) { return String(r.route_id); }); var sts = (res[1] || {}).stations || []; if (window.Schedules && Schedules.setStationNames) Schedules.setStationNames(sts); /* ให้ข้อความ "คำนวณจาก ..." มีชื่อจุด */ var recs = window.Schedules ? Schedules.records(res[2]) : ((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 = t('common.routePrefix', { n: 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 = t('common.routePrefix', { n: rid }) + (route.route_name_th ? ' · ' + route.route_name_th : ''); /* "อัปเดตล่าสุด N วันที่แล้ว" — วันที่จากฟิลด์ updated ของไฟล์ตารางสายนี้ (Schedules.updatedOf) แยกบรรทัดของตัวเอง (#check-updated-line) เพราะ #check-now-line แสดงเวลาปัจจุบันอยู่แล้ว */ var updLine = $('check-updated-line'); if (updLine) { var upd = (window.Schedules && Schedules.updatedOf) ? Schedules.updatedOf(rid) : null; var nDays = daysAgo(upd); updLine.textContent = nDays == null ? '' : t('check.updatedAgo', { ago: agoLabel(nDays) }); } /* จัดกลุ่มตามลำดับจุดจอดของสาย แล้วต่อด้วยจุดที่เหลือ (ถ้ามีข้อมูล) */ var order = (route.station_ids || []).slice(); /* schema 2: ใช้ station_order ที่ตรวจทิศทางมาแล้วจากไฟล์ (ถ้ามี) */ var sched = window.Schedules ? Schedules.route(res[2], rid) : null; if (sched && sched.station_order.length) order = sched.station_order.slice(); recs.forEach(function (r) { if (String(r.route_id) === rid && order.indexOf(r.station_id) === -1) order.push(r.station_id); }); /* จุดที่เป็น "จุดขึ้นรถประจำทางอื่นๆ" (station 8) ให้ใช้ชื่อจริงของสายนั้น จาก 8 other_stop.json (จุดแรก) เช่น 954 → "ตลาดท่าช้าง" แทนชื่อกลาง ๆ */ var firstSubName = null; if (window.OtherStop && OtherStop.allStops) { var allSub = OtherStop.allStops(OTHER_ENTRIES, rid, ROUTE_IDS); if (allSub.length) firstSubName = allSub[0].name; /* จุดแรกของจุดย่อย = ชื่อจริงของสายนี้ */ } var groups = order.map(function (sid) { var st = byId[sid] || null; var isOtherHub = String(sid) === '8'; /* จุดรวม "จุดขึ้นรถอื่นๆ" */ return { sid: sid, st: st, name: isOtherHub && firstSubName ? firstSubName : (st ? st.station_name_th : t('check.stopNameN', { n: 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); /* จุดย่อยของสายนั้น (จาก 8 other_stop.json) — จุดที่ 2 เป็นต้นไปของสาย 8 เช่น 2194: ① วัดหัวเวียง ② วัดถนน (เวลาจุดถัดไป = เวลาจุดแรก + offsets_min) */ appendSubStopCards(listEl, rid, groups, res[2], recs); } var desc = $('check-zone-desc'); if (desc) { var names = groups.map(function (g) { return g.name; }); /* บอกว่า "ตารางวันนี้" เป็นของวันไหน — เฉพาะสายที่มีการแยกตารางตามวัน (service) เท่านั้น สายอื่นข้อความเหมือนเดิมทุกตัวอักษร */ /* ป้ายบอกว่า "ตารางที่เห็นอยู่นี้เป็นของวันไหน" — ใช้ผลตรวจวันจากตัวอ่านกลาง (ไม่คำนวณเองในหน้านี้) */ var dayText = ''; if (window.Schedules && Schedules.dayInfo && Schedules.directions) { var di = Schedules.dayInfo(); var infos = (Schedules.directions(res[2], rid) || []).map(function (d) { return d.service || { from: 'ไม่ระบุ' }; }); var conflict = infos.some(function (x) { return x.conflict; }); /* ทิศเดียวมีทั้งวันทำงาน+วันหยุด */ var unknown = infos.some(function (x) { return x.unknown; }); /* ยังไม่ระบุวันเลย */ var selectedByDay = infos.some(function (x) { return x.from === 'service' || x.from === 'ระบุตรง' || x.from === 'คำในโน้ต'; }); if (unknown) { dayText = t('check.dayUnknown'); } else if (conflict) { dayText = t('check.dayMixed'); } else if (selectedByDay) { dayText = t(di.dayType === 'holiday' ? 'check.dayHoliday' : 'check.dayWeekday') + ' (' + t('check.dayOfWeek') + ' ' + (window.Schedules && Schedules.dayName ? Schedules.dayName(di.weekday) : di.weekday_th) + ')'; } } /* แยกเป็นชิ้น ๆ เพื่อให้ตัดบรรทัดได้เป็นหน่วย (ไม่ตัดกลางชื่อสถานี/ชื่อวัน) */ var segs = [t('common.routePrefix', { n: rid })]; if (names.length > 1) segs.push(names[0] + ' \u2192 ' + names[names.length - 1]); segs.push(groups.length + ' ' + t('check.stopsCount')); if (dayText) segs.push(dayText); setSegments(desc, segs); } /* 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(); })();