import{b as k,ek as H,a as x,iE as N,is as K,r as m,ei as M,iF as j,j as r,P as c,ej as U,T as w,iD as $,iG as J,eV as L,iH as Q,iI as X}from"./index-d2b25495.js";import{o as Y}from"./yup-267e586f.js";import{u as Z}from"./index.esm-ce3c2c20.js";import{c as B}from"./index.esm-3043d6d4.js";import{S as D}from"./SelectComponent-d286bfef.js";import{C as ee}from"./Modal-ad83750a.js";import{S as se}from"./SectionDivider-859b9192.js";const ge=()=>{var y,I,P,z,b,C,_,v,A,T;const i=k(),{id:S,tab:ae}=H(),{leadStage:{loading:F,pageParams:V,selectedData:e,selecteditems:t,modal:l,itemsList:f}}=x(s=>N(s)),{leads:{selectedData:g},system:{userAccessList:oe}}=x(s=>K(s)),{miniStages:a}=x(s=>s.mini),h=()=>{i($(!1)),i(J({})),i(j([])),u()},q=B().shape({stage:B().required("Please select stage")}),G=()=>{const s=t==null?void 0:t.filter(o=>!o.dodelete);if(!s||s.length===0){L.fire({title:'<p style="font-size:20px">No Stage Selected</p>',text:"Please select at least one stage.",icon:"warning",confirmButtonText:"Close",confirmButtonColor:"#3085d6"});return}s.forEach(o=>{var d;const n={lead_id:S||(g==null?void 0:g.id),stage_id:(d=o==null?void 0:o.stage)==null?void 0:d.id,value:""};o!=null&&o.id?i(X({data:n,params:V,id:o==null?void 0:o.id,hide:()=>h()})):i(Q({data:n,params:V,hide:()=>h()}))})},{control:O,handleSubmit:R,reset:u,getValues:re,setValue:te,watch:ne}=Z({resolver:Y(q),defaultValues:{stage:(e==null?void 0:e.stage)||null}}),W=({stage:s})=>{var d;if(!s)return;const o=Array.isArray(t)?t:[];if((d=o==null?void 0:o.filter(p=>!p.dodelete))==null?void 0:d.find(p=>{var E;return((E=p==null?void 0:p.stage)==null?void 0:E.id)===(s==null?void 0:s.id)}))L.fire({title:'<p style="font-size:20px">Stage Already Exists</p>',text:"This stage has already been added.",icon:"warning",confirmButtonText:"Close",confirmButtonColor:"#3085d6"});else{const p={stage:s};i(j([...o,p]))}u({stage:null})};return m.useEffect(()=>{console.log("Modal state:",l),l&&(console.log("Dispatching getMiniStages"),i(M({type:1})))},[l,i]),m.useEffect(()=>{l&&!(e!=null&&e.id)&&(u({stage:null}),i(j([])))},[l,e==null?void 0:e.id,u,i]),m.useEffect(()=>{l&&(e!=null&&e.id)&&u({stage:e!=null&&e.stage?{label:e==null?void 0:e.stage.name,value:Number(e==null?void 0:e.stage.id)}:null})},[l,e==null?void 0:e.id,u]),r.jsx(r.Fragment,{children:r.jsx(ee,{isOpen:l,title:e!=null&&e.id?"Update Lead Stage ":"Add Lead Stage",onClose:()=>{h()},onOK:G,okText:"Save",closeText:"",loading:F,showFooter:!0,fullWidth:!0,maxWidth:"lg",children:r.jsx("form",{style:{width:"100%"},children:r.jsxs(c,{container:!0,spacing:2,children:[r.jsx(se,{title:"Lead Stages"}),r.jsx(c,{container:!0,size:{xs:12},spacing:2,children:r.jsx(c,{size:{xs:12},children:r.jsx(D,{required:!0,name:"stage",label:"Stage",control:O,rules:{required:!0},options:((I=(y=a==null?void 0:a.list)==null?void 0:y.filter(s=>{var o;return!((o=f==null?void 0:f.filter(n=>!n.dodelete))!=null&&o.some(n=>{var d;return((d=n==null?void 0:n.stage)==null?void 0:d.id)===s.id}))}))==null?void 0:I.map(s=>({id:s.id,name:s.name})))||[],loading:a==null?void 0:a.loading,selectParams:{page:((P=a==null?void 0:a.miniParams)==null?void 0:P.page)||1,page_size:((z=a==null?void 0:a.miniParams)==null?void 0:z.page_size)||10,search:((b=a==null?void 0:a.miniParams)==null?void 0:b.search)||"",no_of_pages:((C=a==null?void 0:a.miniParams)==null?void 0:C.no_of_pages)||1,type:1},hasMore:(((_=a==null?void 0:a.miniParams)==null?void 0:_.page)||1)<(((v=a==null?void 0:a.miniParams)==null?void 0:v.no_of_pages)||1),fetchapi:M,clearData:U,onChange:()=>{setTimeout(()=>{R(W,s=>{console.log("errors",s)})()},100)}})})}),t&&((A=t==null?void 0:t.filter(s=>!s.dodelete))==null?void 0:A.length)!=0&&r.jsxs(c,{container:!0,size:{xs:12},spacing:2,sx:{border:"1px solid #e0e0e0",p:2,borderRadius:2},children:[r.jsx(c,{container:!0,size:{xs:12},spacing:2,children:r.jsx(c,{size:{xs:10},children:r.jsx(w,{variant:"h6",color:"primary",children:"Stage"})})}),(T=t==null?void 0:t.filter(s=>!s.dodelete))==null?void 0:T.map((s,o)=>{var n;return r.jsx(c,{container:!0,size:{xs:12},spacing:2,children:r.jsx(c,{size:{xs:10},children:r.jsx(w,{variant:"h6",children:((n=s==null?void 0:s.stage)==null?void 0:n.name)||"-"})})},o)})]})]})},(e==null?void 0:e.id)||"new")})})};export{ge as A};
