import{i as e}from"./rolldown-runtime-Dd_uD5pT.js";import{t}from"./react-BatatxyT.js";import{s as n}from"./utils-BO1RPuol.js";import{Ci as r,Ra as i,aa as a,cs as o,es as s,os as c,za as l}from"./index-B4NicEaS.js";import{t as u}from"./page-content-DdXVZwik.js";import{t as d}from"./card-CBaLHc_f.js";import{t as f}from"./textarea-BD25PH-h.js";import{t as p}from"./error-alert-b_a_jMDN.js";import{n as m,s as h}from"./inventoryApi-qXXV7NNc.js";var g=e(t(),1),_=n();function v(){let e=c(),[t]=o(),[n,v]=(0,g.useState)(t.get(`source_warehouse`)||``),[y,b]=(0,g.useState)(``),[x,S]=(0,g.useState)(t.get(`item`)||``),[C,w]=(0,g.useState)(``),[T,E]=(0,g.useState)(new Date().toISOString().slice(0,10)),[D,O]=(0,g.useState)(``),[k,A]=(0,g.useState)(``),{data:j=[]}=r(),M={};n&&(M.warehouse=n);let{data:N=[]}=h(n?M:void 0),[P,{isLoading:F}]=m(),I=(0,g.useMemo)(()=>j.filter(e=>String(e.id)!==n),[j,n]),L=(0,g.useMemo)(()=>N.find(e=>e.item===x),[N,x]),R=L?Number(L.quantity_on_hand):0;return(0,_.jsxs)(u,{children:[(0,_.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,_.jsx)(l,{variant:`ghost`,size:`icon`,onClick:()=>e(-1),children:(0,_.jsx)(s,{className:`h-4 w-4`})}),(0,_.jsxs)(`div`,{children:[(0,_.jsx)(`h1`,{className:`text-xl font-semibold`,children:`New Stock Transfer`}),(0,_.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:`Transfer stock between warehouses`})]})]}),(0,_.jsxs)(d,{className:`p-6 space-y-4 max-w-2xl`,children:[(0,_.jsx)(p,{message:k}),(0,_.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(a,{children:`Source Warehouse *`}),(0,_.jsxs)(`select`,{value:n,onChange:e=>{v(e.target.value),S(``),b(``)},className:`flex h-9 w-full rounded-md border border-input bg-background px-3 py-1 text-sm shadow-sm focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring`,children:[(0,_.jsx)(`option`,{value:``,children:`Select source warehouse`}),j.map(e=>(0,_.jsx)(`option`,{value:e.id,children:e.name},e.id))]})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(a,{children:`Destination Warehouse *`}),(0,_.jsxs)(`select`,{value:y,onChange:e=>b(e.target.value),disabled:!n,className:`flex h-9 w-full rounded-md border border-input bg-background px-3 py-1 text-sm shadow-sm focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50`,children:[(0,_.jsx)(`option`,{value:``,children:`Select destination warehouse`}),I.map(e=>(0,_.jsx)(`option`,{value:e.id,children:e.name},e.id))]})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(a,{children:`Item *`}),(0,_.jsxs)(`select`,{value:x,onChange:e=>S(e.target.value),disabled:!n,className:`flex h-9 w-full rounded-md border border-input bg-background px-3 py-1 text-sm shadow-sm focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50`,children:[(0,_.jsx)(`option`,{value:``,children:`Select item`}),N.map(e=>(0,_.jsxs)(`option`,{value:e.item,children:[e.item_code,` — `,e.item_description]},e.id))]}),L&&(0,_.jsxs)(`p`,{className:`text-xs text-muted-foreground`,children:[`Available: `,(0,_.jsx)(`span`,{className:`font-mono font-semibold`,children:R.toLocaleString()})]})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(a,{children:`Quantity *`}),(0,_.jsx)(i,{type:`number`,min:`0`,step:`any`,value:C,onChange:e=>w(e.target.value),placeholder:`0`})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(a,{children:`Date *`}),(0,_.jsx)(i,{type:`date`,value:T,onChange:e=>E(e.target.value)})]})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(a,{children:`Remarks`}),(0,_.jsx)(f,{value:D,onChange:e=>O(e.target.value),placeholder:`Optional notes`,rows:3})]}),(0,_.jsxs)(`div`,{className:`flex justify-end gap-2 pt-2`,children:[(0,_.jsx)(l,{variant:`outline`,onClick:()=>e(-1),children:`Cancel`}),(0,_.jsx)(l,{onClick:async()=>{if(A(``),!n||!y||!x||!C){A(`Please fill in all required fields.`);return}let t=Number(C);if(t<=0){A(`Quantity must be greater than zero.`);return}if(t>R){A(`Insufficient stock. Available: ${R}, Requested: ${t}`);return}try{await P({source_warehouse:n,destination_warehouse:y,item:x,quantity:t,movement_date:T,remarks:D}).unwrap(),e(`/inventory/stock-movements`)}catch(e){let t=e?.data?.detail||e?.data?.message||`Failed to create transfer.`;A(typeof t==`string`?t:JSON.stringify(t))}},disabled:F,children:F?`Submitting...`:`Create Transfer`})]})]})]})}export{v as default};