// --- Create Affiliate Modal ---
function createAffiliateModal() {
const modal = document.createElement("div");
modal.id = "affiliateBuilder";
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 = `
`;
document.body.appendChild(modal);
// Close modal
document.getElementById("closeAffiliateModal").onclick = () => modal.style.display = "none";
modal.onclick = e => { if (e.target === modal) modal.style.display = "none"; };
// Build link when inputs change
const codeInput = document.getElementById("affiliateCode");
const campaignInput = document.getElementById("affiliateCampaign");
const linkInput = document.getElementById("affiliateLink");
[codeInput, campaignInput].forEach(input => {
input.addEventListener("input", () => {
const code = codeInput.value.trim() || "affiliate";
const campaign = campaignInput.value.trim() || "default";
linkInput.value = `https://ODOO4projects.com?utm_source=${encodeURIComponent(code)}&utm_campaign=${encodeURIComponent(campaign)}`;
});
});
// Copy to clipboard
document.getElementById("copyAffiliateLink").addEventListener("click", () => {
linkInput.select();
document.execCommand("copy");
alert("Affiliate link copied!");
});
return modal;
}
// --- Open Affiliate Modal ---
function openAffiliateModal() {
const modal = document.getElementById("affiliateBuilder");
modal.style.display = "flex";
// Reset fields
document.getElementById("affiliateCode").value = "";
document.getElementById("affiliateCampaign").value = "";
document.getElementById("affiliateLink").value = "";
}
// --- Attach Affiliate Builder Buttons ---
function attachAffiliateButtons() {
const buttons = Array.from(document.querySelectorAll("button, a"));
buttons.forEach(btn => {
const text = btn.textContent.trim();
if (text === "Start Selling") {
btn.addEventListener("click", (e) => {
e.preventDefault();
openAffiliateModal();
});
}
});
}
// --- Initialize Affiliate Builder ---
document.addEventListener("DOMContentLoaded", () => {
createAffiliateModal();
attachAffiliateButtons();
});