let sheetLeads = []; // ดึงข้อมูลจาก Google Sheets มาแสดงที่หน้าหลังบ้าน function openAdminModal() { let container = document.getElementById('leadsListContainer'); container.innerHTML = `

⏳ กำลังดึงข้อมูลจาก Google Sheets...

`; document.getElementById('adminModal').classList.remove('hidden'); fetch(SCRIPT_URL) .then(res => res.json()) .then(data => { sheetLeads = data; if (!data || data.length === 0) { container.innerHTML = `

ยังไม่มีข้อมูลลูกค้าใน Google Sheets

`; return; } let html = ''; data.forEach((l, index) => { html += `
👤 ${l.customerName} (${l.phone}) ขอ: ${Number(l.requestAmount || 0).toLocaleString()}฿
รถ: ${l.carType} ${l.carBrand} (${l.carPlate}) | วันที่: ${l.datetime}
`; }); container.innerHTML = html; }) .catch(err => { container.innerHTML = `

❌ ดึงข้อมูลไม่สำเร็จ กรุณาตรวจสอบการ Deploy Apps Script

`; }); } // แสดงใบรับงาน 15 บรรทัดตามรูปแบบตัวอย่าง function showReceipt(index) { let l = sheetLeads[index]; if (!l) return; let rows = [ { label: "วันที่", val: l.datetime }, { label: "ชื่อตัวแทน", val: l.agentName }, { label: "รหัสตัวแทน", val: l.agentId }, { label: "ประเภทรายการ", val: l.itemType }, { label: "ประเภทลูกค้า", val: l.customerType }, { label: "ชื่อลูกค้า", val: l.customerName }, { label: "เบอร์โทร", val: l.phone }, { label: "ประเภทประกัน", val: l.insuranceType || "" }, { label: "บริษัทประกันภัย", val: l.insuranceCompany || "" }, { label: "ยอดลูกค้าขอ", val: l.requestAmount }, { label: "ประเภทรถ", val: l.carType }, { label: "ยี่ห้อรถ", val: l.carBrand || "" }, { label: "ปีรถ", val: l.carYear || "" }, { label: "ทะเบียน", val: l.carPlate || "" }, { label: "หมายเหตุ", val: l.note || "" } ]; currentReceiptText = rows.map(r => `${r.label} : ${r.val}`).join('\n'); let html = ''; rows.forEach(r => { html += `
${r.label} : ${r.val}
`; }); document.getElementById('receiptContent').innerHTML = html; document.getElementById('receiptModal').classList.remove('hidden'); }