1import{r as l,j as e,d as b,l as p,f as j}from"./index-Kbt7gc3j.js";import{D as f}from"./dollar-sign-BSzUHkI2.js";import{S as N}from"./shopping-cart-BNsgCmkW.js";import{U as y}from"./users-BTgtk_DP.js";import{P as v}from"./package-DCmZ860i.js";const i=({title:a,value:m,icon:r,color:o,loading:s})=>e.jsx("div",{className:"bg-white p-6 rounded-lg shadow-sm border border-gray-200",children:e.jsxs("div",{className:"flex justify-between items-start",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-medium text-gray-500 mb-1",children:a}),s?e.jsx("div",{className:"w-24 h-8 bg-gray-200 rounded animate-pulse mt-1"}):e.jsx("h3",{className:"text-2xl font-bold text-navy-900",children:m})]}),e.jsx("div",{className:`p-3 rounded-lg ${o}`,children:e.jsx(r,{className:"w-6 h-6 text-white"})})]})}),O=()=>{const[a,m]=l.useState(null),[r,o]=l.useState(!0),[s,n]=l.useState("checking");l.useEffect(()=>{(async()=>{o(!0),n("checking");try{const x=(p.getUser()?.role||"BUYER")==="SALESPERSON"?"/dashboard/sales":"/dashboard/stats",u=await j(x);m(u),n("connected")}catch(g){console.error("Failed to load dashboard stats from API.",g);const c=String(g?.message||"");c.includes("Not allowed by CORS")?n("disconnected"):c.includes("Unauthorized")||c.includes("401")||c.includes("auth-error")?n("unauthorized"):n("disconnected")}finally{o(!1)}})();const d=()=>n("unauthorized");return window.addEventListener("auth-error",d),()=>window.removeEventListener("auth-error",d)},[]);const h=t=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(Number(t)||0);return e.jsxs("div",{className:"space-y-8",children:[e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6",children:[e.jsx(i,{title:"Total Revenue",value:h(Number(a?.totalRevenue)||0),icon:f,color:"bg-action-600",loading:r}),e.jsx(i,{title:"Total Orders",value:(Number(a?.totalOrders)||0).toLocaleString(),icon:N,color:"bg-blue-500",loading:r}),e.jsx(i,{title:"Total Customers",value:(Number(a?.totalCustomers)||0).toLocaleString(),icon:y,color:"bg-purple-500",loading:r}),e.jsx(i,{title:"Low Stock SKUs",value:(Number(a?.lowStockCount)||0).toLocaleString(),icon:v,color:"bg-orange-500",loading:r})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-8",children:[e.jsxs("div",{className:"bg-white rounded-lg shadow-sm border border-gray-200 p-6",children:[e.jsx("h3",{className:"text-lg font-bold text-navy-900 mb-4",children:"Recent Orders"}),r?e.jsx("div",{className:"space-y-2",children:[...Array(5)].map((t,d)=>e.jsx("div",{className:"h-10 bg-gray-100 rounded animate-pulse"},d))}):e.jsxs("table",{className:"w-full text-sm text-left",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-gray-100 text-gray-500",children:[e.jsx("th",{className:"pb-3 font-medium",children:"Order ID"}),e.jsx("th",{className:"pb-3 font-medium",children:"Customer"}),e.jsx("th",{className:"pb-3 font-medium",children:"Total"}),e.jsx("th",{className:"pb-3 font-medium",children:"Status"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-100",children:a?.recentOrders&&a.recentOrders.length>0?a.recentOrders.map(t=>e.jsxs("tr",{className:"group hover:bg-gray-50",children:[e.jsx("td",{className:"py-3 font-mono text-xs text-navy-700",children:e.jsx(b,{to:"/admin/orders",className:"hover:underline",children:t.id.substring(0,8).toUpperCase()})}),e.jsx("td",{className:"py-3 font-medium text-gray-700",children:t.company?.name||t.shippingAddress?.firstName||"Guest"}),e.jsx("td",{className:"py-3 font-semibold text-navy-800",children:h(t.total)}),e.jsx("td",{className:"py-3 text-xs",children:e.jsx("span",{className:"bg-green-100 text-green-700 px-2 py-1 rounded-full",children:t.status})})]},t.id)):e.jsx("tr",{children:e.jsx("td",{colSpan:4,className:"text-center py-8 text-gray-400",children:s==="disconnected"?"Cannot fetch orders while offline.":"No recent orders found."})})})]})]}),e.jsxs("div",{className:"bg-white rounded-lg shadow-sm border border-gray-200 p-6",children:[e.jsx("h3",{className:"text-lg font-bold text-navy-900 mb-4",children:"System Health"}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-sm text-gray-600",children:"API Connectivity"}),e.jsxs("span",{className:`flex items-center gap-1.5 text-xs font-bold ${s==="connected"?"text-green-600":s==="checking"?"text-gray-500":"text-orange-500"}`,children:[e.jsx("div",{className:`w-2 h-2 rounded-full ${s==="connected"?"bg-green-500":s==="checking"?"bg-gray-400 animate-pulse":"bg-orange-500"}`}),s==="connected"?"Operational":s==="checking"?"Connecting...":s==="unauthorized"?"Login Required":"Offline"]})]}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-sm text-gray-600",children:"Database Connection"}),e.jsxs("span",{className:`flex items-center gap-1.5 text-xs font-bold ${s==="connected"?"text-green-600":s==="checking"?"text-gray-500":"text-orange-500"}`,children:[e.jsx("div",{className:`w-2 h-2 rounded-full ${s==="connected"?"bg-green-500":s==="checking"?"bg-gray-400 animate-pulse":"bg-orange-500"}`}),s==="connected"?"Active":s==="checking"?"Connecting...":s==="unauthorized"?"Login Required":"Offline"]})]}),s!=="connected"&&!r&&e.jsx("div",{className:"mt-4 p-3 bg-orange-50 border border-orange-100 rounded text-xs text-orange-700",children:s==="unauthorized"?e.jsxs(e.Fragment,{children:[e.jsx("strong",{children:"Admin Login Required:"})," Please sign in to access dashboard data."]}):e.jsxs(e.Fragment,{children:[e.jsx("strong",{children:"API Unreachable:"})," The application could not connect to the backend server. Ensure the server is running and the API URL is configured in Settings."]})})]})]})]})]})};export{O as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.