import{j as t,S as p,p as z,Y as K,Z as L,$ as M,U as N,V as x}from"./vendor-mui-CBBEb17G.js";import{a as d}from"./vendor-react-BBazpZul.js";import{u as O,at as _,aP as $,dP as F,ao as k,I as D,bI as C,J,b3 as g}from"./index-DdWwwZXu.js";import"./vendor-utils-BveVE6Uf.js";import{u as Q,e as V,D as Y}from"./index-DSw4vraS.js";import{C as Z,a as q}from"./column-B21t9gRL.js";import"./vendor-charts-tGnAgELZ.js";import"./index-DQbmBtLN.js";import"./columnTypeItems-BiaRlfri.js";const ce=({onClose:h,reportType:o,settingId:m})=>{var I,S;const T=new Y,b=new V;O();const A=_(),{actions:{setOpenStructureModal:R}}=Q(),[j,f]=d.useState(!1),[n,i]=d.useState(null),[w,U]=d.useState(1),E=async()=>{try{if(f(!0),!n)return;if(!n.title){J.warning("عنوان قالب را وارد نمایید");return}const e=n.columns.map(s=>({columnType:s.columnType,values:s.values??[],text:s.text,children:s.children.map(a=>({columnType:a.columnType,values:a.values??[],text:a.text}))}));m?await b.updateAsync({id:m,reportType:o,columns:e,title:n.title}):await b.addAsync({reportType:o,columns:e,title:n.title}),h()}finally{f(!1)}},B=e=>{const s=n==null?void 0:n.columns.findIndex(a=>a.uuId===e.uuId);s===void 0||s===-1||i(a=>({...a,columns:[...C.toUpdate(a.columns,s,e)]}))},H=e=>{const s=n==null?void 0:n.columns.findIndex(l=>l.children.some(c=>c.uuId===e.uuId));if(s===void 0||s===-1)return;const a=n==null?void 0:n.columns[s],r=a.children.findIndex(l=>l.uuId===e.uuId);r===void 0||r===-1||i(l=>({...l,columns:[...C.toUpdate(l.columns,s,{...a,children:[...C.toUpdate(a.children,r,e)]})]}))},P=async()=>{try{f(!0),await W()}finally{f(!1)}},W=async()=>{try{const{data:e}=await T.getAsync(o);if(!e){A({content:"برای این گزارش ساختاری ایجاد نشده است قبل از ایجاد قالب  می بایست ساختار گزارش تعریف شود. آیا ادامه می دهید؟"}).then(()=>{h(),R(!0)}).catch(()=>{}),h();return}const s={...e,columns:e.columns.map((l,c)=>{var u;return{...l,uuId:g(),children:(u=l.children)==null?void 0:u.map((v,y)=>({...v,uuId:g(),children:[]}))}})};if(!m){i(s);return}const{data:a}=await b.getAsync(m);if(!a){i(s);return}let r={title:a.title||e.title,reportType:e.reportType,columns:e.columns.map((l,c)=>{var u;return{...l,uuId:g(),values:a.columns[c].values,text:a.columns[c].text,children:(u=l.children)==null?void 0:u.map((v,y)=>({...v,uuId:g(),values:a.columns[c].children[y].values,text:a.columns[c].children[y].text,children:[]}))}})};i(r)}catch(e){console.error(e)}};return d.useEffect(()=>{o&&P()},[o]),d.useEffect(()=>{n&&U(n.columns.some(e=>e.children.length)?2:1)},[n]),t.jsx(p,{p:1,spacing:1,children:n?t.jsxs(p,{spacing:1,children:[t.jsx(p,{direction:"row",justifyContent:"center",children:t.jsx($,{label:"عنوان قالب",value:n.title,onChange:e=>i(s=>({...s,title:e}))})}),t.jsxs(z,{className:"!relative h-[70vh]  border rounded-lg",children:[j&&t.jsx(F,{}),t.jsx(K,{className:" !absolute top-0 bottom-0 left-0 right-0 !w-full !mb-[5px] overflow-auto rounded-lg",children:t.jsx(L,{size:"small",stickyHeader:!0,children:t.jsxs(M,{children:[t.jsxs(N,{children:[t.jsx(x,{width:120,align:"center",variant:"head",className:"!px-1 !bg-blue-500 !text-white border",rowSpan:w,sx:{minWidth:120},children:"ماه"}),(I=n.columns)==null?void 0:I.map((e,s)=>{var a,r,l;return t.jsx(x,{width:e.width||`${100/((a=n.columns)==null?void 0:a.length)}%`,align:"center",variant:"head",className:"!px-1 !bg-blue-500 !text-white border",colSpan:(r=e.children)==null?void 0:r.length,rowSpan:(l=e.children)!=null&&l.length?1:w,sx:{minWidth:150*(e.children.length||1),backgroundColor:e.backgroundColor??"#3b6df6",color:e.color??"#ffffff"},children:t.jsx(Z,{column:e,onChange:B})},e.uuId)}),t.jsx(x,{width:120,align:"center",variant:"head",className:"!px-1 !bg-blue-500 !text-white border",rowSpan:w,sx:{minWidth:120},children:"جمع"})]}),t.jsx(N,{className:"sticky top-[38px]",children:(S=n.columns)==null?void 0:S.flatMap(e=>e.children).map((e,s)=>t.jsx(x,{width:e.width,align:"center",variant:"head",className:"!px-1 border",sx:{backgroundColor:e.backgroundColor??"#475569",color:e.color??"#ffffff"},children:t.jsx(q,{child:e,onChange:H})},e.uuId))})]})})})]}),t.jsxs(p,{className:" flex !flex-row justify-center gap-2 !mt-2 ",children:[t.jsx(k,{color:"primary",onClick:()=>E(),startIcon:t.jsx(D.Save,{}),disabled:j,children:"ذخیره"}),t.jsx(k,{color:"error",onClick:()=>h(),startIcon:t.jsx(D.CloseIcon,{}),children:"بستن"})]})]}):t.jsx("h2",{className:"w-full p-8 text-center text-slate-700",children:j?"در حال دریافت اطلاعات ...":" داده ای برای نمایش وجود ندارد"})})};export{ce as default};
