// --- Category Configuration --- const CATEGORY_CONFIG = { 3: { showLocation: false, webhook: "" }, // services 4: { showLocation: true, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3" }, // hosting 5: { showLocation: false, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/fd20e194-b821-4b1f-814f-7bb93ae16046" }, // Workshops 7: { showLocation: false, webhook: "" }, // modules }; // --- Helper: Get cookie by name --- function getCookie(name) { const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); return match ? decodeURIComponent(match[2]) : null; } // --- Helper: Set cookie if not already set --- function setCookieIfEmpty(name, value, days = 90) { if (!value) return; if (getCookie(name)) return; // Don't overwrite const date = new Date(); date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); document.cookie = `${name}=${encodeURIComponent(value)}; expires=${date.toUTCString()}; path=/; SameSite=Lax`; } // --- Add UTM fields to form using cookies --- function addUtmFields(form) { const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"]; const defaults = { utm_source: "homepage", utm_medium: "direct", utm_campaign: "none", utm_term: "", utm_content: "" }; utmParams.forEach(param => { let input = form.querySelector(`input[name="${param}"]`); if (!input) { input = document.createElement("input"); input.type = "hidden"; input.name = param; form.appendChild(input); } // First check cookie, then fallback to default input.value = getCookie(param) || defaults[param]; }); } // --- Create Modal --- function createModal() { const modal = document.createElement("div"); modal.id = "buyNowModal"; Object.assign(modal.style, { position: "fixed", top: "0", left: "0", width: "100%", height: "100%", backgroundColor: "rgba(0,0,0,0.6)", display: "none", justifyContent: "center", alignItems: "center", zIndex: "1000" }); modal.innerHTML = `
×

Order Details

`; document.body.appendChild(modal); document.getElementById("closeModal").onclick = () => modal.style.display = "none"; document.getElementById("closeConfirmation").onclick = () => { document.getElementById("confirmation").style.display = "none"; modal.style.display = "none"; }; modal.onclick = e => { if (e.target === modal) modal.style.display = "none"; }; return modal; } // --- Open Modal --- function openModal(productHref) { const modal = document.getElementById("buyNowModal"); const form = modal.querySelector("#buyForm"); const confirmation = modal.querySelector("#confirmation"); modal.style.display = "flex"; form.style.display = "flex"; confirmation.style.display = "none"; const parts = productHref.split("/").filter(Boolean); const [id, price, category, ...productParts] = parts; const product = decodeURIComponent(productParts.join("/")); const categoryNum = parseInt(category, 10); const config = CATEGORY_CONFIG[categoryNum] || { showLocation: false, webhook: "" }; const locationSelect = document.getElementById("locationSelect"); if (config.showLocation) { locationSelect.style.display = "block"; locationSelect.setAttribute("required", "true"); } else { locationSelect.style.display = "none"; locationSelect.removeAttribute("required"); locationSelect.value = ""; } form.querySelector('input[name="id"]').value = id || ""; form.querySelector('input[name="price"]').value = price || "0"; form.querySelector('input[name="category"]').value = category || ""; form.querySelector('input[name="product"]').value = product || "Unknown"; form.dataset.webhook = config.webhook || ""; modal.querySelector("#productText").textContent = `We have send you the payment details for your order ${product} $${price} via mail.`; addUtmFields(form); // Read from cookie or defaults } // --- Handle Form Submit --- function handleFormSubmit() { const form = document.getElementById("buyForm"); const confirmation = document.getElementById("confirmation"); form.addEventListener("submit", async (e) => { e.preventDefault(); addUtmFields(form); const data = Object.fromEntries(new FormData(form).entries()); const webhookUrl = form.dataset.webhook; if (!webhookUrl) { alert("No webhook configured for this category."); return; } try { const res = await fetch(webhookUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); if (res.ok) { form.style.display = "none"; confirmation.style.display = "block"; form.reset(); } else { alert("Failed to submit form."); } } catch (err) { console.error(err); alert("Error submitting form."); } }); } // --- Attach Buttons --- function attachButtons() { const buttons = Array.from(document.querySelectorAll("button, a")); buttons.forEach(btn => { const text = btn.textContent.trim(); if (text === "Buy Now" || text === "Book Now") { btn.addEventListener("click", (e) => { e.preventDefault(); const href = btn.getAttribute("href") || btn.dataset.product || "Unknown/0/0"; openModal(href); }); } }); } // --- Initialize --- document.addEventListener("DOMContentLoaded", () => { createModal(); handleFormSubmit(); attachButtons(); });