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-CvTTLloU.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{s as m,t as h}from"./inventoryApi-CvwMhHhk.js";var g=e(t(),1),_=n();function v(){let e=c(),[t]=s(),[n,v]=(0,g.useState)(t.get(`warehouse`)||``),[y,b]=(0,g.useState)(t.get(`item`)||``),[x,S]=(0,g.useState)(`in`),[C,w]=(0,g.useState)(``),[T,E]=(0,g.useState)(``),[D,O]=(0,g.useState)(new Date().toISOString().slice(0,10)),[k,A]=(0,g.useState)(``),[j,M]=(0,g.useState)(``),{data:N=[]}=r(),P={};n&&(P.warehouse=n);let{data:F=[]}=m(n?P:void 0),[I,{isLoading:L}]=h(),R=(0,g.useMemo)(()=>F.find(e=>e.item===y),[F,y]),z=R?Number(R.quantity_on_hand):0,B=R?Number(R.average_cost):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 Adjustment`}),(0,_.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:`Record a stock adjustment (in or out)`})]})]}),(0,_.jsxs)(d,{className:`p-6 space-y-4 max-w-2xl`,children:[(0,_.jsx)(p,{message:j}),(0,_.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(o,{children:`Warehouse *`}),(0,_.jsxs)(`select`,{value:n,onChange:e=>{v(e.target.value),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 warehouse`}),N.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: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 item`}),F.map(e=>(0,_.jsxs)(`option`,{value:e.item,children:[e.item_code,` — `,e.item_description]},e.id))]}),R&&(0,_.jsxs)(`p`,{className:`text-xs text-muted-foreground`,children:[`Current qty on hand: `,(0,_.jsx)(`span`,{className:`font-mono font-semibold`,children:z.toLocaleString()})]})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(o,{children:`Adjustment Type *`}),(0,_.jsxs)(`select`,{value:x,onChange:e=>S(e.target.value),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:`in`,children:`Adjustment In`}),(0,_.jsx)(`option`,{value:`out`,children:`Adjustment Out`})]})]}),(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`})]}),x===`in`?(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(o,{children:`Unit Cost *`}),(0,_.jsx)(i,{type:`number`,min:`0`,step:`any`,value:T,onChange:e=>E(e.target.value),placeholder:`0.00`})]}):(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(o,{children:`Unit Cost (auto)`}),(0,_.jsx)(i,{type:`number`,value:B.toFixed(2),disabled:!0,className:`disabled:opacity-50`})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(o,{children:`Date *`}),(0,_.jsx)(i,{type:`date`,value:D,onChange:e=>O(e.target.value)})]})]}),(0,_.jsxs)(`div`,{className:`space-y-1.5`,children:[(0,_.jsx)(o,{children:`Remarks *`}),(0,_.jsx)(f,{value:k,onChange:e=>A(e.target.value),placeholder:`Reason for adjustment (required)`,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(M(``),!n||!y||!C||!k.trim()){M(`Please fill in all required fields.`);return}let t=Number(C);if(t<=0){M(`Quantity must be greater than zero.`);return}if(x===`out`&&t>z){M(`Insufficient stock. Available: ${z}, Requested: ${t}`);return}try{await I({warehouse:n,item:y,adjustment_type:x,quantity:t,unit_cost:x===`in`?Number(T)||0:void 0,movement_date:D,remarks:k}).unwrap(),e(`/inventory/stock-movements`)}catch(e){let t=e?.data?.detail||e?.data?.message||`Failed to create adjustment.`;M(typeof t==`string`?t:JSON.stringify(t))}},disabled:L,children:L?`Submitting...`:`Create Adjustment`})]})]})]})}export{v as default};