import{f}from"./vendor-zLyX4B24.js";import{t as u}from"./tr-CHCA6xOE.js";import{r as p,g as w}from"./api-DZ3cXm1m.js";import{i as h}from"./linkable-picker-oU28o8Zu.js";import"./datatables-Bo82YvhT.js";const c=()=>window.pageConfig||{},d=window.translations||{};document.addEventListener("DOMContentLoaded",()=>{const e=d.locale==="tr"?u.Turkish:"default";f(".expense-datepicker",{dateFormat:"Y-m-d",locale:e,allowInput:!0}),h();const t=document.getElementById("expenseForm");t==null||t.addEventListener("submit",b)});async function b(e){var a,i,l;e.preventDefault();const t=e.target;v();const s=new FormData(t);c().mode==="edit"&&s.append("_method","PUT");const o=t.querySelector('button[type="submit"]');o&&(o.disabled=!0);try{let n=await m(c().submitUrl,s);n.status===401&&await p()&&(n=await m(c().submitUrl,s));const r=await n.json().catch(()=>null);if(n.ok&&(r!=null&&r.success)){(a=window.showToast)==null||a.call(window,"success",r.message),window.location.href=r.redirect||c().indexUrl;return}n.status===422&&(r!=null&&r.errors)?y(r.errors):(i=window.showToast)==null||i.call(window,"error",(r==null?void 0:r.message)||d.save_error)}catch{(l=window.showToast)==null||l.call(window,"error",d.save_error||"Error")}finally{o&&(o.disabled=!1)}}function m(e,t){return fetch(e,{method:"POST",headers:{Authorization:`Bearer ${w()}`,Accept:"application/json"},body:t})}function v(){document.querySelectorAll("#expenseForm .is-invalid").forEach(e=>e.classList.remove("is-invalid")),document.querySelectorAll("#expenseForm .invalid-feedback.js-err").forEach(e=>e.remove())}function y(e){var t;Object.entries(e).forEach(([s,o])=>{const a=document.querySelector(`#expenseForm [name="${s}"]`);if(!a)return;a.classList.add("is-invalid");const i=document.createElement("div");i.className="invalid-feedback js-err d-block",i.textContent=Array.isArray(o)?o[0]:o,a.parentNode.appendChild(i)}),(t=window.showToast)==null||t.call(window,"error",d.save_error||"Validation error")}
