import{r as s,u as M,j as t}from"./index-DlcHsm52.js";import{F as o,g as R,B as c,a as B,I as V,R as L,d as x,S as h,r as l,s as u,T as N,C as $,n as q}from"./IdurarOs-j0IoqkpA.js";import{W as H}from"./shared-N8zi2N1N.js";import{c as j,b as K}from"./ErpApp-7VdZNpBd.js";import{M as Y}from"./index-Clbw7Jrz.js";import{D as z}from"./index-BPgDK0RC.js";import{R as G}from"./ReloadOutlined-C37-gl6w.js";import"./index-B1UP1jWz.js";import"./CalendarOutlined-C3QmoJqY.js";const J={open:"blue",won:"green",lost:"red"};function ne(){const[y,k]=s.useState([]),[p,S]=s.useState([]),[b,m]=s.useState(!1),[i]=o.useForm(),[C,D]=s.useState(null),[F,r]=s.useState(!1),[v,w]=s.useState(!1),d=M(R),A=j(d,"deal","update"),I=K(d,"automations.workflow.create"),f=j(d,"task","create"),n=async()=>{m(!0);const[e,a]=await Promise.all([l.list({entity:"deal",options:{page:1,items:100}}),l.list({entity:"pipelinestage",options:{page:1,items:100,sortBy:"order",sortValue:1}})]);k((e==null?void 0:e.result)||[]),S((a==null?void 0:a.result)||[]),m(!1)};s.useEffect(()=>{n()},[]);const T=s.useMemo(()=>p.map(e=>({label:e.name,value:e._id,stage:e})),[p]),P=async(e,a)=>{await l.patch({entity:`deal/${e._id}/stage`,jsonData:{stageId:a}}),u.success("Deal stage updated"),n()},U=async e=>{await l.post({entity:"workflows/deal-won",jsonData:{dealId:e._id,procurementNeeded:!1,thankYouCampaignName:`${e.name} thank-you campaign`}}),u.success("Won workflow completed"),n()},_=e=>{D(e),r(!0),i.setFieldsValue({title:`Follow up ${e.name}`,dueAt:void 0,priority:"normal"})},O=async()=>{const e=await i.validateFields();w(!0),await l.create({entity:"task",jsonData:{title:e.title,priority:e.priority,dueAt:e.dueAt?e.dueAt.toISOString():void 0,relatedTo:{entityType:"Deal",entityId:C._id}}}),u.success("Follow-up task created"),w(!1),r(!1)},W=[{title:"Deal",dataIndex:"name"},{title:"Value",dataIndex:["value","amount"]},{title:"Stage",dataIndex:["stage","name"],render:(e,a)=>{var g;return t.jsx(h,{value:(g=a.stage)==null?void 0:g._id,options:T,style:{width:180},disabled:!A,onChange:E=>P(a,E)})}},{title:"Status",dataIndex:"status",render:e=>t.jsx(N,{color:J[e]||"default",children:e})},{title:"Actions",render:(e,a)=>t.jsxs($,{children:[t.jsx(c,{icon:t.jsx(q,{}),type:"primary",disabled:!I,onClick:()=>U(a),children:"Mark Won"}),t.jsx(c,{disabled:!f,onClick:()=>_(a),children:"Create Follow-up Task"})]})}];return t.jsxs(H,{title:"Deal Pipeline",description:"Advance stages, mark deals won, generate downstream orders and invoices, and create follow-up tasks.",permission:"crm.deal.read",extra:t.jsx(c,{icon:t.jsx(G,{}),onClick:n,children:"Refresh"}),children:[t.jsx(B,{rowKey:"_id",columns:W,dataSource:y,loading:b,pagination:!1,scroll:{x:!0}}),t.jsx(Y,{title:"Create Follow-up Task",open:F,onCancel:()=>r(!1),onOk:O,confirmLoading:v,okButtonProps:{disabled:!f},children:t.jsxs(o,{form:i,layout:"vertical",children:[t.jsx(o.Item,{name:"title",label:"Task title",rules:[{required:!0,message:"Enter a title"}],children:t.jsx(V,{})}),t.jsxs(L,{gutter:12,children:[t.jsx(x,{span:12,children:t.jsx(o.Item,{name:"priority",label:"Priority",initialValue:"normal",children:t.jsx(h,{options:[{value:"low",label:"Low"},{value:"normal",label:"Normal"},{value:"high",label:"High"},{value:"urgent",label:"Urgent"}]})})}),t.jsx(x,{span:12,children:t.jsx(o.Item,{name:"dueAt",label:"Due date",children:t.jsx(z,{style:{width:"100%"}})})})]})]})})]})}export{ne as default};
