import{M as m}from"./bootstrap.esm-Bb7dPm-H.js";import{d as w}from"./datatables-I6jthWPb.js";import{i as q}from"./tooltip-manager-D9RaGGY4.js";import{r as b,g as h}from"./api-DZ3cXm1m.js";import{c as x,g as T}from"./datatable-filters-CecjJm_7.js";import"./datatables-Bo82YvhT.js";/* empty css                                  */import"./vendor-zLyX4B24.js";import"./tr-CHCA6xOE.js";const s=window.translations||{};var p;const d=((p=window.pageConfig)==null?void 0:p.routes)||{};var g;const f=((g=window.permissions)==null?void 0:g.recurring_expenses)||{},k=e=>s["freq_"+e]||e,c=x(null,{filters:[{type:"select",id:"frequencyFilter",width:"150px",options:[{value:"",label:s.filter_all_frequencies||"All frequencies"},{value:"monthly",label:s.freq_monthly||"Monthly"},{value:"quarterly",label:s.freq_quarterly||"Quarterly"},{value:"semi_annually",label:s.freq_semi_annually||"Semi-annually"},{value:"annually",label:s.freq_annually||"Annually"},{value:"biennially",label:s.freq_biennially||"Biennially"}]},{type:"select",id:"statusFilter",width:"130px",options:[{value:"",label:s.filter_all_statuses||"All Statuses"},{value:"active",label:s.status_active||"Active"},{value:"inactive",label:s.status_inactive||"Inactive"}]}],search:!0,searchPlaceholder:s.search_placeholder||"Search..."}),r=$("#recurringTable").DataTable({...w.getDefaultConfig(),serverSide:!0,dom:T(),order:[[4,"asc"]],ajax:{url:$("#recurringTable").data("records-url"),type:"GET",data:function(e){const a=c.getValues();e.frequency=a.frequencyFilter,e.status=a.statusFilter,e.search={value:a.search||""}},beforeSend:e=>e.setRequestHeader("Authorization",`Bearer ${h()}`),error:async e=>{e.status===401&&await b()&&r.ajax.reload()}},columns:[{data:"description",name:"description"},{data:"amount",name:"amount",render:(e,a,t)=>`${e} ${t.currency_code}`},{data:"frequency",name:"frequency",render:e=>`<span class="badge bg-light text-dark border">${k(e)}</span>`},{data:"category",name:"category",render:e=>e||'<span class="text-muted">—</span>'},{data:"next_run_date",name:"next_run_date"},{data:"is_active",name:"is_active",render:e=>e?`<span class="badge bg-success">${s.status_active||"Active"}</span>`:`<span class="badge bg-secondary">${s.status_inactive||"Inactive"}</span>`},{data:"linked_label",name:"linked",orderable:!1,searchable:!1,render:e=>e?`<span class="badge bg-light text-dark border"><i class="fas fa-link me-1"></i>${e}</span>`:'<span class="text-muted">—</span>'},{data:"id",name:"actions",orderable:!1,searchable:!1,render:function(e,a,t){var u;const n=[],l="btn btn-sm d-inline-flex align-items-center justify-content-center",o='style="width:32px;height:32px;"';if(f.update){const _=t.is_active?"fa-pause":"fa-play",v=t.is_active?s.toggle_deactivate||"Deactivate":s.toggle_activate||"Activate";n.push(`<button type="button" class="${l} btn-outline-secondary toggle-recurring" ${o} data-id="${e}" data-bs-toggle="tooltip" title="${v}"><i class="fas ${_}"></i></button>`),n.push(`<a href="${(u=d.edit)==null?void 0:u.replace(":id",e)}" class="${l} btn-outline-primary ms-1" ${o} data-bs-toggle="tooltip" title="${s.buttons_edit||"Edit"}"><i class="fas fa-edit"></i></a>`)}return f.delete&&n.push(`<button type="button" class="${l} btn-outline-danger ms-1 delete-recurring" ${o} data-id="${e}" data-bs-toggle="tooltip" title="${s.buttons_delete||"Delete"}"><i class="fas fa-trash"></i></button>`),`<div class="d-inline-flex">${n.join("")}</div>`}}],initComplete:()=>c.init(),drawCallback:()=>q()});c.setDataTable(r);$(document).on("click",".toggle-recurring",async function(){var n,l;const e=$(this).data("id"),a=await y(d.toggle.replace(":id",e),{method:"PATCH"}),t=await a.json().catch(()=>null);a.ok&&(t!=null&&t.success)?(r.ajax.reload(null,!1),(n=window.showToast)==null||n.call(window,"success",t.message)):(l=window.showToast)==null||l.call(window,"error",(t==null?void 0:t.message)||s.delete_error)});let i=null;$(document).on("click",".delete-recurring",function(){i=$(this).data("id"),new m("#deleteModal").show()});$("#confirmDelete").on("click",async function(){var t,n,l;if(!i)return;const e=await y(d.delete.replace(":id",i),{method:"DELETE"}),a=await e.json().catch(()=>null);(t=m.getInstance(document.getElementById("deleteModal")))==null||t.hide(),e.ok&&(a!=null&&a.success)?(r.ajax.reload(),(n=window.showToast)==null||n.call(window,"success",a.message)):(l=window.showToast)==null||l.call(window,"error",(a==null?void 0:a.message)||s.delete_error),i=null});async function y(e,a={}){a.headers={Authorization:`Bearer ${h()}`,Accept:"application/json","Content-Type":"application/json",...a.headers||{}};let t=await fetch(e,a);if(t.status===401){const n=await b();n&&(a.headers.Authorization=`Bearer ${n}`,t=await fetch(e,a))}return t}
