1import{a as Ye,u as Ge,r as i,b as A,c as S,j as e,W as we,L as j,B as n,s as Se,T as Xe,U as Ze,V as ke,$ as et,_ as te,I as m}from"./index-BRJ5ZK5v.js";import{B as qe}from"./badge-B3_-or2v.js";import{C as _}from"./card-CDridMaN.js";import{D as M,a as D,b as R,c as U}from"./dialog-BBwNo65S.js";import{L as p}from"./label-DRpmBi18.js";import{S as Q,a as K,b as V,c as z,d as h}from"./select-CmaxMc0N.js";import{S as tt}from"./SupplierPhoto-DbFhYw6I.js";import{D as st}from"./download-CJ-w3lRy.js";import{M as at}from"./mail-CU6x-rbT.js";import{P as Ce}from"./plus-BykrFEEd.js";import{C as it}from"./circle-check-Bz6lbHkK.js";import{E as rt}from"./external-link-CcpSZQ96.js";import{T as nt}from"./trash-2-DtAeGN4W.js";import"./index-3cz39GOp.js";import"./index-BdQq_4o_.js";import"./index-CHcawZx6.js";import"./index-DMe0aH8K.js";import"./check-D8iPEp1e.js";import"./chevron-up-8zgptxCn.js";import"./uploadFile-BPtLl6vF.js";import"./image-plus-QylmL3Xd.js";function At(){const f=Ye(),{toast:r}=Ge(),[O,Ae]=i.useState(""),[k,Oe]=i.useState(()=>{const t=localStorage.getItem("supplyOrdersStatusFilter")||"all";return t==="pending"?"need_to_order":t});function Pe(t){Oe(t),localStorage.setItem("supplyOrdersStatusFilter",t)}const[Ee,J]=i.useState(!1),[P,se]=i.useState(""),[ae,ie]=i.useState(1),[H,W]=i.useState(""),[re,Y]=i.useState(""),[ne,de]=i.useState(""),[le,oe]=i.useState(""),[ce,ue]=i.useState(""),[x,me]=i.useState(new Set),[he,pe]=i.useState(!1),{data:N=[],isLoading:Le}=A({queryKey:["/api/supply-orders"],queryFn:()=>fetch("/api/supply-orders").then(t=>t.json())}),{data:Te=[]}=A({queryKey:["/api/supply-orders/square-exemptions"],queryFn:async()=>{const a=await(await fetch("/api/supply-orders/square-exemptions")).json().catch(()=>[]);return Array.isArray(a)?a:[]}});function xe(t){return Te.some(a=>a.kind==="item"&&a.value===t.itemName.trim().toLowerCase()||a.kind==="supplier"&&!!t.supplierName&&a.value===t.supplierName.trim().toLowerCase())}const{data:G=[]}=A({queryKey:["/api/suppliers"],queryFn:()=>fetch("/api/suppliers").then(t=>t.json())}),{data:E=[]}=A({queryKey:["/api/supply-orders/mislinked"],queryFn:async()=>{const t=await fetch("/api/supply-orders/mislinked");if(!t.ok)return[];const a=await t.json().catch(()=>[]);return Array.isArray(a)?a:[]},staleTime:6e4}),X=S({mutationFn:async t=>{const a=await fetch("/api/supply-orders",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(!a.ok)throw new Error("Failed to create order");return a.json()},onSuccess:()=>{f.invalidateQueries({queryKey:["/api/supply-orders"]}),r({title:"Order added"}),je()}}),ge=S({mutationFn:async({id:t,...a})=>{const s=await fetch(`/api/supply-orders/${t}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(!s.ok)throw new Error("Failed to update order");return s.json()},onSuccess:t=>{f.invalidateQueries({queryKey:["/api/supply-orders"]}),t?.squareApplied?r({title:"Square inventory updated",description:t.squareApplied}):t?.squareApplyError?r({title:"Order updated, but Square wasn't",description:`Square inventory update failed: ${t.squareApplyError}. Adjust the count in Square manually.`,variant:"destructive"}):r({title:"Order updated"})}}),$e=S({mutationFn:t=>fetch(`/api/supply-orders/${t}`,{method:"DELETE"}),onSuccess:()=>{f.invalidateQueries({queryKey:["/api/supply-orders"]}),r({title:"Order deleted"})}});function je(){J(!1),se(""),ie(1),W(""),Y(""),de(""),oe(""),ue("")}function Be(){P.trim()&&X.mutate({itemName:P.trim(),quantity:ae,supplierName:H.trim()||null,supplierUrl:re.trim()||null,notes:ne.trim()||null,category:le.trim()||null,orderedBy:ce.trim()||null,status:"need_to_order"})}const l=S({mutationFn:async({id:t,linkProductId:a})=>{const s=await fetch(`/api/supply-orders/${t}/add-to-square`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a?{linkProductId:a}:{})}),u=await s.json().catch(()=>null);if(!s.ok)throw new Error(u?.message||"Could not add to Square");return u},onSuccess:t=>{f.invalidateQueries({queryKey:["/api/supply-orders"]}),v(null),B(null),t?.squareApplied?r({title:t?.created?"Added to Square":"Linked to existing Square item",description:`${t?.created?"Catalog item created and ":""}${t.squareApplied} added to inventory.`}):t?.squareApplyError?r({title:"Added to Square catalog, but stock wasn't updated",description:t.squareApplyError,variant:"destructive"}):r({title:"Added to Square catalog"})},onError:t=>{r({title:"Couldn't add to Square",description:t.message,variant:"destructive"})}}),L=S({mutationFn:async({id:t,linkProductId:a,variationName:s})=>{const u=await fetch(`/api/supply-orders/${t}/add-to-square`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({linkProductId:a,...s?{variationName:s}:{}})}),I=await u.json().catch(()=>null);if(!u.ok)throw new Error(I?.message||"Couldn't link the label");return I},onSuccess:(t,a)=>{f.invalidateQueries({queryKey:["/api/supply-orders"]});const s=d;C(null),g(""),r({title:"Label linked",description:"Reference only â Square inventory was not changed."}),s?.receiveAfter&&F(s.order)},onError:t=>{r({title:"Couldn't link the label",description:t.message,variant:"destructive"})}});async function Fe(t){ve(t.id);try{const a=await fetch(`/api/supply-orders/${t.id}/square-match`),s=await a.json().catch(()=>null);if(!a.ok)throw new Error(s?.message||"Couldn't check Square");Z(s?.match?{id:s.match.id,name:s.match.name}:null),T(""),C({order:t,suggestion:s?.match||null,parsedName:s?.parsedName||null,receiveAfter:!1})}catch(a){r({title:"Couldn't check Square",description:a?.message,variant:"destructive"})}finally{ve(null)}}const q=S({mutationFn:async({order:t,kind:a})=>{const s=a==="supplier"?t.supplierName:t.itemName;if(!(await fetch("/api/supply-orders/square-exemptions",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({kind:a,value:s})}
1)).ok)throw new Error("Couldn't save the preference");return{order:t,kind:a,value:s}},onSuccess:({order:t,kind:a,value:s})=>{f.invalidateQueries({queryKey:["/api/supply-orders/square-exemptions"]}),F(t),v(null),r({title:"Got it â won't ask again",description:a==="supplier"?`Anything from ${s} stays out of Square.`:`"${s}" stays out of Square.`})},onError:t=>{r({title:"Couldn't save",description:t.message,variant:"destructive"})}}),[o,v]=i.useState(null),[y,g]=i.useState(""),[d,C]=i.useState(null),[b,Z]=i.useState(null),[fe,T]=i.useState(""),[Ne,ve]=i.useState(null);function Ie(t){return/\blabels?\b/i.test(t.itemName)}const{data:ee}=A({queryKey:["/api/square/products"],enabled:!!o||!!d}),$=i.useMemo(()=>{const t=y.trim().toLowerCase();return!t||t.length<2?[]:(ee?.items||[]).filter(a=>!a.isArchived&&a.name.toLowerCase().includes(t)).slice(0,8)},[ee,y]),[c,B]=i.useState(null);function F(t){ge.mutate({id:t.id,status:"received",completedAt:new Date().toISOString()})}const[_e,ye]=i.useState(null);async function Me(t){ye(t.id);try{const a=await fetch(`/api/supply-orders/${t.id}/square-match`),s=await a.json().catch(()=>null);if(!a.ok){r({title:"Couldn't check Square",description:s?.message||"Try marking it received again in a moment.",variant:"destructive"});return}s?.referenceOnly?(Z(s?.match?{id:s.match.id,name:s.match.name}:null),T(""),C({order:t,suggestion:s?.match||null,parsedName:s?.parsedName||null,receiveAfter:!0})):s?.match?.kind==="item"?l.mutate({id:t.id,linkProductId:s.match.id}):s?.match?.kind==="variation"?B({order:t,match:s.match}):v(t)}catch{r({title:"Couldn't check Square",description:"Try marking it received again in a moment.",variant:"destructive"})}finally{ye(null)}}function De(t,a){if(a==="received"&&!t.squareProductId&&!xe(t)){Me(t);return}const s={id:t.id,status:a};a==="received"?s.completedAt=new Date().toISOString():s.completedAt=null,ge.mutate(s)}function Re(t){W(t);const a=G.find(s=>s.name===t);a?.website&&Y(a.website)}const w=i.useMemo(()=>{let t=N;return k!=="all"&&(t=t.filter(a=>k==="need_to_order"?a.status==="need_to_order"||a.status==="pending":a.status===k)),O&&(t=t.filter(a=>a.itemName.toLowerCase().includes(O.toLowerCase()))),t},[N,O,k]);function Ue(t){me(a=>{const s=new Set(a);return s.has(t)?s.delete(t):s.add(t),s})}const be=w.length>0&&w.every(t=>x.has(t.id));function Qe(){me(t=>{const a=new Set(t);if(be)for(const s of w)a.delete(s.id);else for(const s of w)a.add(s.id);return a})}async function Ke(){if(x.size){pe(!0);try{const t=await fetch(`/api/supply-orders/square-po-export?ids=${Array.from(x).join(",")}`);if(!t.ok){const I=await t.json().catch(()=>null);throw new Error(I?.message||"Export failed")}const a=await t.blob(),s=URL.createObjectURL(a),u=document.createElement("a");u.href=s,u.download="square-purchase-order.csv",u.click(),URL.revokeObjectURL(s),r({title:"File downloaded",description:"In Square, go to Items & inventory â Purchase orders â Import and upload this file."})}catch(t){r({title:"Couldn't create the file",description:t.message,variant:"destructive"})}finally{pe(!1)}}}const Ve=N.filter(t=>t.status==="need_to_order"||t.status==="pending").length,ze=N.filter(t=>t.status==="ordered").length,Je=N.filter(t=>t.status==="received").length;function He(t){return t==="need_to_order"||t==="pending"?"Need to Order":t}function We(t){switch(t){case"need_to_order":case"pending":return"bg-red-100 text-red-800 border-red-300";case"ordered":return"bg-blue-100 text-blue-800 border-blue-300";case"received":return"bg-green-100 text-green-800 border-green-300";default:return"bg-gray-100 text-gray-800 border-gray-300"}}return Le?e.jsx(we,{children:e.jsx("div",{className:"flex justify-center py-20",children:e.jsx(j,{className:"h-8 w-8 animate-spin text-primary"})})}):e.jsxs(we,{children:[e.jsxs("div",{className:"max-w-[1400px]",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row justify-between items-start sm:items-center mb-8 gap-4",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-serif text-[#2B4650] mb-2","data-testid":"text-supply-orders-title",children:"Supply Orders"}),e.jsx("p",{className:"text-muted-foreground",children:"Track and manage supply orders from vendors."})]}),e.jsxs("div",{className:"flex gap-2 flex-wrap",children:[e.jsxs(n,{variant:"outline",onClick:Ke,disabled:!x.size||he,"data-testid":"button-square-po-export",children:[he?e.jsx(j,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(st,{className:"h-4 w-4 mr-2"}),"Square PO file",x.size?` (${x.size})`:""]}),e.jsx(Se,{href:"/admin/suppliers",children:e.jsxs(n,{variant:"outline","data-testid":"button-go-suppliers",children:[e.jsx(Xe,{className:"h-4 w-4 mr-2"})," Suppliers"]})}),e.jsx(Se,{href:"/admin/scan-invoice",children:e.jsxs(n,{variant:"outline","data-testid":"button-go-gmail-receipt",children:[e.jsx(at,{className:"h-4 w-4 mr-2"})," Email Receipts"]})}),e.jsxs(n,{className:"bg-[#708238] hover:bg-[#5d6d2e] text-white",onClick:()=>J(!0),"data-testid":"button-add-order",children:[e.jsx(Ce,{className:"h-4 w-4 mr-2"})," Add Order"]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4 mb-6",children:[e.jsx(_,{className:"p-4 border-l-4 border-l-[#2B4650]","data-testid":"card-total-orders",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Ze,{className:"h-8 w-8 text-[#2B4650]"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Total Orders"}),e.jsx("p",{className:"text-2xl font-bold text-[#2B4650]","data-testid":"text-total-orders",children:N.length})]})]})}),e.jsx(_,{className:"p-4 border-l-4 border-l-[#B03A2E]","data-testid":"card-need-to-order-orders",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(ke,{className:"h-8 w-8 text-[#B03A2E]"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Need to Order"}),e.jsx("p",{className:"text-2xl font-bold text-[#B03A2E]","data-testid":"text-need-to-order-orders",children:Ve})]})]})}),e.jsx(_,{className:"p-4 border-l-4 border-l-[#2B4650]","data-testid":"card-ordered-orders",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(ke,{className:"h-8 w-8 text-[#2B4650]"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Ordered"}),e.jsx("p",{className:"text-2xl font-bold text-[#2B4650]","data-testid":"text-ordered-orders",children:ze})]})]})}),e.jsx(_,{className:"p-4 border-l-4 border-l-[#708238]","data-testid":"card-received-orders",children:e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(it,{className:"h-8 w-8 text-[#708238]"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Received"}),e.jsx("p",{className:"text-2xl font-bold text-[#708238]","data-testid":"text-received-orders",children:Je})]})]})})]}),E.length>0&&e.jsx("div",{className:"mb-6 rounded-lg border border-amber-300 bg-amber-50 p-4",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(et,{className:"h-5 w-5 text-amber-600 mt-0.5 shrink-0"}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("p",{className:"font-medium text-amber-800",children:[E.length," supply order",E.length!==1?"s are":" is"," linked to a retail Square product instead of a raw material."]}),e.jsxs("p",{className:"text-sm text-amber-700 mt-1",children:["These are usually fragrance oils or supplies whose name matches a retail product. The server will clear them on the next restart. To fix immediately, use the API: ",e.jsx("code",{className:"bg-amber-100 px-1 rounded text-xs",children:"PATCH /api/supply-orders/{id}/unlink-square"})]}),e.jsx("ul",{className:"mt-2 space-y-1",children:E.map(t=>e.jsxs("li",{className:"text-sm text-amber-800",children:[e.jsxs("span",{className:"font-medium",children:["#",t.id," ",t.itemName]}),t.supplierName&&e.jsxs("span",{className:"text-amber-600",children:[" (",t.supplierName,")"]})," ","â linked to retail product ",e.jsxs("span",{className:"font-medium",children:['"',t.linkedProductName,'"']}),t.linkedProductCategory&&e.jsxs("span",{className:"text-amber-600",children:[" [",t.linkedProductCategory,"]"]})]},t.id))})]})]})}),e.jsxs("div",{className:"flex gap-3 mb-6",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(te,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(m,{placeholder:"Search by item name...",value:O,onChange:t=>Ae(t.target.value),className:"pl-10","data-testid":"input-search-orders"})]}),e.jsxs(Q,{value:k,onValueChange:Pe,children:[e.jsx(K,{className:"w-[170px]","data-testid":"select-status-filter",children:e.jsx(V,{placeholder:"All statuses"})}),e.jsxs(z,{children:[e.jsx(h,{value:"all","data-testid":"option-filter-all",children:"All statuses"}),e.jsx(h,{value:"need_to_order","data-testid":"option-filter-need-to-order",children:"Need to Order"}),e.jsx(h,{value:"ordered","data-testid":"option-filter-ordered",children:"Ordered"}),e.jsx(h,{value:"received","data-testid":"option-filter-received",children:"Received"})]})]})]}),e.jsx("div",{className:"bg-white rounded-lg border shadow-sm overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-[#ECE9EA]/50",children:[e.jsx("th",{className:"p-3 w-8",children:e.jsx("input",{type:"checkbox",checked:be,onChange:Qe,className:"h-4 w-4 accent-[#708238]","data-testid":"checkbox-select-all"})}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Item Name"}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Qty"}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Supplier"}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Status"}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Ordered By"}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Date Added"}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Notes"}),e.jsx("th",{className:"text-left p-3 font-medium text-[#2B4650]",children:"Actions"})]})}),e.jsx("tbody",{children:w.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:9,className:"text-center py-12 text-muted-foreground",children:'No supply orders found. Click "Add Order" to create one.'})}):w.map(t=>
1e.jsxs("tr",{className:"border-b hover:bg-[#ECE9EA]/20 transition-colors","data-testid":`row-order-${t.id}`,children:[e.jsx("td",{className:"p-3",children:e.jsx("input",{type:"checkbox",checked:x.has(t.id),onChange:()=>Ue(t.id),className:"h-4 w-4 accent-[#708238]","data-testid":`checkbox-select-${t.id}`})}),e.jsx("td",{className:"p-3 font-medium","data-testid":`text-item-name-${t.id}`,children:e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[t.imageUrl&&e.jsx(tt,{src:t.imageUrl,alt:t.itemName}),e.jsx("span",{children:t.itemName}),!t.squareProductId&&!xe(t)&&e.jsx(qe,{className:"bg-amber-100 text-amber-800 border-amber-300 text-[10px]","data-testid":`badge-not-in-square-${t.id}`,children:"Not in Square"}),!t.squareProductId&&Ie(t)&&e.jsxs(n,{variant:"outline",size:"sm",className:"h-6 px-2 text-[11px]",disabled:Ne===t.id,onClick:()=>Fe(t),"data-testid":`button-match-label-${t.id}`,children:[Ne===t.id?e.jsx(j,{className:"h-3 w-3 animate-spin mr-1"}):null,"Match label"]})]})}),e.jsx("td",{className:"p-3","data-testid":`text-quantity-${t.id}`,children:t.quantity}),e.jsx("td",{className:"p-3","data-testid":`text-supplier-${t.id}`,children:t.supplierName||"â"}),e.jsx("td",{className:"p-3",children:e.jsxs(Q,{value:t.status,onValueChange:a=>De(t,a),disabled:_e===t.id,"data-testid":`select-status-${t.id}`,children:[e.jsx(K,{className:"w-[130px] h-8","data-testid":`select-status-trigger-${t.id}`,children:e.jsx(V,{children:e.jsx(qe,{className:`${We(t.status)} text-xs`,"data-testid":`badge-status-${t.id}`,children:He(t.status)})})}),e.jsxs(z,{children:[e.jsx(h,{value:"need_to_order","data-testid":`option-status-need-to-order-${t.id}`,children:"Need to Order"}),e.jsx(h,{value:"ordered","data-testid":`option-status-ordered-${t.id}`,children:"Ordered"}),e.jsx(h,{value:"received","data-testid":`option-status-received-${t.id}`,children:"Received"})]})]})}),e.jsx("td",{className:"p-3 text-muted-foreground","data-testid":`text-ordered-by-${t.id}`,children:t.orderedBy||"â"}),e.jsx("td",{className:"p-3 text-muted-foreground","data-testid":`text-date-${t.id}`,children:t.createdAt?new Date(t.createdAt).toLocaleDateString():"â"}),e.jsx("td",{className:"p-3 text-muted-foreground max-w-[200px] truncate","data-testid":`text-notes-${t.id}`,children:t.notes||"â"}),e.jsx("td",{className:"p-3",children:e.jsxs("div",{className:"flex items-center gap-1",children:[t.supplierUrl&&e.jsx(n,{variant:"ghost",size:"icon",className:"h-8 w-8",onClick:()=>window.open(t.supplierUrl,"_blank"),"data-testid":`button-link-${t.id}`,children:e.jsx(rt,{className:"h-4 w-4 text-[#708238]"})}),e.jsx(n,{variant:"ghost",size:"icon",className:"h-8 w-8 text-red-500 hover:text-red-700 hover:bg-red-50",onClick:()=>$e.mutate(t.id),"data-testid":`button-delete-${t.id}`,children:e.jsx(nt,{className:"h-4 w-4"})})]})})]},t.id))})]})})]}),e.jsx(M,{open:!!o,onOpenChange:t=>{t||(v(null),g(""))},children:e.jsxs(D,{className:"max-w-md","data-testid":"dialog-add-to-square",children:[e.jsx(R,{children:e.jsx(U,{children:"This item isn't in Square yet"})}),o&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[e.jsx("span",{className:"font-medium text-foreground",children:o.itemName})," isn't linked to anything in your Square catalog. Search your existing Square items below and pick the right one â the received quantity (",o.quantity,") will be added to its inventory. New items are never created automatically."]}),e.jsxs("div",{children:[e.jsxs("div",{className:"relative",children:[e.jsx(te,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(m,{placeholder:"Search your Square catalogâ¦",className:"pl-10",value:y,onChange:t=>g(t.target.value),"data-testid":"input-link-search"})]}),y.trim().length>=2&&e.jsx("div",{className:"mt-2 max-h-48 overflow-y-auto rounded-md border divide-y",children:$.length===0?e.jsx("p",{className:"p-3 text-sm text-muted-foreground",children:"No Square items match that search."}):$.map(t=>e.jsxs("button",{className:"w-full text-left px-3 py-2 text-sm hover:bg-gray-50 disabled:opacity-50",disabled:l.isPending,onClick:()=>l.mutate({id:o.id,linkProductId:t.id}),"data-testid":`link-result-${t.id}`,children:[e.jsx("span",{className:"font-medium",children:t.name}),t.category&&e.jsxs("span",{className:"text-muted-foreground",children:[" â ",t.category]})]},t.id))}),l.isPending&&e.jsxs("p",{className:"mt-2 text-sm text-muted-foreground flex items-center",children:[e.jsx(j,{className:"h-4 w-4 animate-spin mr-2"})," Linkingâ¦"]})]}),e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsx(n,{variant:"outline",onClick:()=>{F(o),v(null)},disabled:l.isPending||q.isPending,"data-testid":"button-receive-without-square",children:"Just mark received (skip Square this time)"}),e.jsx(n,{variant:"outline",onClick:()=>q.mutate({order:o,kind:"item"}),disabled:l.isPending||q.isPending,"data-testid":"button-exempt-item",children:"This item is never in Square â don't ask again"}),o.supplierName&&e.jsxs(n,{variant:"outline",onClick:()=>q.mutate({order:o,kind:"supplier"}),disabled:l.isPending||q.isPending,"data-testid":"button-exempt-supplier",children:["Nothing from ",o.supplierName," goes in Square"]})]})]})]})}),e.jsx(M,{open:!!d,onOpenChange:t=>{t||(C(null),g(""))},children:e.jsxs(D,{className:"max-w-md","data-testid":"dialog-label-link",children:[e.jsx(R,{children:e.jsx(U,{children:"Which product is this label for?"})}),d&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[e.jsx("span",{className:"font-medium text-foreground",children:d.order.itemName}
1)," ","is a label order. Linking it just records which product the labels belong to â"," ",e.jsx("span",{className:"font-medium text-foreground",children:"Square inventory is never changed"}),"."]}),b?e.jsxs("div",{className:"rounded-md border p-3 space-y-3",children:[e.jsxs("p",{className:"text-sm",children:[d.suggestion&&b.id===d.suggestion.id?"Suggested match: ":"Selected: ",e.jsx("span",{className:"font-medium",children:b.name})]}),(()=>{const a=((ee?.items||[]).find(s=>s.id===b.id)?.variations||[]||[]).filter(s=>(s.name||"").trim());return a.length<2?null:e.jsxs("div",{className:"space-y-1",children:[e.jsx(p,{className:"text-xs",children:"Size / variant (optional)"}),e.jsxs(Q,{value:fe||"none",onValueChange:s=>T(s==="none"?"":s),children:[e.jsx(K,{className:"h-8","data-testid":"select-label-variation",children:e.jsx(V,{placeholder:"Pick a variant"})}),e.jsxs(z,{children:[e.jsx(h,{value:"none",children:"Whole product"}),a.map(s=>e.jsx(h,{value:s.name,children:s.name},s.id))]})]})]})})(),e.jsxs(n,{className:"w-full bg-[#2B4650] hover:bg-[#1E323A] text-white",disabled:L.isPending,onClick:()=>L.mutate({id:d.order.id,linkProductId:b.id,variationName:fe||void 0}),"data-testid":"button-confirm-label-link",children:[L.isPending?e.jsx(j,{className:"h-4 w-4 animate-spin mr-2"}):null,"Link label to ",b.name]})]}):e.jsxs("p",{className:"text-sm text-muted-foreground",children:["No confident match was found",d.parsedName?e.jsxs(e.Fragment,{children:[' for "',e.jsx("span",{className:"font-medium text-foreground",children:d.parsedName}),'"']}):null,". Search your Square catalog below."]}),e.jsxs("div",{children:[e.jsxs("div",{className:"relative",children:[e.jsx(te,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(m,{placeholder:"Search for a different productâ¦",className:"pl-10",value:y,onChange:t=>g(t.target.value),"data-testid":"input-label-link-search"})]}),y.trim().length>=2&&e.jsx("div",{className:"mt-2 max-h-48 overflow-y-auto rounded-md border divide-y",children:$.length===0?e.jsx("p",{className:"p-3 text-sm text-muted-foreground",children:"No Square items match that search."}):$.map(t=>e.jsxs("button",{className:"w-full text-left px-3 py-2 text-sm hover:bg-gray-50",onClick:()=>{Z({id:t.id,name:t.name}),T(""),g("")},"data-testid":`label-link-result-${t.id}`,children:[e.jsx("span",{className:"font-medium",children:t.name}),t.category&&e.jsxs("span",{className:"text-muted-foreground",children:[" â ",t.category]})]},t.id))})]}),d.receiveAfter&&e.jsx(n,{variant:"outline",className:"w-full",disabled:L.isPending,onClick:()=>{F(d.order),C(null),g("")},"data-testid":"button-receive-label-without-link",children:"Just mark received (link it later)"})]})]})}),e.jsx(M,{open:!!c,onOpenChange:t=>!t&&B(null),children:e.jsxs(D,{className:"max-w-md","data-testid":"dialog-variant-match",children:[e.jsx(R,{children:e.jsx(U,{children:"Is this the same item?"})}),c&&e.jsxs("div",{className:"space-y-4 mt-2",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:['"',e.jsx("span",{className:"font-medium text-foreground",children:c.order.itemName}),'" matches the'," ",e.jsx("span",{className:"font-medium text-foreground",children:c.match.matchedVariation||c.order.itemName})," ","variation of"," ",e.jsx("span",{className:"font-medium text-foreground",children:c.match.name})," in Square. Should the received stock go there, or is this a different item that needs its own spot in Square?"]}),e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsxs(n,{onClick:()=>l.mutate({id:c.order.id,linkProductId:c.match.id}),disabled:l.isPending,className:"bg-[#2B4650] hover:bg-[#1E323A] text-white","data-testid":"button-link-variant",children:[l.isPending?e.jsx(j,{className:"h-4 w-4 animate-spin mr-2"}):null,"Yes â add stock to ",c.match.name]}),e.jsx(n,{variant:"outline",onClick:()=>{const t=c.order;B(null),v(t)},disabled:l.isPending,"data-testid":"button-not-variant",children:"No â this is a different item"})]})]})]})}),e.jsx(M,{open:Ee,onOpenChange:J,children:e.jsxs(D,{className:"max-w-lg","data-testid":"dialog-add-order",children:[e.jsx(R,{children:e.jsx(U,{className:"text-[#2B4650]",children:"Add Supply Order"})}
1),e.jsxs("div",{className:"space-y-4 mt-2",children:[e.jsxs("div",{children:[e.jsx(p,{htmlFor:"itemName",children:"Item Name *"}),e.jsx(m,{id:"itemName",value:P,onChange:t=>se(t.target.value),placeholder:"e.g., 8oz Candle Jars","data-testid":"input-item-name"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx(p,{htmlFor:"quantity",children:"Quantity"}),e.jsx(m,{id:"quantity",type:"number",min:1,value:ae,onChange:t=>ie(parseInt(t.target.value)||1),"data-testid":"input-quantity"})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"category",children:"Category"}),e.jsx(m,{id:"category",value:le,onChange:t=>oe(t.target.value),placeholder:"e.g., Packaging","data-testid":"input-category"})]})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"supplierName",children:"Supplier"}),G.length>0?e.jsxs(Q,{value:H,onValueChange:Re,children:[e.jsx(K,{"data-testid":"select-supplier",children:e.jsx(V,{placeholder:"Select a supplier"})}),e.jsx(z,{children:G.map(t=>e.jsx(h,{value:t.name,"data-testid":`option-supplier-${t.id}`,children:t.name},t.id))})]}):e.jsx(m,{id:"supplierName",value:H,onChange:t=>W(t.target.value),placeholder:"e.g., Candle Science","data-testid":"input-supplier-name"})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"supplierUrl",children:"Supplier URL"}),e.jsx(m,{id:"supplierUrl",value:re,onChange:t=>Y(t.target.value),placeholder:"https://www.candlescience.com/...","data-testid":"input-supplier-url"})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"orderedBy",children:"Ordered By"}),e.jsx(m,{id:"orderedBy",value:ce,onChange:t=>ue(t.target.value),placeholder:"Your name","data-testid":"input-ordered-by"})]}),e.jsxs("div",{children:[e.jsx(p,{htmlFor:"notes",children:"Notes"}),e.jsx(m,{id:"notes",value:ne,onChange:t=>de(t.target.value),placeholder:"Any additional notes...","data-testid":"input-notes"})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[e.jsx(n,{variant:"outline",onClick:je,"data-testid":"button-cancel-order",children:"Cancel"}),e.jsxs(n,{className:"bg-[#708238] hover:bg-[#5d6d2e] text-white",onClick:Be,disabled:!P.trim()||X.isPending,"data-testid":"button-submit-order",children:[X.isPending?e.jsx(j,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(Ce,{className:"h-4 w-4 mr-2"}),"Add Order"]})]})]})]})})]})}export{At 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.