import{r as l,R as C,g as m,j as e}from"./index-HUEYA8gO.js";function w({open:a,onClose:i,onSave:j}){const[n,b]=l.useState({user:"",action:"",detail:""});if(!a)return null;function r(c){const{name:x,value:p}=c.target;b(d=>({...d,[x]:p}))}function h(c){c.preventDefault(),j({...n,time:new Date().toISOString().replace("T"," ").slice(0,16)})}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:h,children:[e.jsx("h2",{className:"text-2xl font-bold text-accent mb-2",children:"Add Log Entry"}),e.jsxs("label",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"font-semibold text-accent",children:"User/Admin"}),e.jsx("input",{name:"user",value:n.user,onChange:r,className:"input text-black",required:!0})]}),e.jsxs("label",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"font-semibold text-accent",children:"Action"}),e.jsx("input",{name:"action",value:n.action,onChange:r,className:"input text-black",required:!0})]}),e.jsxs("label",{className:"flex flex-col gap-1",children:[e.jsx("span",{className:"font-semibold text-accent",children:"Detail"}),e.jsx("input",{name:"detail",value:n.detail,onChange:r,className:"input text-black",required:!0})]}),e.jsxs("div",{className:"flex gap-4 mt-4",children:[e.jsx("button",{type:"button",onClick:i,className:"btn flex-1 btn-gray",children:"Cancel"}),e.jsx("button",{type:"submit",className:"btn flex-1 btn-accent",children:"Save"})]})]})})}function k(){const[a,i]=l.useState([]),[j,n]=l.useState(!0),[b,r]=l.useState(null);C.useEffect(()=>{n(!0),fetch(m("/get_logs.php"),{credentials:"include"}).then(t=>{if(!t.ok)throw new Error("Failed to fetch logs");return t.json()}).then(t=>{t.success?i(t.logs):r(t.error||"Unknown error"),n(!1)}).catch(t=>{r(t.message),n(!1)})},[]);const[h,c]=l.useState(!1);async function x(t,s=null){if(s===null)try{const u=await(await fetch(m("/add_log.php"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({action:t.action,details:t.detail,admin:t.user,timestamp:t.time})})).json();u.success&&i([{...t,id:u.id},...a])}catch{}else try{(await(await fetch(m("/edit_log.php"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({id:a[s].id,action:t.action,details:t.detail,admin:t.user,timestamp:t.time})})).json()).success&&i(a.map((N,f)=>f===s?{...t,id:a[s].id}:N))}catch{}c(!1),d(null)}const[p,d]=l.useState(null),[y,g]=l.useState(!1),[E,S]=l.useState(null);async function v(t){if(!window.confirm("Are you sure you want to delete this log entry?"))return;const s=a[t];try{(await(await fetch(m("/delete_log.php"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({id:s.id})})).json()).success&&i(a.filter((N,f)=>f!==t))}catch{}}return e.jsxs("section",{className:"py-8",children:[e.jsx("h2",{className:"text-3xl font-extrabold mb-6 text-accent",children:"System Logs"}),e.jsx(w,{open:h,onClose:()=>c(!1),onSave:x}),y&&e.jsx(w,{open:y,onClose:()=>{g(!1),d(null)},onSave:t=>x(t,p)}),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:"View all admin and user actions, errors, and system events."}),e.jsx("div",{className:"mb-4 flex flex-col md:flex-row gap-4",children:e.jsx("button",{className:"bg-accent text-gray-900 font-bold px-4 py-2 rounded-lg shadow hover:bg-sui transition",onClick:()=>c(!0),children:"Add Log Entry"})}),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:"Time"}),e.jsx("th",{className:"py-2 px-2",children:"User/Admin"}),e.jsx("th",{className:"py-2 px-2",children:"Action"}),e.jsx("th",{className:"py-2 px-2",children:"Detail"}),e.jsx("th",{className:"py-2 px-2",children:"Actions"})]})}),e.jsx("tbody",{children:a.map((t,s)=>e.jsxs("tr",{className:"hover:bg-accent/10",children:[e.jsx("td",{className:"py-2 px-2",children:t.time}),e.jsx("td",{className:"py-2 px-2",children:t.user}),e.jsx("td",{className:"py-2 px-2",children:t.action}),e.jsx("td",{className:"py-2 px-2",children:t.detail}),e.jsxs("td",{className:"py-2 px-2 flex gap-2",children:[e.jsx("button",{className:"bg-blue-700 text-white px-2 py-1 rounded hover:bg-blue-800 transition",onClick:()=>{d(s),g(!0),S(t)},children:"Edit"}),e.jsx("button",{className:"bg-red-700 text-white px-2 py-1 rounded hover:bg-red-800 transition",onClick:()=>v(s),children:"Delete"})]})]},t.id||s))})]})})]})]})}export{k as default};
