var mapsVal = formEl.querySelector('input[name="google_maps_link"]')?.value || '';
var payload = {
name: name,
phone: phone,
shop_name: shop,
shop_type: "Wholesale Partner",
city: location,
remarks: "Daily Req: " + quantity + " | Products: " + productStr + " | Expected Start: " + startDate + " | GPS: " + mapsVal + " | Notes: " + messageTxt
};
try {
// This form captures approximate requirements, so save it as a sales
// lead instead of creating an inaccurate dispatch order.
const res = await fetch('dashboard/save_quote.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const data = await res.json();
if (!res.ok || !data.success) {
throw new Error(data.error || 'Unable to submit your requirement.');
}
var locMsg = location + (mapsVal ? "\n🗺️ *GPS Maps:* " + mapsVal : '');
// Build WhatsApp Message
var message = "👋 Hi FrySS Team, I submitted a wholesale requirement!\n\n" +
"👤 *Owner:* " + name + "\n" +
"🏪 *Shop Name:* " + shop + "\n" +
"📞 *Phone:* " + phone + "\n" +
"📍 *Location:* " + locMsg + "\n" +
"--------------------------------\n" +
"🆔 *Lead ID:* " + (data.customer?.id || 'New Enquiry') + "\n" +
"📦 *Daily Req:* " + quantity + "\n" +
"🥟 *Interests:* " + productStr + "\n" +
"📅 *Delivery Date:* " + startDate + "\n" +
"📝 *Note:* " + (messageTxt ? messageTxt : "None") + "\n\n" +
"Please contact me to confirm products, quantity, and pricing.";
var whatsappUrl = "https://wa.me/917003837512?text=" + encodeURIComponent(message);
window.open(whatsappUrl, '_blank');
} catch (err) {
alert(err.message || 'Unable to submit the form. Opening WhatsApp instead.');
var whatsappUrl = "https://wa.me/917003837512?text=Enquiry%20from%20" + encodeURIComponent(name);
window.open(whatsappUrl, '_blank');
}
}
// Drag and Drop Visual Feedback Script
const dropArea = document.querySelector('.file-drop-area');
const fileInput = document.querySelector('.file-input');
const msg = document.querySelector('.file-msg');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
function highlight(e) {
dropArea.style.background = '#ffe4e6'; // Light red
dropArea.style.borderColor = '#ef4444';
}
function unhighlight(e) {
dropArea.style.background = '#fff1f2';
dropArea.style.borderColor = '#fca5a5';
}
dropArea.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
fileInput.files = files;
updateMsg(files);
}
fileInput.addEventListener('change', function() {
updateMsg(this.files);
});
function updateMsg(files) {
if (files.length > 0) {
msg.textContent = files[0].name + " selected (Please send via WhatsApp)";
msg.classList.add('text-red-600');
msg.classList.add('font-bold');
} else {
msg.textContent = "Click to select file or drag visiting card here";
msg.classList.remove('text-red-600');
msg.classList.remove('font-bold');
}
}