export default { async fetch(request, env, ctx) { const url = new URL(request.url); // ========================================== // 1. BACKEND API: สำหรับรับค่า QR จาก รปภ. // ========================================== if (request.method === 'POST' && url.pathname === '/api/scan') { try { const body = await request.json(); const { qr_token } = body; // จำลองการเช็ค Database (MVP: ถ้าขึ้นต้นด้วย VMS- ถือว่าผ่าน) if (qr_token && qr_token.startsWith('VMS-')) { return Response.json({ success: true, message: 'ผ่านได้', data: { house_number: '99/1', type: 'ผู้มาติดต่อ' } }); } else { return Response.json({ success: false, message: 'QR Code ไม่ถูกต้อง หรือ หมดอายุ' }, { status: 400 }); } } catch (error) { return Response.json({ success: false, message: 'ระบบขัดข้อง' }, { status: 500 }); } } // ========================================== // 2. FRONTEND WEB APP: หน้าจอผู้ใช้งาน (HTML/CSS/JS) // ========================================== if (request.method === 'GET' && url.pathname === '/') { const html = ` Smart Village MVP

Smart Village

บ้านเลขที่ 99/1

สร้าง QR Code สำหรับแขก

`; return new Response(html, { headers: { 'Content-Type': 'text/html;charset=UTF-8' }, }); } // Fallback สำหรับ Path อื่นๆ return new Response('Not Found', { status: 404 }); }, };