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