1import{H as q,w as L,i as M,a as S,r as d,c as O,j as e,B as T,x as Q,L as l,a6 as R,h as _,I as A,b as i}from"./index-DQRZPzqR.js";import{g as H}from"./Layout-Bn407Rjh.js";import{S as $}from"./switch-Cn5Eqn5D.js";import{A as B}from"./AdminHelp-DF3w7Cim.js";import{T as E}from"./triangle-alert-3RlYCIqx.js";import{R as D}from"./refresh-cw-BeeM0BPT.js";import{A as g}from"./arrow-up-right-wRn-DXyR.js";import{S as G}from"./settings-d8P1lHlH.js";import"./chart-no-axes-column-8R6ZplEH.js";import"./users-G1WI2zrz.js";import"./mail-DfJNwXQX.js";import"./graduation-cap-CvGA7XSh.js";import"./trending-up-DOgi-3vC.js";import"./file-text-BQdsZdj4.js";import"./link-2-B3Gbj_7o.js";import"./circle-help-fA1Od6Ct.js";const K=q("Bell",[["path",{d:"M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9",key:"1qo2s2"}],["path",{d:"M10.3 21a1.94 1.94 0 0 0 3.4 0",key:"qgo35s"}]]),x=()=>({"x-admin-token":H()});function ie(){const{toast:b}=L(),f=M(),{data:s={}}=S({queryKey:["/api/site-settings"],queryFn:()=>i("GET","/api/site-settings").then(t=>t.json())}),{data:m,isLoading:o,refetch:C,dataUpdatedAt:j}=S({queryKey:["/api/admin/low-stock"],queryFn:()=>i("GET","/api/admin/low-stock",void 0,x()).then(t=>t.json()),refetchInterval:6e4}),r=m?.products??[],a=r.filter(t=>(t.stockQty??0)===0),h=r.filter(t=>(t.stockQty??0)>0),[c,N]=d.useState(""),[u,v]=d.useState(!1),[k,w]=d.useState(""),[z,n]=d.useState(!1);d.useEffect(()=>{Object.keys(s).length>0&&(N(s.low_stock_default_threshold??"5"),v(s.low_stock_email_enabled==="true"),w(s.low_stock_email_recipient??""))},[s]);const p=O({mutationFn:async()=>{await Promise.all([i("PATCH","/api/admin/site-settings/low_stock_default_threshold",{value:c},x()),i("PATCH","/api/admin/site-settings/low_stock_email_enabled",{value:String(u)},x()),i("PATCH","/api/admin/site-settings/low_stock_email_recipient",{value:k},x())])},onSuccess:()=>{f.invalidateQueries({queryKey:["/api/site-settings"]}),f.invalidateQueries({queryKey:["/api/admin/low-stock"]}),n(!1),b({title:"Stock alert settings saved"})},onError:()=>b({title:"Error saving settings",variant:"destructive"})}),y=j?new Date(j).toLocaleTimeString("en-GB"):null;return e.jsxs("div",{className:"p-6 space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2 flex-wrap",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-lg font-black uppercase tracking-tight text-foreground flex items-center gap-2",children:[e.jsx(E,{size:16,className:"text-amber-400"}),"Stock Alerts"]}),e.jsxs("p",{className:"text-xs text-muted-foreground mt-0.5",children:["Monitor low stock levels and configure alert thresholds.",y&&e.jsxs("span",{className:"ml-2 text-muted-foreground/60",children:["Last refreshed: ",y]})]})]}),e.jsx(B,{page:"stock-alerts"})]}),e.jsxs(T,{variant:"outline",size:"sm",onClick:()=>C(),className:"text-xs gap-1.5","data-testid":"button-refresh-stock",children:[e.jsx(D,{size:12})," Refresh"]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"bg-card border border-card-border rounded p-4",children:[e.jsx("p",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1",children:"Total Low Stock"}),e.jsx("p",{className:`text-2xl font-black ${r.length>0?"text-amber-400":"text-green-400"}`,children:o?"â":r.length}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"below threshold"})]}),e.jsxs("div",{className:"bg-card border border-card-border rounded p-4",children:[e.jsx("p",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1",children:"Out of Stock"}),e.jsx("p",{className:`text-2xl font-black ${a.length>0?"text-red-400":"text-green-400"}`,children:o?"â":a.length}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"zero units"})]}),e.jsxs("div",{className:"bg-card border border-card-border rounded p-4",children:[e.jsx("p",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1",children:"Global Threshold"}),e.jsx("p",{className:"text-2xl font-black text-foreground",children:m?.globalThreshold??c}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"default alert level"})]})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-6",children:[e.jsxs("div",{className:"lg:col-span-2 space-y-3",children:[a.length>0&&e.jsxs("div",{className:"bg-card border border-red-900/40 rounded overflow-hidden",children:[e.jsxs("div",{className:"px-4 py-2.5 bg-red-950/30 border-b border-red-900/40 flex items-center justify-between",children:[e.jsxs("h2",{className:"text-xs font-bold uppercase tracking-wider text-red-400 flex items-center gap-1.5",children:[e.jsx(Q,{size:12})," Out of Stock (",a.length,")"]}),e.jsxs(l,{href:"/admin/products?stock=low",className:"text-xs text-blue-400 hover:text-blue-300 flex items-center gap-1",children:["Manage ",e.jsx(g,{size:11})]})]}),e.jsx("div",{className:"divide-y divide-border",children:a.map(t=>e.jsxs(l,{href:`/admin/products/${t.id}`,className:"flex items-center justify-between px-4 py-3 hover:bg-accent/20 transition-colors","data-testid":`row-out-of-stock-${t.id}`,children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"text-sm font-medium text-foreground truncate",children:t.name}),e.jsx("p",{className:"text-xs text-muted-foreground font-mono",children:t.sku})]}),e.jsx(R,{variant:"destructive",className:"ml-3 text-xs flex-shrink-0",children:"OUT OF STOCK"})]},t.id))})]}),h.length>0&&e.jsxs("div",{className:"bg-card border border-amber-900/40 rounded overflow-hidden",children:[e.jsxs("div",{className:"px-4 py-2.5 bg-amber-950/20 border-b border-amber-900/40 flex items-center justify-between",children:[e.jsxs("h2",{className:"text-xs font-bold uppercase tracking-wider text-amber-400 flex items-center gap-1.5",children:[e.jsx(E,{size:12})," Low Stock (",h.length,")"]}),e.jsxs(l,{href:"/admin/products?stock=low",className:"text-xs text-blue-400 hover:text-blue-300 flex items-center gap-1",children:["Manage ",e.jsx(g,{size:11})]})]}),e.jsx("div",{className:"divide-y divide-border",children:h.map(t=>{const P=Math.round((t.stockQty??0)/Math.max(1,t.threshold??t.lowStockThreshold??5)*100);return e.jsxs(l,{href:`/admin/products/${t.id}`,className:"flex items-center gap-4 px-4 py-3 hover:bg-accent/20 transition-colors","data-testid":`row-low-stock-${t.id}`,children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"text-sm font-medium text-foreground truncate",children:t.name}),e.jsx("p",{className:"text-xs text-muted-foreground font-mono",children:t.sku})]}),e.jsxs("div",{className:"flex items-center gap-3 flex-shrink-0",children:[e.jsx("div",{className:"w-20",children:e.jsx("div",{className:"h-1.5 bg-muted rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full bg-amber-500 rounded-full",style:{width:`${Math.min(100,P)}%`}})})}),e.jsxs("div",{className:"text-right w-20",children:[e.jsx("span",{className:"text-sm font-bold text-amber-400",children:t.stockQty}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:[" / alert: ",t.threshold??t.lowStockThreshold]})]})]})]},t.id)})})]}),!o&&r.length===0&&e.jsxs("div",{className:"bg-card border border-card-border rounded p-8 text-center",children:[e.jsx("p",{className:"text-green-400 font-bold text-sm",children:"All products are well stocked."}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"No products are currently below their alert threshold."})]}),o&&e.jsx("div",{className:"bg-card border border-card-border rounded p-8 text-center text-sm text-muted-foreground",children:"Loading..."})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"bg-card border border-card-border rounded p-4 space-y-4",children:[e.jsxs("h2",{className:"text-xs font-bold uppercase tracking-wider flex items-center gap-1.5",children:[e.jsx(G,{size:12})," Alert Settings"]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Global Default Threshold"}),e.jsx(A,{type:"number",min:"0",value:c,onChange:t=>{N(t.target.value),n(!0)},className:"text-sm","data-testid":"input-global-threshold"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Products without a custom threshold use this value. Currently: ",m?.globalThreshold??c," units."]})]}),e.jsxs("div",{className:"border-t border-border pt-3 space-y-3",children:[e.jsxs("h3",{className:"text-xs font-semibold uppercase tracking-wider text-muted-foreground flex items-center gap-1.5",children:[e.jsx(K,{size:11})," Daily Email Digest"]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium",children:"Enable email alerts"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Sends a daily summary at 8am"})]}),e.jsx($,{checked:u,onCheckedChange:t=>{v(t),n(!0)},"data-testid":"switch-email-alerts"})]}),u&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(_,{className:"text-xs",children:"Alert recipient email"}),e.jsx(A,{type:"email",value:k,onChange:t=>{w(t.target.value),n(!0)},placeholder:"[email protected]",className:"text-sm","data-testid":"input-alert-email"})]})]}),e.jsx(T,{onClick:()=>p.mutate(),disabled:p.isPending||!z,className:"w-full bg-blue-600 hover:bg-blue-700 text-white text-xs font-bold uppercase tracking-wider","data-testid":"button-save-stock-settings",children:p.isPending?"Saving...":"Save Settings"})]}),e.jsxs("div",{className:"bg-card border border-card-border rounded p-4",children:[e.jsx("h3",{className:"text-xs font-bold uppercase tracking-wider mb-2",children:"Per-Product Threshold"}),e.jsx("p",{className:"text-xs text-muted-foreground leading-relaxed",children:"Each product can have its own alert threshold set in the product editor under the Stock section. When set to 0, the global default is used."}),e.jsxs(l,{href:"/admin/products",className:"mt-2 flex items-center gap-1 text-xs text-blue-400 hover:text-blue-300",children:["Go to Products ",e.jsx(g,{size:11})]})]})]})]})]})}export{ie 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.