import{a as E}from"./bootstrap.esm-Bb7dPm-H.js";let p=0;document.addEventListener("DOMContentLoaded",function(){b(),h()});function b(){document.getElementById("type").addEventListener("change",function(){var e;const t=document.getElementById("maxDiscountGroup"),i=document.getElementById("discount_unit"),r=document.getElementById("currency_id"),n=document.querySelector(".currency-required-star");this.value==="percentage"?(t.style.display="block",i.textContent="%",r.removeAttribute("required"),n&&(n.style.display="none")):(t.style.display="none",i.textContent=((e=window.formData.currencies.find(c=>c.id==document.getElementById("currency_id").value))==null?void 0:e.code)||"",r.setAttribute("required","required"),n&&(n.style.display=""))}),document.getElementById("currency_id").addEventListener("change",function(){const t=document.getElementById("discount_unit");if(document.getElementById("type").value==="fixed"){const r=window.formData.currencies.find(n=>n.id==this.value);t.textContent=r?r.code:""}}),document.getElementById("target_type").addEventListener("change",function(){const t=document.getElementById("targetProductsGroup"),i=document.getElementById("targetGroupsGroup"),r=document.getElementById("targetTldsGroup");t.style.display="none",i.style.display="none",r.style.display="none",this.value==="specific_products"?t.style.display="block":this.value==="specific_groups"?i.style.display="block":this.value==="specific_tlds"&&(r.style.display="block")});const o=document.getElementById("target_type").value;o==="specific_products"?document.getElementById("targetProductsGroup").style.display="block":o==="specific_groups"?document.getElementById("targetGroupsGroup").style.display="block":o==="specific_tlds"&&(document.getElementById("targetTldsGroup").style.display="block")}function h(){document.getElementById("addCriterion").addEventListener("click",function(){I()}),document.getElementById("couponForm").addEventListener("submit",function(o){o.preventDefault(),L()}),document.addEventListener("change",function(o){o.target.classList.contains("criterion-type")&&f(o.target)}),document.addEventListener("click",function(o){o.target.closest(".remove-criterion")&&o.target.closest(".criterion-item").remove()})}function I(){const o=document.getElementById("criteriaContainer"),i=document.getElementById("criterionTemplate").innerHTML.replace(/INDEX/g,p),r=document.createElement("div");r.innerHTML=i;const n=r.firstElementChild;o.appendChild(n);const e=n.querySelector(".criterion-type");f(e),p++}function f(o){var c;const i=o.closest(".criterion-item").querySelector(".criterion-values"),r=o.value;let n="";switch(r){case"product":n=l("products","products","Ürün seçin");break;case"product_group":n=l("productGroups","product_groups","Ürün grubu seçin");break;case"tld":n=l("tlds","tlds","TLD seçin");break;case"country":n=l("countries","countries","Ülke seçin");break;case"currency":n=l("currencies","currencies","Para birimi seçin");break;case"user_type":n=l("userTypes","user_types","Kullanıcı tipi seçin");break;case"order_type":n=l("orderTypes","order_types","Sipariş tipi seçin");break;case"order_amount":n=w();break;case"registration_date":n=k();break;default:n='<input type="text" class="form-control" name="criteria[INDEX][criteria_value][]" placeholder="'+(((c=window.translations)==null?void 0:c.common_placeholders_enter_value)||"Enter value")+'">'}i.innerHTML=n,i.querySelectorAll("input, select").forEach(a=>{a.name=a.name.replace("INDEX",p-1)})}function l(o,t,i){const r=window.formData[o]||[];let n=`<option value="">${i}</option>`;return r.forEach(e=>{let c,a;typeof e=="object"&&e!==null?e.tld?(c=e.tld,a=e.tld):e.common_name&&e.a2_iso?(c=`${e.common_name} (${e.a2_iso})`,a=e.a2_iso):e.code&&e.name?(c=`${e.name} (${e.code})`,a=e.code):o==="currencies"?(c=`${e.code} - ${e.name}`,a=e.id):e.name&&e.id?(c=e.name,a=e.id):e.id?(c=e.name||e.title||e.code||`Item ${e.id}`,a=e.id):(c=e.name||e.code||e.title||JSON.stringify(e),a=e.id||e.code||e):(c=e,a=e),n+=`<option value="${a}">${c}</option>`}),`<select class="form-select" name="criteria[INDEX][criteria_value][]" multiple size="5">${n}</select>`}function w(o){var r,n;const t=((r=window.translations)==null?void 0:r.common_min)||"Min",i=((n=window.translations)==null?void 0:n.common_max)||"Max";return`
        <div class="row">
            <div class="col-6">
                <input type="number" class="form-control" name="criteria[INDEX][criteria_value][]" placeholder="${t}" step="0.01">
            </div>
            <div class="col-6">
                <input type="number" class="form-control" name="criteria[INDEX][criteria_value][]" placeholder="${i}" step="0.01">
            </div>
        </div>
    `}function k(o){var r,n;const t=((r=window.translations)==null?void 0:r.common_start_date)||"Start",i=((n=window.translations)==null?void 0:n.common_end_date)||"End";return`
        <div class="row">
            <div class="col-6">
                <input type="date" class="form-control" name="criteria[INDEX][criteria_value][]" placeholder="${t}">
            </div>
            <div class="col-6">
                <input type="date" class="form-control" name="criteria[INDEX][criteria_value][]" placeholder="${i}">
            </div>
        </div>
    `}async function L(){try{const o=document.getElementById("couponForm"),t=new FormData(o),i={code:t.get("code"),name:t.get("name"),description:t.get("description"),type:t.get("type"),discount_value:t.get("discount_value"),currency_id:t.get("currency_id"),min_order_amount:t.get("min_order_amount")||null,max_discount_amount:t.get("max_discount_amount")||null,usage_limit:t.get("usage_limit")||null,is_active:!!t.get("is_active"),starts_at:t.get("starts_at")||null,expires_at:t.get("expires_at")||null,target_type:t.get("target_type")||"all",target_products:t.getAll("target_products[]").filter(c=>c),target_groups:t.getAll("target_groups[]").filter(c=>c),target_tlds:t.getAll("target_tlds[]").filter(c=>c),criteria:[]};document.querySelectorAll(".criterion-item").forEach((c,a)=>{const m=c.querySelector(".criterion-type").value,g=c.querySelector(".criterion-operator").value,v=c.querySelector('input[name*="is_required"]').checked,_=c.querySelector('input[name*="order"]').value||0;if(m){const d={criteria_type:m,criteria_operator:g,is_required:v,order:parseInt(_),criteria_value:[]};c.querySelectorAll('input[name*="criteria_value"], select[name*="criteria_value"]').forEach(s=>{s.type==="checkbox"?s.checked&&d.criteria_value.push(s.value):s.type==="select-multiple"?Array.from(s.selectedOptions).forEach(y=>{y.value&&d.criteria_value.push(y.value)}):s.value&&d.criteria_value.push(s.value)}),d.criteria_value.length>0&&i.criteria.push(d)}});const e=await(await fetch(window.routes.coupons.store,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})).json();e.success?(u("success",e.message),e.redirect&&setTimeout(()=>{window.location.href=e.redirect},1500)):e.errors?B(e.errors):u("error",e.message||"Error creating coupon.")}catch(o){console.error("Error:",o),u("error","Kupon oluşturulurken hata oluştu.")}}function B(o){document.querySelectorAll(".is-invalid").forEach(t=>{t.classList.remove("is-invalid")}),document.querySelectorAll(".invalid-feedback").forEach(t=>{t.remove()}),Object.keys(o).forEach(t=>{const i=document.querySelector(`[name="${t}"]`);if(i){i.classList.add("is-invalid");const r=document.createElement("div");r.className="invalid-feedback",r.textContent=o[t][0],i.parentNode.appendChild(r)}}),u("error","Lütfen form hatalarını düzeltin.")}function u(o,t){const i=document.getElementById("toast-container");if(!i)return;const r=document.createElement("div");r.className=`toast align-items-center text-white bg-${o==="success"?"success":"danger"} border-0`,r.setAttribute("role","alert"),r.setAttribute("aria-live","assertive"),r.setAttribute("aria-atomic","true"),r.innerHTML=`
        <div class="d-flex">
            <div class="toast-body">
                ${t}
            </div>
            <button type="button" class="btn-btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
    `,i.appendChild(r),new E(r).show(),r.addEventListener("hidden.bs.toast",function(){r.remove()})}
