import{r as l,j as e,R as E,g as b}from"./index-HUEYA8gO.js";function O({open:s,onClose:d,onSave:g,initial:c,title:N,fields:x}){const[r,h]=l.useState(c||{});if(l.useEffect(()=>{h(c||{})},[c,s]),!s)return null;function u(a){const{name:o,value:f,type:p,checked:y}=a.target;h(j=>({...j,[o]:p==="checkbox"?y:f}))}function i(a){a.preventDefault(),g(r)}return e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/60",children:e.jsxs("form",{className:"bg-gray-900 rounded-2xl shadow-2xl p-8 w-full max-w-lg flex flex-col gap-4 border-2 border-accent",onSubmit:i,children:[e.jsx("h2",{className:"text-2xl font-bold text-accent mb-2",children:N}),x.map(a=>e.jsxs("label",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"font-semibold text-accent",children:a.label}),a.type==="textarea"?e.jsx("textarea",{name:a.name,value:r[a.name]||"",onChange:u,className:"input text-black",rows:2,required:a.required}):a.type==="checkbox"?e.jsx("input",{type:"checkbox",name:a.name,checked:!!r[a.name],onChange:u,className:"input"}):e.jsx("input",{type:a.type||"text",name:a.name,value:r[a.name]||"",onChange:u,className:"input text-black",required:a.required})]},a.name)),e.jsxs("div",{className:"flex gap-4 mt-4",children:[e.jsx("button",{type:"button",onClick:d,className:"btn flex-1 btn-gray",children:"Cancel"}),e.jsx("button",{type:"submit",className:"btn flex-1 btn-accent",children:"Save"})]})]})})}const q=[{name:"username",label:"Username",required:!0},{name:"role",label:"Role",required:!0},{name:"status",label:"Status",required:!0}];function _(){const[s,d]=l.useState([]),[g,c]=l.useState(!0),[N,x]=l.useState(null);E.useEffect(()=>{c(!0),fetch(b("/get_admins.php"),{credentials:"include"}).then(t=>{if(!t.ok)throw new Error("Failed to fetch admins");return t.json()}).then(t=>{t.success?d(t.admins):x(t.error||"Unknown error"),c(!1)}).catch(t=>{x(t.message),c(!1)})},[]);const[r,h]=l.useState(""),[u,i]=l.useState(!1),[a,o]=l.useState(null),[f,p]=l.useState({});function y(){o(null),p({}),i(!0)}function j(t){o(t),p(s[t]),i(!0)}async function w(t){if(a===null)try{const m=await(await fetch(b("/add_admin.php"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify(t)})).json();m.success&&d([...s,{...t,id:m.id}])}catch{}else try{(await(await fetch(b("/edit_admin.php"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({...t,id:s[a].id})})).json()).success&&d(s.map((S,v)=>v===a?{...t,id:s[a].id}:S))}catch{}i(!1),o(null)}async function k(t){if(!window.confirm("Are you sure you want to delete this admin?"))return;const n=s[t];try{(await(await fetch(b("/delete_admin.php"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({id:n.id})})).json()).success&&d(s.filter((v,C)=>C!==t))}catch{}}const A=s.filter(t=>t.username.includes(r));return e.jsxs("section",{className:"py-8",children:[e.jsx("h2",{className:"text-3xl font-extrabold mb-6 text-accent",children:"Admin Management"}),e.jsxs("div",{className:"bg-gray-900/80 rounded-2xl p-6 shadow-xl",children:[e.jsx("p",{className:"text-gray-400 mb-4",children:"Add, edit, and manage admin users and roles."}),e.jsxs("div",{className:"mb-4 flex flex-col md:flex-row gap-4",children:[e.jsx("input",{className:"flex-1 rounded-lg px-3 py-2 bg-gray-800 text-black border border-accent/40 focus:outline-accent",placeholder:"Search admins...",value:r,onChange:t=>h(t.target.value)}),e.jsx("button",{className:"bg-accent text-gray-900 font-bold px-4 py-2 rounded-lg shadow hover:bg-sui transition",onClick:y,children:"Add Admin"})]}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full divide-y divide-accent/20",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"text-accent text-left",children:[e.jsx("th",{className:"py-2 px-2",children:"Username"}),e.jsx("th",{className:"py-2 px-2",children:"Role"}),e.jsx("th",{className:"py-2 px-2",children:"Status"}),e.jsx("th",{className:"py-2 px-2",children:"Actions"})]})}),e.jsx("tbody",{children:A.map((t,n)=>e.jsxs("tr",{className:"hover:bg-accent/10",children:[e.jsxs("td",{className:"py-2 px-2 font-semibold text-accent",children:["@",t.username]}),e.jsx("td",{className:"py-2 px-2",children:t.role}),e.jsx("td",{className:"py-2 px-2",children:t.status}),e.jsxs("td",{className:"py-2 px-2 flex gap-2",children:[e.jsx("button",{className:"bg-sui text-black px-2 py-1 rounded hover:bg-accent/80 transition",onClick:()=>j(n),children:"Edit"}),e.jsx("button",{className:"bg-red-500 text-black px-2 py-1 rounded hover:bg-red-600 transition",onClick:()=>k(n),children:"Delete"})]})]},n))})]})})]}),e.jsx(O,{open:u,onClose:()=>{i(!1),o(null)},onSave:w,initial:f,title:a===null?"Add Admin":"Edit Admin",fields:q})]})}export{_ as default};
