import{r,u as E,j as e,D as u}from"./index-DlcHsm52.js";import{F as s,g as F,C as A,B as d,a as P,T as v,I as f,S as m,b as O,r as o,s as b}from"./IdurarOs-j0IoqkpA.js";import{W as D}from"./shared-N8zi2N1N.js";import{c as p}from"./ErpApp-7VdZNpBd.js";import{c as R}from"./currencyList-mYH1Mcza.js";import{u as M}from"./useMoney-DK8w0tFT.js";import{M as K}from"./index-Clbw7Jrz.js";import{R as V}from"./ReloadOutlined-C37-gl6w.js";const W=[{value:"customer_offer",label:"Customer Offer"},{value:"lead_offer",label:"Lead Offer"},{value:"proforma",label:"Proforma"}],B=R();function J(){const[C,I]=r.useState([]),[w,S]=r.useState([]),[_,x]=r.useState(!1),[k,i]=r.useState(!1),[l]=s.useForm(),c=E(F),{moneyFormatter:N}=M(),j=p(c,"offer","create"),T=p(c,"offer","update")&&p(c,"invoice","create"),n=async()=>{x(!0);const[t,a]=await Promise.all([o.list({entity:"offer",options:{page:1,items:100}}),o.list({entity:"contact",options:{page:1,items:100}})]);I((t==null?void 0:t.result)||[]),S((a==null?void 0:a.result)||[]),x(!1)};r.useEffect(()=>{n()},[]);const q=async()=>{const t=await l.validateFields(),a=t.quantity*t.price;await o.create({entity:"offer",jsonData:{title:t.title,kind:t.kind,contact:t.contact,items:[{itemName:t.itemName,quantity:t.quantity,price:t.price,total:a}],total:a,value:{amount:a,currency:t.currency||u},notes:t.notes}}),b.success("Offer created"),i(!1),l.resetFields(),n()},g=async t=>{const a=await o.post({entity:`offer/convert/${t._id}`,jsonData:{}});a!=null&&a.success&&(b.success("Offer converted to invoice"),n())};return e.jsxs(D,{title:"Offer Workbench",description:"Manage customer offers, lead offers, and proforma drafts with invoice conversion from the same sales surface.",permission:"sales.offer.read",extra:e.jsxs(A,{children:[e.jsx(d,{icon:e.jsx(V,{}),onClick:n,children:"Refresh"}),e.jsx(d,{type:"primary",disabled:!j,onClick:()=>i(!0),children:"New Offer"})]}),children:[e.jsx(P,{rowKey:"_id",loading:_,dataSource:C,pagination:!1,columns:[{title:"Title",dataIndex:"title"},{title:"Kind",dataIndex:"kind",render:t=>e.jsx(v,{children:t})},{title:"Contact",dataIndex:["contact","name"]},{title:"Total",render:(t,a)=>{var h,y;return N({amount:((h=a.value)==null?void 0:h.amount)??a.total??0,currency_code:((y=a.value)==null?void 0:y.currency)||a.currency||u})}},{title:"Status",dataIndex:"status",render:t=>e.jsx(v,{children:t})},{title:"Action",render:(t,a)=>e.jsx(d,{size:"small",disabled:!T||a.status==="converted",onClick:()=>g(a),children:"Convert to Invoice"})}]}),e.jsx(K,{title:"New Offer",open:k,onCancel:()=>i(!1),onOk:q,okButtonProps:{disabled:!j},children:e.jsxs(s,{form:l,layout:"vertical",children:[e.jsx(s.Item,{name:"title",label:"Title",rules:[{required:!0}],children:e.jsx(f,{})}),e.jsx(s.Item,{name:"kind",label:"Kind",initialValue:"customer_offer",rules:[{required:!0}],children:e.jsx(m,{options:W})}),e.jsx(s.Item,{name:"contact",label:"Contact",children:e.jsx(m,{showSearch:!0,placeholder:"Select contact",options:w.map(t=>({value:t._id,label:t.name}))})}),e.jsx(s.Item,{name:"itemName",label:"Item",rules:[{required:!0}],children:e.jsx(f,{})}),e.jsx(s.Item,{name:"quantity",label:"Quantity",initialValue:1,rules:[{required:!0}],children:e.jsx(O,{min:1,style:{width:"100%"}})}),e.jsx(s.Item,{name:"price",label:"Price",initialValue:0,rules:[{required:!0}],children:e.jsx(O,{min:0,style:{width:"100%"}})}),e.jsx(s.Item,{name:"currency",label:"Currency",initialValue:u,children:e.jsx(m,{options:B,showSearch:!0})}),e.jsx(s.Item,{name:"notes",label:"Notes",children:e.jsx(f.TextArea,{rows:3})})]})})]})}export{J as default};
