document.addEventListener("DOMContentLoaded",function(){const u=window.affiliateLinksConfig||{},m=u.routes||{},o=u.translations||{},f=u.csrfToken||"",k=document.getElementById("linkModal"),i=k?new bootstrap.Modal(k):null;document.addEventListener("click",function(n){const e=n.target.closest(".js-copy-url");if(!e)return;const c=e.dataset.url;c&&navigator.clipboard.writeText(c).then(function(){const t=e.innerHTML;e.innerHTML='<i class="fas fa-check"></i>',e.classList.remove("btn-primary","btn-outline-primary"),e.classList.add("btn-success"),setTimeout(function(){e.innerHTML=t,e.classList.remove("btn-success"),e.classList.add(e.classList.contains("btn-sm")?"btn-outline-primary":"btn-primary")},2e3)}).catch(function(){const t=document.createElement("input");t.value=c,document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)})});const y=document.querySelector(".js-create-link-btn");y&&y.addEventListener("click",function(){document.getElementById("linkId").value="",document.getElementById("linkLabel").value="",document.getElementById("linkTargetUrl").value="",document.getElementById("linkActive").checked=!0,document.getElementById("linkActiveGroup").style.display="none",document.getElementById("linkModalTitle").textContent=o.createLink||"Create Link",i&&i.show()}),document.addEventListener("click",function(n){const e=n.target.closest(".js-edit-link");e&&(document.getElementById("linkId").value=e.dataset.id,document.getElementById("linkLabel").value=e.dataset.label,document.getElementById("linkTargetUrl").value=e.dataset.url,document.getElementById("linkActive").checked=e.dataset.active==="1",document.getElementById("linkActiveGroup").style.display="block",document.getElementById("linkModalTitle").textContent=o.editLink||"Edit Link",i&&i.show())});const d=document.getElementById("linkSaveBtn");d&&d.addEventListener("click",function(){const n=document.getElementById("linkId").value,e=document.getElementById("linkLabel").value.trim(),c=document.getElementById("linkTargetUrl").value.trim(),t=document.getElementById("linkActive").checked;if(!e||!c)return;const l=n!=="",r=l?m.update.replace(":id",n):m.store,s=l?"PUT":"POST",g={label:e,target_url:c};l&&(g.is_active=t?1:0),d.disabled=!0,fetch(r,{method:s,headers:{"Content-Type":"application/json","X-CSRF-TOKEN":f,Accept:"application/json"},body:JSON.stringify(g)}).then(function(a){return a.json()}).then(function(a){a.success?(i&&i.hide(),window.location.reload()):alert(a.message||o.errorOccurred||"An error occurred.")}).catch(function(){alert(o.errorOccurred||"An error occurred.")}).finally(function(){d.disabled=!1})}),document.addEventListener("click",function(n){const e=n.target.closest(".js-delete-link");if(!e)return;const c=e.dataset.id,t=o.confirmDelete||"Are you sure you want to delete this link?";if(!confirm(t))return;const l=m.destroy.replace(":id",c);fetch(l,{method:"DELETE",headers:{"X-CSRF-TOKEN":f,Accept:"application/json"}}).then(function(r){return r.json()}).then(function(r){if(r.success){const s=e.closest("tr");s&&s.remove()}else alert(r.message||o.errorOccurred||"An error occurred.")}).catch(function(){alert(o.errorOccurred||"An error occurred.")})})});
