import{r as a,j as e}from"./index-HUEYA8gO.js";function f(){const[i,u]=a.useState([]),[c,j]=a.useState(""),[N,x]=a.useState(!0),[n,o]=a.useState(!1),[p,s]=a.useState(""),r=async()=>{x(!0),s("");try{const t=c?`?status=${encodeURIComponent(c)}`:"",l=await(await fetch(`/auth-backend/admin_loans_list.php${t}`,{credentials:"include"})).json();l.success?u(l.data||[]):s(l.error||"Failed to load loans")}catch{s("Network error")}finally{x(!1)}};a.useEffect(()=>{r()},[c]);const d=async(t,h)=>{o(!0),s("");try{const m=await(await fetch("/auth-backend/admin_loan_update.php",{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({id:t,action:h})})).json();m.success?r():s(m.error||"Failed")}catch{s("Network error")}finally{o(!1)}},y=t=>e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsx("button",{onClick:()=>d(t.id,"approve"),className:"px-2 py-1 rounded bg-indigo-600 text-white text-xs",disabled:n,children:"Approve"}),e.jsx("button",{onClick:()=>d(t.id,"reject"),className:"px-2 py-1 rounded bg-red-600 text-white text-xs",disabled:n,children:"Reject"}),e.jsx("button",{onClick:()=>d(t.id,"activate"),className:"px-2 py-1 rounded bg-emerald-600 text-white text-xs",disabled:n,children:"Activate"}),e.jsx("button",{onClick:()=>d(t.id,"close"),className:"px-2 py-1 rounded bg-slate-600 text-white text-xs",disabled:n,children:"Close"})]});return e.jsxs("div",{className:"bg-slate-900 text-white rounded-2xl p-4 border border-slate-700/50",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("h2",{className:"text-xl font-semibold",children:"Loans"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("select",{value:c,onChange:t=>j(t.target.value),className:"bg-slate-800 border border-slate-700 rounded px-2 py-1 text-sm",children:[e.jsx("option",{value:"",children:"All"}),["pending","approved","rejected","active","closed"].map(t=>e.jsx("option",{value:t,children:t},t))]}),e.jsx("button",{onClick:r,className:"px-3 py-1 rounded bg-accent text-gray-900 text-sm",children:"Refresh"})]})]}),p&&e.jsx("div",{className:"mb-3 text-sm text-amber-300",children:p}),N?e.jsx("div",{className:"text-slate-300",children:"Loading..."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full text-sm",children:[e.jsx("thead",{className:"text-slate-300",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-3 py-2 text-left",children:"ID"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"User"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Email"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Amount"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Term"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"APR"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Status"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Outstanding"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Next Due"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Applied"}),e.jsx("th",{className:"px-3 py-2 text-left",children:"Actions"})]})}),e.jsx("tbody",{children:i.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:11,className:"px-3 py-4 text-slate-400",children:"No loans found."})}):i.map(t=>e.jsxs("tr",{className:"border-t border-slate-700/50",children:[e.jsx("td",{className:"px-3 py-2",children:t.id}),e.jsx("td",{className:"px-3 py-2",children:t.username}),e.jsx("td",{className:"px-3 py-2",children:t.email}),e.jsxs("td",{className:"px-3 py-2",children:["$",Number(t.amount).toLocaleString()]}),e.jsxs("td",{className:"px-3 py-2",children:[t.term_months,"m"]}),e.jsxs("td",{className:"px-3 py-2",children:[Number(t.rate_apr).toFixed(2),"%"]}),e.jsx("td",{className:"px-3 py-2",children:t.status}),e.jsxs("td",{className:"px-3 py-2",children:["$",Number(t.balance_outstanding||0).toLocaleString()]}),e.jsx("td",{className:"px-3 py-2",children:t.next_due_date||"—"}),e.jsx("td",{className:"px-3 py-2",children:t.created_at||""}),e.jsx("td",{className:"px-3 py-2",children:y(t)})]},t.id))})]})})]})}export{f as default};
