import{M as b}from"./bootstrap.esm-Bb7dPm-H.js";import{d as h}from"./datatables-CdFC5o1t.js";import{i as y}from"./tooltip-manager-D9RaGGY4.js";import{r as g,g as _}from"./api-DZ3cXm1m.js";import{c as w,g as v}from"./datatable-filters-dR_6SwzI.js";import"jquery";import"./datatables-bbQliV7G.js";/* empty css                                  */import"./vendor-C0v32J92.js";import"./tr-gGhodeh6.js";const s=window.translations||{};var f;const c=((f=window.routes)==null?void 0:f.coupons)||{};var m;const l=((m=window.permissions)==null?void 0:m.coupons)||{},T=[{value:"",label:s.filters_all_types||"All Types"},{value:"percentage",label:s.types_percentage||"Percentage"},{value:"fixed",label:s.types_fixed||"Fixed"}],x=[{value:"",label:s.filters_all_statuses||"All Statuses"},{value:"active",label:s.statuses_active||"Active"},{value:"inactive",label:s.statuses_inactive||"Inactive"},{value:"expired",label:s.statuses_expired||"Expired"},{value:"not_started",label:s.statuses_not_started||"Not Started"},{value:"limit_reached",label:s.statuses_limit_reached||"Limit Reached"}],d=w(null,{filters:[{type:"select",id:"typeFilter",width:"140px",options:T},{type:"select",id:"statusFilter",width:"150px",options:x}],search:!0,searchPlaceholder:s.search_placeholder||"Search..."}),p=$("#couponsTable").DataTable({...h.getDefaultConfig(),serverSide:!0,dom:v(),ajax:{url:$("#couponsTable").data("records-url"),type:"GET",data:function(e){const a=d.getValues();e.type=a.typeFilter,e.status=a.statusFilter,e.search={value:a.search||""}},beforeSend:function(e){e.setRequestHeader("Authorization",`Bearer ${_()}`)},error:async function(e){e.status===401&&await g()&&p.ajax.reload()}},columns:[{data:"code",name:"code"},{data:"name",name:"name"},{data:"type",name:"type",render:function(e){return e==="percentage"?`<span class="badge bg-info">${s.types_percentage||"Percentage"}</span>`:`<span class="badge bg-warning">${s.types_fixed||"Fixed"}</span>`}},{data:"discount_value",name:"discount_value",render:function(e,a,t){return t.type==="percentage"?`%${e}`:`${t.currency_code} ${e}`}},{data:"currency_code",name:"currency_code"},{data:"usage_count",name:"usage_count",render:function(e,a,t){return t.usage_limit?`${e}/${t.usage_limit}`:`${e}/∞`}},{data:"status",name:"status",render:function(e,a,t){return`<span class="badge ${t.status_badge_class}">${e}</span>`}},{data:"starts_at",name:"starts_at",render:function(e,a,t){let n="";return t.starts_at&&(n+=`<small>${t.starts_at}</small><br>`),t.expires_at&&(n+=`<small>${t.expires_at}</small>`),n||"-"}},{data:"id",name:"actions",orderable:!1,searchable:!1,render:function(e){var o,i;const a=[],t="btn btn-sm d-inline-flex align-items-center justify-content-center",n='style="width:32px;height:32px;"';return l.view&&a.push(`<a href="${(o=c.show)==null?void 0:o.replace(":id",e)}" class="${t} btn-outline-info" ${n} data-bs-toggle="tooltip" title="${s.buttons_view||"View"}"><i class="fas fa-eye"></i></a>`),l.update&&a.push(`<a href="${(i=c.edit)==null?void 0:i.replace(":id",e)}" class="${t} btn-outline-primary ms-1" ${n} data-bs-toggle="tooltip" title="${s.buttons_edit||"Edit"}"><i class="fas fa-edit"></i></a>`),l.delete&&a.push(`<button type="button" class="${t} btn-outline-danger ms-1 delete-coupon" ${n} data-id="${e}" data-bs-toggle="tooltip" title="${s.buttons_delete||"Delete"}"><i class="fas fa-trash"></i></button>`),`<div class="d-inline-flex">${a.join("")}</div>`}}],initComplete:function(){d.init()},drawCallback:function(){y()}});d.setDataTable(p);let u=null;$(document).on("click",".delete-coupon",function(){u=$(this).data("id"),new b("#deleteModal").show()});$("#confirmDelete").on("click",async function(){var e,a,t,n,o;if(u)try{const i=await fetch((e=c.delete)==null?void 0:e.replace(":id",u),{method:"DELETE",headers:{Authorization:`Bearer ${_()}`,Accept:"application/json","Content-Type":"application/json"}});if(i.status===401&&await g())return $("#confirmDelete").trigger("click");const r=await i.json();r.success?((a=b.getInstance(document.getElementById("deleteModal")))==null||a.hide(),p.ajax.reload(),(t=window.showToast)==null||t.call(window,"success",r.message)):(n=window.showToast)==null||n.call(window,"error",r.message||"Error")}catch(i){console.error("Error:",i),(o=window.showToast)==null||o.call(window,"error","Error deleting coupon")}});
