import{a as f}from"./bootstrap.esm-Bb7dPm-H.js";const _=window.promotionForm||{};document.addEventListener("DOMContentLoaded",function(){var t;h(),(t=document.getElementById("promotionForm"))==null||t.addEventListener("submit",function(n){n.preventDefault(),b()})});function h(){const t=document.getElementById("type"),n=document.getElementById("currency_id"),o=document.getElementById("amount_unit"),e=document.querySelector(".currency-required-star");function r(){if(t.value==="fixed"){const l=(_.currencies||[]).find(u=>String(u.id)===String(n.value));o.textContent=l?l.code:window.__curfallback||"₺",n.setAttribute("required","required"),e&&(e.style.display="")}else o.textContent="%",n.removeAttribute("required"),e&&(e.style.display="none")}t==null||t.addEventListener("change",r),n==null||n.addEventListener("change",r),r();const c=document.getElementById("target_type");function a(){const i=c.value;document.getElementById("targetProductsGroup").style.display=i==="specific_products"?"block":"none",document.getElementById("targetGroupsGroup").style.display=i==="specific_groups"?"block":"none"}c==null||c.addEventListener("change",a),a()}function w(){var t;return((t=document.querySelector('meta[name="csrf-token"]'))==null?void 0:t.getAttribute("content"))||""}async function b(){var c,a,i,l,u,y,p;const t=document.getElementById("promotionForm"),n=t.dataset.submitUrl,o=(t.dataset.submitMethod||"POST").toUpperCase(),e=new FormData(t),r={code:e.get("code"),description:e.get("description")||null,status:e.get("status"),type:e.get("type"),amount:e.get("amount"),currency_id:e.get("currency_id")||null,limit:e.get("limit")||null,usage_limit_per_client:e.get("usage_limit_per_client")||null,cycles:e.get("cycles")||null,start_date:e.get("start_date")||null,end_date:e.get("end_date")||null,target_type:e.get("target_type")||"all",target_products:e.getAll("target_products[]").filter(s=>s),target_groups:e.getAll("target_groups[]").filter(s=>s),is_combinable:((c=t.querySelector("#is_combinable"))==null?void 0:c.checked)||!1,only_new_client:((a=t.querySelector("#only_new_client"))==null?void 0:a.checked)||!1,only_existing_client:((i=t.querySelector("#only_existing_client"))==null?void 0:i.checked)||!1,only_once_client:((l=t.querySelector("#only_once_client"))==null?void 0:l.checked)||!1,only_new_orders:((u=t.querySelector("#only_new_orders"))==null?void 0:u.checked)||!1,allow_renewal:((y=t.querySelector("#allow_renewal"))==null?void 0:y.checked)||!1,allow_upgrades:((p=t.querySelector("#allow_upgrades"))==null?void 0:p.checked)||!1,applies_to:e.get("applies_to")||null,requires_for:e.get("requires_for")||null};try{const s=await fetch(n,{method:o,headers:{"Content-Type":"application/json",Accept:"application/json","X-CSRF-TOKEN":w(),"X-Requested-With":"XMLHttpRequest"},body:JSON.stringify(r)});if(s.status===422){const g=await s.json();v(g.errors||{});return}const d=await s.json();d.success?(m("success",d.message),d.redirect&&setTimeout(()=>{window.location.href=d.redirect},1e3)):m("error",d.message||"Error")}catch(s){console.error("Error:",s),m("error","Promosyon kaydedilirken hata oluştu.")}}function v(t){document.querySelectorAll(".is-invalid").forEach(n=>n.classList.remove("is-invalid")),document.querySelectorAll(".invalid-feedback.js-dynamic").forEach(n=>n.remove()),Object.keys(t).forEach(n=>{const o=n.split(".")[0],e=document.querySelector(`[name="${o}"], [name="${o}[]"]`);if(e){e.classList.add("is-invalid");const r=document.createElement("div");r.className="invalid-feedback js-dynamic d-block",r.textContent=t[n][0],e.parentNode.appendChild(r)}}),m("error","Lütfen form hatalarını düzeltin.")}function m(t,n){if(window.showToast)return window.showToast(t,n);const o=document.getElementById("toast-container");if(!o){alert(n);return}const e=document.createElement("div");e.className=`toast align-items-center text-white bg-${t==="success"?"success":"danger"} border-0`,e.setAttribute("role","alert"),e.innerHTML=`<div class="d-flex"><div class="toast-body">${n}</div><button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button></div>`,o.appendChild(e),new f(e).show(),e.addEventListener("hidden.bs.toast",()=>e.remove())}
