import"./bootstrap.esm-Bb7dPm-H.js";import{i as b}from"./tooltip-manager-D9RaGGY4.js";import{s as d}from"./toastr-helper-Wdj0mZjX.js";import{g as v}from"./api-DZ3cXm1m.js";class E{constructor(){this.init()}init(){this.initializeForm(),this.initializeTooltips()}initializeForm(){const s=document.getElementById("create-admin-form");if(!s)return;s.addEventListener("submit",n=>{n.preventDefault(),this.handleSubmit()});const e=document.getElementById("create_password"),t=document.getElementById("create_password_confirmation");e&&t&&(t.addEventListener("input",()=>{this.validatePasswordConfirmation()}),e.addEventListener("input",()=>{this.validatePasswordConfirmation()})),document.querySelectorAll('input[name="roles[]"]').forEach(n=>{n.addEventListener("change",()=>{this.updateRoleSelection()})})}validatePasswordConfirmation(){var i,a;const s=document.getElementById("create_password"),e=document.getElementById("create_password_confirmation"),t=document.getElementById("password-confirmation-feedback");if(!s||!e)return;const o=s.value,n=e.value;return n&&o!==n?(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):(n&&o===n&&(e.classList.remove("is-invalid"),e.classList.add("is-valid"),t&&(t.style.display="none")),!0)}updateRoleSelection(){const s=document.querySelectorAll('input[name="roles[]"]:checked'),e=document.getElementById("role-count-badge");if(e){const t=s.length;e.textContent=t,e.className=t>0?"badge bg-primary":"badge bg-secondary"}}async handleSubmit(){var o,n,i,a,l,m,u,f,p,w,g,h;const s=document.getElementById("create-admin-form"),e=document.getElementById("submit-btn"),t=e.innerHTML;e.disabled=!0,e.innerHTML='<span class="spinner-border spinner-border-sm me-2"></span>'+(((n=(o=window.translations)==null?void 0:o.buttons)==null?void 0:n.creating)||"Creating...");try{const c=new FormData(s),y=await fetch(s.action,{method:"POST",body:c,headers:{Authorization:`Bearer ${v()}`}}),r=await y.json();y.ok?r.success?(d("success",r.message||((a=(i=window.translations)==null?void 0:i.messages)==null?void 0:a.created_successfully)||"Admin created successfully"),window.location.href=window.routes.adminManagement.index):r.errors?(d("error",r.message||((m=(l=window.translations)==null?void 0:l.messages)==null?void 0:m.validation_error)||"Please fix form errors"),this.displayValidationErrors(r.errors)):d("error",((f=(u=window.translations)==null?void 0:u.messages)==null?void 0:f.unexpected_error)||"An unexpected error occurred"):(this.handleValidationErrors(r.errors),this.showAlert("error",r.message||((w=(p=window.translations)==null?void 0:p.messages)==null?void 0:w.validation_error)||"Please fix form errors"))}catch(c){console.error("Form submit error:",c),d("error",((h=(g=window.translations)==null?void 0:g.messages)==null?void 0:h.unexpected_error)||"An unexpected error occurred")}finally{e.disabled=!1,e.innerHTML=t}}handleValidationErrors(s){document.querySelectorAll(".is-invalid").forEach(e=>e.classList.remove("is-invalid")),document.querySelectorAll(".invalid-feedback").forEach(e=>e.style.display="none"),Object.keys(s).forEach(e=>{const t=document.getElementById("create_"+e),o=document.getElementById(e+"-feedback");t&&(t.classList.add("is-invalid"),o&&(o.textContent=s[e][0],o.style.display="block"))})}initializeTooltips(){b()}}document.addEventListener("DOMContentLoaded",()=>{new E});
