document.getElementById("createForm").addEventListener("submit",async e=>{e.preventDefault();const t=document.getElementById("createError");t.hidden=!0;const n=document.getElementById("createName").value.trim(),o=document.getElementById("createPassword").value,r=document.getElementById("createConfirm").value,d=document.getElementById("createLabel").value.trim()||"Initial admin";if(!n)return t.hidden=!1,void(t.textContent="Username is required");if(!o)return t.hidden=!1,void(t.textContent="Password is required");if(o!==r)return t.hidden=!1,void(t.textContent="Passwords do not match");const i=await fetch("/api/auth/tokens",{method:"POST",headers:{"content-type":"application/json"},credentials:"same-origin",body:JSON.stringify({name:n,password:o,label:d,permissions:["*"]})}),a=await i.json().catch(()=>({}));if(!i.ok)return t.hidden=!1,void(t.textContent=a?.error?.message||a?.error||a?.data?.error||"Failed to create admin token");window.location.replace("/admin/login")}),document.getElementById("joinForm").addEventListener("submit",async e=>{e.preventDefault();const t=document.getElementById("joinError");t.hidden=!0;const n=document.getElementById("joinAddress").value.trim(),o=document.getElementById("joinToken").value.trim();if(!n)return t.hidden=!1,void(t.textContent="Remote node address is required");if(!o)return t.hidden=!1,void(t.textContent="Cluster sync token is required");const r=await fetch("/admin/setup/join",{method:"POST",headers:{"content-type":"application/json"},credentials:"same-origin",body:JSON.stringify({address:n,token:o})}),d=await r.json().catch(()=>({}));if(!r.ok)return t.hidden=!1,void(t.textContent=d?.error?.message||d?.error||d?.data?.error||"Failed to join cluster");window.location.replace("/admin/login")});