import{r as n,u as E,j as t,D as d}from"./index-DlcHsm52.js";import{F as a,g as F,C as k,B as y,a as v,T as R,S as u,I as x,b as h,r as l,s as j}from"./IdurarOs-j0IoqkpA.js";import{W as P}from"./shared-N8zi2N1N.js";import{c as b}from"./ErpApp-7VdZNpBd.js";import{c as U}from"./currencyList-mYH1Mcza.js";import{u as D}from"./useMoney-DK8w0tFT.js";import{M}from"./index-Clbw7Jrz.js";import{R as W}from"./ReloadOutlined-C37-gl6w.js";const L=["draft","confirmed","processing","delivered","returned","refunded","cancelled"],V=U(),B={draft:"default",confirmed:"blue",processing:"gold",delivered:"green",returned:"orange",refunded:"purple",cancelled:"red"};function X(){const[O,S]=n.useState([]),[C,g]=n.useState([]),[I,o]=n.useState(!1),[w,m]=n.useState(!1),[c]=a.useForm(),p=E(F),{moneyFormatter:T}=D(),N=b(p,"order","create"),_=b(p,"order","update"),i=async()=>{m(!0);const[e,r]=await Promise.all([l.list({entity:"order",options:{page:1,items:100}}),l.list({entity:"client",options:{page:1,items:100}})]);S((e==null?void 0:e.result)||[]),g((r==null?void 0:r.result)||[]),m(!1)};n.useEffect(()=>{i()},[]);const q=async()=>{const e=await c.validateFields(),r=e.quantity*e.price;await l.create({entity:"order",jsonData:{client:e.client,number:e.number,items:[{itemName:e.itemName,quantity:e.quantity,unitPrice:e.price,total:r}],total:{amount:r,currency:e.currency||d},status:"draft"}}),j.success("Order created"),o(!1),c.resetFields(),i()},A=async(e,r)=>{const s=await l.patch({entity:`order/${e._id}/status`,jsonData:{status:r}});s!=null&&s.success&&(j.success("Order status updated"),i())};return t.jsxs(P,{title:"Order Workbench",description:"Track enterprise order lifecycle states from confirmation through fulfillment and returns.",permission:"inventory.order.read",extra:t.jsxs(k,{children:[t.jsx(y,{icon:t.jsx(W,{}),onClick:i,children:"Refresh"}),t.jsx(y,{type:"primary",disabled:!N,onClick:()=>o(!0),children:"New Order"})]}),children:[t.jsx(v,{rowKey:"_id",loading:w,dataSource:O,pagination:!1,columns:[{title:"Number",dataIndex:"number"},{title:"Client",dataIndex:["client","name"]},{title:"Total",render:(e,r)=>{var s,f;return T({amount:((s=r.total)==null?void 0:s.amount)||r.total||0,currency_code:((f=r.total)==null?void 0:f.currency)||r.currency||d})}},{title:"Status",dataIndex:"status",render:e=>t.jsx(R,{color:B[e]||"default",children:e||"draft"})},{title:"Action",render:(e,r)=>t.jsx(u,{size:"small",value:r.status,disabled:!_,style:{width:150},onChange:s=>A(r,s),options:L.map(s=>({value:s,label:s}))})}]}),t.jsx(M,{title:"New Order",open:I,onCancel:()=>o(!1),onOk:q,children:t.jsxs(a,{form:c,layout:"vertical",children:[t.jsx(a.Item,{name:"client",label:"Client",rules:[{required:!0}],children:t.jsx(u,{options:C.map(e=>({value:e._id,label:e.name}))})}),t.jsx(a.Item,{name:"number",label:"Number",rules:[{required:!0}],children:t.jsx(x,{})}),t.jsx(a.Item,{name:"itemName",label:"Item",rules:[{required:!0}],children:t.jsx(x,{})}),t.jsx(a.Item,{name:"quantity",label:"Quantity",initialValue:1,rules:[{required:!0}],children:t.jsx(h,{min:1,style:{width:"100%"}})}),t.jsx(a.Item,{name:"price",label:"Price",initialValue:0,rules:[{required:!0}],children:t.jsx(h,{min:0,style:{width:"100%"}})}),t.jsx(a.Item,{name:"currency",label:"Currency",initialValue:d,children:t.jsx(u,{options:V,showSearch:!0})})]})})]})}export{X as default};
