import{M as m}from"./bootstrap.esm-Bb7dPm-H.js";import{d as x}from"./datatables-CdFC5o1t.js";import{i as C}from"./tooltip-manager-D9RaGGY4.js";import{r as b,g as w}from"./api-DZ3cXm1m.js";import"jquery";import"./datatables-bbQliV7G.js";/* empty css                                  */var y;const g=((y=window.pageConfig)==null?void 0:y.routes)||{},i=window.translations||{},f=window.permissions||{};let d=null,u=null;const p=$("#categoriesTable").DataTable({...x.getDefaultConfig(),serverSide:!0,ajax:{url:$("#categoriesTable").data("records-url"),type:"GET",data:e=>{var t;e.search={value:((t=e.search)==null?void 0:t.value)||$("input[type=search]").val()||""}},beforeSend:e=>e.setRequestHeader("Authorization",`Bearer ${w()}`),error:async e=>{e.status===401&&await b()&&p.ajax.reload()}},columns:[{data:"name",name:"name"},{data:"icon",name:"icon",orderable:!1,render:e=>e?`<i class="${e}"></i> <span class="small text-muted">${e}</span>`:'<span class="text-muted">—</span>'},{data:"is_active",name:"is_active",render:e=>e?`<span class="badge bg-success">${i.status_active||"Active"}</span>`:`<span class="badge bg-secondary">${i.status_inactive||"Inactive"}</span>`},{data:"expenses_count",name:"expenses_count"},{data:"id",name:"actions",orderable:!1,searchable:!1,render:(e,t,n)=>{var r,l;const s="btn btn-sm d-inline-flex align-items-center justify-content-center",o='style="width:32px;height:32px;"',a=encodeURIComponent(JSON.stringify(n));let c="";return(r=f.expense_categories)!=null&&r.update&&(c+=`<button type="button" class="${s} btn-outline-primary edit-category" ${o} data-row="${a}" data-bs-toggle="tooltip" title="${i.buttons_edit||"Edit"}"><i class="fas fa-edit"></i></button>`),(l=f.expense_categories)!=null&&l.delete&&(c+=`<button type="button" class="${s} btn-outline-danger ms-1 delete-category" ${o} data-id="${e}" data-bs-toggle="tooltip" title="${i.buttons_delete||"Delete"}"><i class="fas fa-trash"></i></button>`),`<div class="d-inline-flex">${c}</div>`}}],drawCallback:()=>C()});function v(e=null){const t=document.getElementById("categoryForm");t.reset(),d=e?e.id:null,document.querySelector("#categoryModal .modal-title").textContent=e?i.edit_title||"Edit":i.create_title||"Create",e?(t.querySelector("[name=name]").value=e.name||"",t.querySelector("[name=icon]").value=e.icon||"",t.querySelector("[name=is_active]").checked=!!e.is_active):t.querySelector("[name=is_active]").checked=!0,new m("#categoryModal").show()}var h;(h=document.getElementById("btnAddCategory"))==null||h.addEventListener("click",e=>{e.preventDefault(),v()});$(document).on("click",".edit-category",function(){const e=JSON.parse(decodeURIComponent($(this).data("row")));v(e)});$("#saveCategory").on("click",async function(){var c,r,l;const e=document.getElementById("categoryForm"),t={name:e.querySelector("[name=name]").value,icon:e.querySelector("[name=icon]").value,is_active:e.querySelector("[name=is_active]").checked?1:0},n=d?g.update.replace(":id",d):g.store,o=await _(n,{method:d?"PUT":"POST",body:JSON.stringify(t)}),a=await o.json().catch(()=>null);if(o.ok&&(a!=null&&a.success))(c=m.getInstance(document.getElementById("categoryModal")))==null||c.hide(),p.ajax.reload(),(r=window.showToast)==null||r.call(window,"success",a.message);else{const T=a!=null&&a.errors?Object.values(a.errors).flat().join(" "):(a==null?void 0:a.message)||i.save_error;(l=window.showToast)==null||l.call(window,"error",T)}});$(document).on("click",".delete-category",function(){u=$(this).data("id"),new m("#deleteCategoryModal").show()});$("#confirmDeleteCategory").on("click",async function(){var n,s,o;if(!u)return;const e=await _(g.delete.replace(":id",u),{method:"DELETE"}),t=await e.json().catch(()=>null);(n=m.getInstance(document.getElementById("deleteCategoryModal")))==null||n.hide(),e.ok&&(t!=null&&t.success)?(p.ajax.reload(),(s=window.showToast)==null||s.call(window,"success",t.message)):(o=window.showToast)==null||o.call(window,"error",(t==null?void 0:t.message)||i.save_error),u=null});async function _(e,t={}){t.headers={Authorization:`Bearer ${w()}`,Accept:"application/json","Content-Type":"application/json",...t.headers||{}};let n=await fetch(e,t);if(n.status===401){const s=await b();s&&(t.headers.Authorization=`Bearer ${s}`,n=await fetch(e,t))}return n}
