import Link from "next/link"; import { api, type RouteListItem, type Schedule } from "@/lib/api"; import { VisitTracker } from "@/lib/visit-tracker"; import { HERO_MAP, PARTNERS } from "@/lib/images"; import { Icon } from "@/components/Icons"; // Map route_group_code to visual tone const ZONE_TONES: Record = { bangkok: "sky", interprovincial: "purple", in_province: "pink", municipal: "mint", }; const ZONE_DESCS: Record = { bangkok: "เส้นทางเชื่อมต่อกรุงเทพมหานคร", interprovincial: "เส้นทางเชื่อมต่อจังหวัดใกล้เคียง", in_province: "เส้นทางภายในจังหวัดอ่างทอง", municipal: "เส้นทางภายในเขตเทศบาล", }; // Display order: municipal → bangkok → interprovincial → in_province const ZONE_ORDER = ["municipal", "bangkok", "interprovincial", "in_province"]; // Legend rows (คำอธิบายสีการ์ดตามหมวดเส้นทาง) const LEGEND = [ { tone: "sky", label: "หมวดเส้นทางเข้ากรุงเทพฯ — การ์ดสีน้ำเงิน" }, { tone: "purple", label: "หมวดเส้นทางระหว่างจังหวัด — การ์ดสีม่วง" }, { tone: "pink", label: "หมวดเส้นทางภายในจังหวัด — การ์ดสีชมพู" }, { tone: "mint", label: "หมวดเส้นทางภายในเทศบาล — การ์ดสีเขียวมินต์" }, { tone: "gray", label: "ยังไม่มีข้อมูล — การ์ดสีเทา แสดง N/A แทนเลขสาย" }, ]; const CHIP_CYCLE = ["purple", "sky", "pink", "mint"] as const; /** เที่ยวถัดไปของสาย = เที่ยวแรกที่ยังไม่ผ่าน */ function nextTrip(times: string[]): string | null { const now = new Date(); const cur = now.getHours() * 60 + now.getMinutes(); for (const t of [...times].sort()) { const [h, m] = t.split(":").map(Number); if (h * 60 + m >= cur) return t; } return null; } export default async function HomePage() { const [groupsRes, routesRes, schedRes] = await Promise.allSettled([ api.routeGroups(), api.routes(), api.schedules({}), ]); const groups = groupsRes.status === "fulfilled" ? groupsRes.value.items : []; const routes = routesRes.status === "fulfilled" ? routesRes.value.items : []; const schedules: Schedule[] = schedRes.status === "fulfilled" ? schedRes.value.items : []; // route_id -> all departure times (ทุกทิศทาง/จุดจอด) const timesByRoute = new Map(); for (const s of schedules) { const list = timesByRoute.get(s.route_id) ?? []; list.push(...s.departure_times); timesByRoute.set(s.route_id, list); } // Group routes by code const byCode = new Map(); for (const r of routes) { const list = byCode.get(r.route_group_code) ?? []; list.push(r); byCode.set(r.route_group_code, list); } const orderedGroups = [...groups].sort((a, b) => { const ai = ZONE_ORDER.indexOf(a.code); const bi = ZONE_ORDER.indexOf(b.code); return (ai === -1 ? 99 : ai) - (bi === -1 ? 99 : bi); }); // Duplicate partner tiles for a seamless marquee loop const marqueePartners = [...PARTNERS, ...PARTNERS]; return ( <>
{/* แผนที่เครือข่าย (จาก design: .image/network/overview-map.png) */}
ยินดีต้อนรับ

สถานีขนส่งผู้โดยสารภายใน
จังหวัดอ่างทอง

ดูสายรถ, ค้นหาป้าย และเปิดตัวอย่างเส้นทางได้ทันทีจากหน้าแรก

ดูรายละเอียดสายรถ ค้นหาป้ายหยุดรถ {routes[0] && ( ดูตัวอย่างสายรถ )}
{/* ผู้สนับสนุน & พาร์ทเนอร์ (marquee — จาก design) */}

ผู้สนับสนุน & พาร์ทเนอร์

ร่วมเป็นผู้สนับสนุนเราได้ เมื่อซื้อของออนไลน์ หรือซื้อตั๋วรถทัวร์ เครื่องบิน ออนไลน์

{marqueePartners.map((p, i) => ( {p.label} ))}
{/* Legend — คำอธิบายสีการ์ดตามหมวดเส้นทาง (จาก design) */}

คำอธิบายสีการ์ดตามหมวดเส้นทาง

    {LEGEND.map((l) => (
  • {l.label}
  • ))}
{orderedGroups.map((g) => { const items = byCode.get(g.code) ?? []; if (items.length === 0) return null; const tone = ZONE_TONES[g.code] ?? "purple"; return (
{g.label_th}

{ZONE_DESCS[g.code] ?? ""} · {items.length} เส้นทาง

{items.map((r, i) => { const times = timesByRoute.get(r.route_id) ?? []; const next = nextTrip(times); const chipTone = CHIP_CYCLE[i % CHIP_CYCLE.length]; return (

{r.route_id}

{r.name_th}

{next ? ( เที่ยวถัดไป {next} ) : times.length > 0 ? ( ดูตาราง ) : ( ยังไม่มีตาราง )}
); })}
); })}
); }