import{a as e,t}from"./react-D0JuimcS.js";import{o as n}from"./utils-Dac180Gn.js";import{Fo as r,Ki as i,Lo as a,ga as o,ha as s,ko as c,ui as l}from"./index-8M5AnKdQ.js";import{t as u}from"./page-content-DDtdeMxM.js";import{t as d}from"./card-BVFw-jgA.js";import{t as f}from"./textarea-DijfUAYL.js";import{t as p}from"./error-alert-qe7c2K0-.js";import{n as m,s as h}from"./inventoryApi-BpMmJXTM.js";var g=e(t(),1),_=n();function v(){let e=r(),[t]=a(),[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=[]}=l(),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)(o,{variant:`ghost`,size:`icon`,onClick:()=>e(-1),children:(0,_.jsx)(c,{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)(i,{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)(i,{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)(i,{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)(i,{children:`Quantity *`}),(0,_.jsx)(s,{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)(i,{children:`Date *`}),(0,_.jsx)(s,{type:`date`,value:T,onChange:e=>E(e.target.value)})]})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(i,{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)(o,{variant:`outline`,onClick:()=>e(-1),children:`Cancel`}),(0,_.jsx)(o,{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};