import"./bootstrap.esm-Bb7dPm-H.js";import{i as y}from"./tooltip-manager-D9RaGGY4.js";import{s as c}from"./toastr-helper-Wdj0mZjX.js";class g{constructor(){this.init()}init(){this.initializeForm(),this.initializeTooltips()}initializeForm(){const n=document.getElementById("adminForm")||document.getElementById("edit-admin-form");if(!n)return;this.form=n,n.addEventListener("submit",i=>{const a=n.querySelector('input[name="password"]'),r=n.querySelector('input[name="password_confirmation"]');a&&!this.passwordTouched&&(a.disabled=!0,r&&(r.disabled=!0))}),this.passwordTouched=!1;const e=n.querySelector('input[name="password"]');e&&(e.addEventListener("input",()=>{this.passwordTouched=!0}),e.addEventListener("keydown",()=>{this.passwordTouched=!0}));const t=document.getElementById("edit_password"),s=document.getElementById("edit_password_confirmation");t&&s&&(s.addEventListener("input",()=>{this.validatePasswordConfirmation()}),t.addEventListener("input",()=>{this.validatePasswordConfirmation()})),document.querySelectorAll('input[name="roles[]"]').forEach(i=>{i.addEventListener("change",()=>{this.updateRoleSelection()})}),this.updateRoleSelection()}validatePasswordConfirmation(){var i,a;const n=document.getElementById("edit_password"),e=document.getElementById("edit_password_confirmation"),t=document.getElementById("password-confirmation-feedback");if(!n||!e)return;const s=n.value,o=e.value;return s.length===0&&o.length===0?(e.classList.remove("is-invalid","is-valid"),t&&(t.style.display="none"),!0):o&&s!==o?(e.classList.add("is-invalid"),e.classList.remove("is-valid"),t&&(t.textContent=((a=(i=window.translations)==null?void 0:i.validation)==null?void 0:a.password_confirmation)||"Passwords do not match",t.style.display="block"),!1):(o&&s===o&&(e.classList.remove("is-invalid"),e.classList.add("is-valid"),t&&(t.style.display="none")),!0)}updateRoleSelection(){const n=document.querySelectorAll('input[name="roles[]"]:checked'),e=document.getElementById("role-count-badge");if(e){const t=n.length;e.textContent=t,e.className=t>0?"badge bg-primary":"badge bg-secondary"}}async handleSubmit(){var s,o,i,a,r,m,u,p;const n=this.form||document.getElementById("adminForm"),e=document.getElementById("submit-btn"),t=e.innerHTML;e.disabled=!0,e.innerHTML='<span class="spinner-border spinner-border-sm me-2"></span>'+(((o=(s=window.translations)==null?void 0:s.buttons)==null?void 0:o.updating)||"Updating...");try{const d=new FormData(n);d.append("_method","PUT");const f=await fetch(n.action,{method:"POST",body:d,headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content")}}),l=await f.json();f.ok?(c("success",l.message||((a=(i=window.translations)==null?void 0:i.messages)==null?void 0:a.updated_successfully)||"Admin updated successfully"),setTimeout(()=>{var w,h;window.location.href=((h=(w=window.routes)==null?void 0:w.adminManagement)==null?void 0:h.index)||"/admin/admin-management"},1500)):(this.handleValidationErrors(l.errors),c("error",l.message||((m=(r=window.translations)==null?void 0:r.messages)==null?void 0:m.validation_error)||"Please fix form errors"))}catch(d){console.error("Form submit error:",d),c("error",((p=(u=window.translations)==null?void 0:u.messages)==null?void 0:p.unexpected_error)||"An unexpected error occurred")}finally{e.disabled=!1,e.innerHTML=t}}handleValidationErrors(n){document.querySelectorAll(".is-invalid").forEach(e=>e.classList.remove("is-invalid")),document.querySelectorAll(".invalid-feedback").forEach(e=>e.style.display="none"),Object.keys(n).forEach(e=>{const t=document.getElementById("edit_"+e),s=document.getElementById(e+"-feedback");t&&(t.classList.add("is-invalid"),s&&(s.textContent=n[e][0],s.style.display="block"))})}initializeTooltips(){y()}}document.addEventListener("DOMContentLoaded",()=>{new g});
