import{s as i}from"./toastr-helper-Wdj0mZjX.js";import{g as u,r as l}from"./api-DZ3cXm1m.js";function m(){document.querySelectorAll(".is-invalid").forEach(t=>{t.classList.remove("is-invalid")}),document.querySelectorAll(".invalid-feedback").forEach(t=>{t.textContent=""})}function f(t){Object.keys(t).forEach(o=>{const n=document.getElementById(`edit_${o}`)||document.querySelector(`[name="${o}"]`);if(n){n.classList.add("is-invalid");const r=n.parentNode.querySelector(".invalid-feedback");r&&(r.textContent=t[o][0])}})}function p(t){var c;t.preventDefault();const o=document.getElementById("rackForm"),n=o.querySelector('button[type="submit"]')||document.querySelector(`button[type="submit"][form="${o.id}"]`),r=new FormData;r.append("_method","PUT"),["datacenter_id","name","row","total_u","notes"].forEach(e=>{const s=document.getElementById(`edit_${e}`);s&&r.append(e,s.value)});const d=n.innerHTML;n.disabled=!0,n.innerHTML='<i class="fas fa-spinner fa-spin me-2"></i>'+(((c=window.translations.messages)==null?void 0:c.loading)||"Loading..."),m();const a=e=>({method:"POST",headers:{Accept:"application/json",Authorization:`Bearer ${e}`,"X-Requested-With":"XMLHttpRequest"},body:r});(async()=>{let e=await fetch(window.routes.racks.update,a(u()));if(e.status===401){const s=await l();s&&(e=await fetch(window.routes.racks.update,a(s)))}return e})().then(e=>e.json()).then(e=>{e.success?(i("success",e.message),setTimeout(()=>{window.location.href=window.routes.racks.index},1500)):e.errors?f(e.errors):i("danger",e.message||"An error occurred.")}).catch(e=>{console.error("Error:",e),i("danger","An error occurred.")}).finally(()=>{n.disabled=!1,n.innerHTML=d})}document.addEventListener("DOMContentLoaded",function(){const t=document.getElementById("rackForm");t&&t.addEventListener("submit",p)});
