/* ================= CONFIG ================= */
const API_WABLAS = "API_KEY_WABLAS_KAMU";
const GOOGLE_API = "API_KEY_GOOGLE_KAMU";
const GOOGLE_SHEET_URL = "URL_WEB_APP_GOOGLE_SCRIPT";
/* ================= GLOBAL ================= */
let currentOTP = null;
let userCity = "";
/* ================= GET PARAM ================= */
function getParam(name){
return new URLSearchParams(window.location.search).get(name);
}
/* ================= AUTO INPUT ================= */
document.addEventListener("DOMContentLoaded", function(){
let type = getParam("type");
let motor = getParam("motor");
let campaign = getParam("campaign");
let nama = getParam("nama");
let email = getParam("email");
let phone = getParam("phone");
if(nama) document.querySelector('[name="nama"]').value = decodeURIComponent(nama);
if(email) document.querySelector('[name="email"]').value = email;
if(phone) document.querySelector('[name="phone"]').value = phone;
if(type){
let el = document.querySelector('[name="motor_type_group"]');
if(el){
el.value = type;
el.dispatchEvent(new Event('change'));
}
}
setTimeout(function(){
if(motor){
['motor_matic','motor_sport','motor_cub','motor_ev'].forEach(name=>{
let el = document.querySelector('[name="'+name+'"]');
if(el) el.value = motor;
});
let radio = document.querySelector('[value="'+motor+'"]');
if(radio) radio.checked = true;
}
if(campaign){
let el = document.querySelector('[name="campaign"]');
if(el) el.value = campaign;
}
},500);
});
/* ================= GEOLOCATION ================= */
navigator.geolocation.getCurrentPosition(function(pos){
let lat = pos.coords.latitude;
let lng = pos.coords.longitude;
document.querySelector('[name="user_location"]').value = lat+","+lng;
fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${GOOGLE_API}`)
.then(res=>res.json())
.then(data=>{
let comp = data.results[0].address_components;
comp.forEach(c=>{
if(c.types.includes("administrative_area_level_2")){
userCity = c.long_name;
document.querySelector('[name="user_city"]').value = userCity;
}
});
});
});
/* ================= VALIDASI LOKASI ================= */
function isAllowedCity(city){
let allowed = [
"Jakarta","Jakarta Selatan","Jakarta Barat","Jakarta Timur",
"Jakarta Utara","Jakarta Pusat",
"Tangerang","Kota Tangerang","Tangerang Selatan","Kabupaten Tangerang"
];
return allowed.some(a => city && city.includes(a));
}
/* ================= OTP ================= */
function generateOTP(){
return Math.floor(100000 + Math.random() * 900000);
}
function sendOTP(){
let phone = document.querySelector('[name="phone"]').value;
if(!/^628[0-9]{8,12}$/.test(phone)){
alert("Format WA harus 628xxxx");
return;
}
currentOTP = generateOTP();
document.querySelector('[name="otp_code"]').value = currentOTP;
fetch("https://console.wablas.com/api/send-message", {
method:"POST",
headers:{
"Authorization": API_WABLAS,
"Content-Type":"application/json"
},
body: JSON.stringify({
phone: phone,
message: "Kode OTP kamu: "+currentOTP
})
});
alert("OTP dikirim ke WhatsApp");
}
/* ================= GOOGLE SHEET ================= */
function sendToGoogleSheet(data){
fetch(GOOGLE_SHEET_URL, {
method: "POST",
body: JSON.stringify(data)
});
}
/* ================= VALIDASI FORM ================= */
document.addEventListener("submit", function(e){
let nama = document.querySelector('[name="nama"]').value;
let ktp = document.querySelector('[name="no_ktp"]').value;
let email = document.querySelector('[name="email"]').value;
let phone = document.querySelector('[name="phone"]').value;
let alamat = document.querySelector('[name="alamat"]').value;
let otp = document.querySelector('[name="otp"]').value;
let otpReal = document.querySelector('[name="otp_code"]').value;
if(nama.length < 3){
alert("Nama minimal 3 karakter"); e.preventDefault(); return;
}
if(!/^[0-9]{16}$/.test(ktp)){
alert("KTP harus 16 digit"); e.preventDefault(); return;
}
if(!/^S+@S+.S+$/.test(email)){
alert("Email tidak valid"); e.preventDefault(); return;
}
if(!/^628[0-9]{8,12}$/.test(phone)){
alert("WA harus format 628"); e.preventDefault(); return;
}
if(alamat.length {
let el = document.querySelector('[name="'+name+'"]');
if(el && el.value) motor = el.value;
});
sendToGoogleSheet({
nama, no_ktp: ktp, email, phone, alamat,
user_city: city, motor, type, campaign
});
let urlMap = {
"vario160": "/motor/honda-vario-160",
"beat": "/motor/honda-beat",
"stylo160": "/motor/honda-stylo-160",
"cbr150": "/motor/honda-cbr150",
"cbr250": "/motor/honda-cbr250",
"revo": "/motor/honda-revo",
"suprax": "/motor/honda-supra-x",
"em1": "/motor/honda-em1",
"em1_plus": "/motor/honda-em1-plus"
};
if(urlMap[motor]){
window.location.href = urlMap[motor];
} else {
window.location.href = "/motor";
}
});