let currentVehicle = 'car'; function switchVehicle(type) { currentVehicle = type; document.getElementById('btn-car').classList.remove('active'); document.getElementById('btn-bike').classList.remove('active'); const rateInput = document.getElementById('interest-rate'); const carQuick = document.getElementById('quick-car-buttons'); const bikeQuick = document.getElementById('quick-bike-buttons'); if (type === 'car') { document.getElementById('btn-car').classList.add('active'); rateInput.value = 15; carQuick.style.display = 'flex'; bikeQuick.style.display = 'none'; } else { document.getElementById('btn-bike').classList.add('active'); rateInput.value = 22; carQuick.style.display = 'none'; bikeQuick.style.display = 'flex'; } calculateLoan(); } function setQuickAmount(amount) { document.getElementById('loan-amount').value = amount; calculateLoan(); } function calculateLoan() { const principal = parseFloat(document.getElementById('loan-amount').value) || 0; const months = parseInt(document.getElementById('loan-term').value) || 0; let annualRate = parseFloat(document.getElementById('interest-rate').value) || 0; const topUp = parseFloat(document.getElementById('top-up-amount').value) || 0; const transferType = document.querySelector('input[name="transfer-type"]:checked').value; if (transferType === 'no') { annualRate = annualRate + 2; } // ส่วนตรวจเช็กและแจ้งเตือนภัยหนี้นอกระบบแบบใช้ Backtick หลุดพยากรณ์ const dangerWarning = document.getElementById('danger-warning'); const rawInputRate = parseFloat(document.getElementById('interest-rate').value) || 0; if (annualRate > 24) { dangerWarning.style.display = 'block'; if (rawInputRate <= 24 && transferType === 'no') { dangerWarning.innerHTML = `⚠️ แจ้งเตือนความปลอดภัย: เนื่องจากคุณเลือกรูปแบบ "ไม่โอนเล่ม" ผู้ปล่อยสินเชื่อมักบวกดอกเบี้ยเพิ่มอีก 2% ทำให้ดอกเบี้ยรวมกลายเป็น ${annualRate}% ต่อปี ซึ่งเกินกว่าที่กฎหมายกำหนด โปรดระวังแอปเงินกู้ปลอมหรือหนี้นอกระบบที่ผิดกฎหมาย!`; } else { dangerWarning.innerHTML = `⚠️ แจ้งเตือนความปลอดภัย: อัตราดอกเบี้ยที่คุณกรอกเกิน 24% ต่อปี ซึ่งเกินกว่าที่กฎหมายกำหนด โปรดระวังแอปเงินกู้ปลอมหรือหนี้นอกระบบที่ผิดกฎหมาย!`; } } else { dangerWarning.style.display = 'none'; } if (principal <= 0 || months <= 0 || annualRate <= 0) return; const monthlyRate = (annualRate / 100) / 12; let monthlyPaymentStandard = (principal * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -months)); let tempPrincipalNormal = principal; let totalInterestNormal = 0; for (let i = 0; i < months; i++) { let interestMonth = tempPrincipalNormal * monthlyRate; totalInterestNormal += interestMonth; let principalPaid = monthlyPaymentStandard - interestMonth; tempPrincipalNormal -= principalPaid; } let tempPrincipalWithTopUp = principal; let totalInterestWithTopUp = 0; let actualMonthsPaid = 0; const tableBody = document.querySelector('#payment-schedule-table tbody'); if(tableBody) tableBody.innerHTML = ''; while (tempPrincipalWithTopUp > 0 && actualMonthsPaid < 120) { actualMonthsPaid++; let interestMonth = tempPrincipalWithTopUp * monthlyRate; totalInterestWithTopUp += interestMonth; let currentPayment = monthlyPaymentStandard; let currentTopUp = topUp; let totalPaidThisMonth = currentPayment + currentTopUp; if (tempPrincipalWithTopUp + interestMonth < totalPaidThisMonth) { totalPaidThisMonth = tempPrincipalWithTopUp + interestMonth; if (totalPaidThisMonth > currentPayment) { currentTopUp = totalPaidThisMonth - currentPayment; } else { currentPayment = totalPaidThisMonth; currentTopUp = 0; } } let principalPaid = totalPaidThisMonth - interestMonth; let previousPrincipal = tempPrincipalWithTopUp; tempPrincipalWithTopUp -= principalPaid; if(tableBody) { let row = ` ${actualMonthsPaid} ${Math.round(previousPrincipal).toLocaleString('th-TH')} บาท ${Math.round(interestMonth).toLocaleString('th-TH')} บาท ${Math.round(principalPaid - currentTopUp).toLocaleString('th-TH')} บาท ${Math.round(currentTopUp).toLocaleString('th-TH')} บาท ${Math.round(totalPaidThisMonth).toLocaleString('th-TH')} บาท `; tableBody.innerHTML += row; } } let savedInterest = totalInterestNormal - totalInterestWithTopUp; let savedTime = months - actualMonthsPaid; if (savedInterest < 0) savedInterest = 0; if (savedTime < 0) savedTime = 0; document.getElementById('res-monthly-payment').innerText = Math.round(monthlyPaymentStandard).toLocaleString('th-TH'); document.getElementById('res-total-interest').innerText = Math.round(totalInterestWithTopUp).toLocaleString('th-TH'); document.getElementById('res-saved-interest').innerText = Math.round(savedInterest).toLocaleString('th-TH'); document.getElementById('res-saved-time').innerText = savedTime; } function shareToLine() { const amount = document.getElementById('loan-amount').value; const payment = document.getElementById('res-monthly-payment').innerText; const text = encodeURIComponent(`ฉันลองใช้เครื่องมือคำนวณสินเชื่อจำนำทะเบียนรถ วงเงินกู้ ${amount} บาท ผ่อนต่อเดือนประมาณ ${payment} บาท/เดือน ลองไปคำนวณปรับแผนผ่อนของตัวเองดูได้ที่นี่นะ! -> ${window.location.href}`); window.open(`https://social-plugins.line.me/lineit/share?url=${text}`, '_blank'); } function openPopup() { document.getElementById('apply-popup').style.setProperty('display', 'flex', 'important'); document.getElementById('lead-finance-form').style.display = 'block'; document.getElementById('success-message').style.display = 'none'; document.getElementById('lead-finance-form').reset(); const currentAmount = parseFloat(document.getElementById('loan-amount').value) || 0; document.getElementById('lead-amount-display').value = currentAmount.toLocaleString('th-TH') + " บาท"; document.getElementById('lead-car-type').value = currentVehicle; } function closePopup() { document.getElementById('apply-popup').style.setProperty('display', 'none', 'important'); } function submitLeadForm(event) { event.preventDefault(); const submitBtn = document.querySelector('.btn-submit-lead'); const originalBtnText = submitBtn.innerText; submitBtn.innerText = "⏳ กำลังส่งข้อมูล..."; submitBtn.disabled = true; const leadData = { name: document.getElementById('lead-name').value, phone: document.getElementById('lead-phone').value, province: document.getElementById('lead-province').value, vehicleType: document.getElementById('lead-car-type').value, carModel: document.getElementById('lead-brand').value, loanAmount: document.getElementById('loan-amount').value }; // ⚠️ อย่าลืมเอา Web app URL ที่ได้จาก Google Apps Script มาวางแทนข้อความในเครื่องหมายคำพูดด้านล่างนี้นะครับ const googleScriptUrl = "https://script.google.com/macros/s/AKfycbwIXigjKma-Ek2N8DkkSWdKS0M_JJHSWvx9ehCHeyx6PkjP0NpHnirihCG0ePsctDyXvg/exec"; fetch(googleScriptUrl, { method: "POST", body: JSON.stringify(leadData) }) .then(response => response.json()) .then(result => { if(result.result === "success") { document.getElementById('lead-finance-form').style.display = 'none'; document.getElementById('success-message').style.display = 'block'; } else { alert("เกิดข้อผิดพลาดจากระบบ กรุณาลองใหม่อีกครั้ง หรือติดต่อเจ้าหน้าที่"); submitBtn.innerText = originalBtnText; submitBtn.disabled = false; } }) .catch(error => { console.error("Error:", error); alert("ไม่สามารถเชื่อมต่อระบบได้ในขณะนี้ กรุณาตรวจสอบอินเทอร์เน็ตของคุณ"); submitBtn.innerText = originalBtnText; submitBtn.disabled = false; }); } window.onclick = function(event) { const modal = document.getElementById('apply-popup'); if (event.target == modal) { modal.style.setProperty('display', 'none', 'important'); } } document.addEventListener("DOMContentLoaded", function() { const popup = document.getElementById('apply-popup'); if (popup) { document.body.appendChild(popup); } calculateLoan(); });