import{a as e,t}from"./react-DK-MTtD8.js";import{s as n}from"./utils-CXcsdpX-.js";import{Ci as r,Ra as i,Zo as a,aa as o,as as s,rs as c,za as l}from"./index-Ct5uU080.js";import{t as u}from"./page-content-Bh5Zxb_Y.js";import{t as d}from"./card-CCLQVS9k.js";import{t as f}from"./textarea-Ca2twCQz.js";import{t as p}from"./error-alert-eQ6ClPXs.js";import{n as m,s as h}from"./inventoryApi-Cjq7hXA7.js";var g=e(t(),1),_=n();function v(){let e=c(),[t]=s(),[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)(a,{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)(o,{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)(o,{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)(o,{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)(o,{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)(o,{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)(o,{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};