import{j as e,S as I,c as E,M as L,W as N}from"./vendor-mui-CBBEb17G.js";import{B}from"./index-DQbmBtLN.js";import{a as n}from"./vendor-react-BBazpZul.js";import{at as O,J as u,jh as R,Z as F,aO as H,n as D,I as k,v as V,bL as G,x as K,dD as M,aX as U,bc as q,ao as J}from"./index-DdWwwZXu.js";import{P as Q}from"./index-DrXn43TP.js";import{S as X}from"./index-CDArk_1r.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const P=n.createContext(void 0),Z=({children:t})=>{const[r,m]=n.useState(),[h,c]=n.useState(!1),[i,s]=n.useState([]),{getWiseText:T,toggleWiseText:j,deleteWiseText:f}=R(),l=n.useRef([]),g=O(),C=()=>d(),S=o=>{s(l.current.filter(W=>W.text.includes(o)))},d=async()=>{c(!0);try{const{data:o}=await T();o&&(l.current=o,s(o))}finally{c(!1)}},p=async o=>{const{message:W}=await j(o);W&&u.success("تغییر وضعیت با موفقیت انجام شد"),d()},w=async()=>{g({content:"آیا از حذف این مورد مطمئن هستید؟"}).then(async()=>{c(!0);try{const{statusCode:o}=await f(r.id);o===200?(u.success("با موفقیت حذف شد."),d()):u.error("خطا در انجام عملیات")}finally{c(!1)}}).catch(()=>{})};n.useEffect(()=>{d()},[]);const b={states:{wiseTextData:i,loading:h,selectedWiseText:r},handlers:{toggleWiseTextState:p,setSelectedWiseText:m,refresh:C,search:S,deleteWiseContextHandler:w}};return e.jsx(P.Provider,{value:b,children:t})},v=()=>n.useContext(P),z=()=>{const{states:{selectedWiseText:t},handlers:{refresh:r,deleteWiseContextHandler:m}}=v(),{addWiseText:h,editWiseText:c}=R(),[i,s]=n.useState({editWiseText:!1,addWiseText:!1}),[T,j]=n.useState(!1),{isInRole:f}=F(),l=()=>{s({editWiseText:!1,addWiseText:!1}),p()},g=async a=>{const{wiseText:x}=a;if(x.length<20){u.error("سخن حکیمانه نباید کمتر از 20 کاراکتر باشد!");return}try{if(j(!0),i.addWiseText){const{message:y}=await h({text:x});u.success(y)}else if(i.editWiseText){const{message:y}=await c({id:t.id,text:x});u.success(y)}p(),l(),r()}finally{j(!1)}},{register:C,handleSubmit:S,formState:{errors:d},reset:p,setValue:w}=H(),b=[{title:"جدید",icon:e.jsx(k.AddIcon,{color:"success"}),onClick:()=>s(a=>({...a,addWiseText:!0})),disabled:!1,show:f(void 0,[D.WiseTextAdd])},{title:"ویرایش",icon:e.jsx(k.EditIcon,{color:t?"primary":"inherit"}),onClick:()=>s(a=>({...a,editWiseText:!0})),disabled:!t,show:f(void 0,[D.WiseTextEdit])},{title:"حذف",icon:e.jsx(k.DeleteIcon,{color:t?"error":"inherit"}),onClick:()=>m(),disabled:!t,show:f(void 0,[D.WiseTextDelete])}],o=i.editWiseText?t.text:"",W=i.editWiseText?"ویرایش سخن حکیمانه":"افزودن سخن حکیمانه",A=!t;return n.useEffect(()=>{i.editWiseText&&w("wiseText",t==null?void 0:t.text)},[i]),e.jsxs(I,{children:[(i.addWiseText||i.editWiseText)&&e.jsxs(V,{title:W,onClose:l,maxWidth:"sm",children:[e.jsx(E,{children:e.jsx(L,{item:!0,md:9,sm:12,xs:12,order:{xs:2,sm:2,md:1},children:e.jsx("form",{children:e.jsx(L,{item:!0,sm:4,xs:12,children:e.jsx(G,{name:"wiseText",register:C,validation:{required:!0},error:!!d.wiseText,fullWidth:!0,placeholder:o,multiline:!0,rows:2})})})})}),e.jsx(K,{onClose:l,onSubmit:S(g),loading:T})]}),e.jsx(I,{direction:"row",justifyContent:"start",columnGap:1,width:"100%",children:b.map((a,x)=>e.jsx(N,{title:a.disabled?"":e.jsx("h1",{className:"text-sm",children:a.title}),children:e.jsx(M,{disabled:a.disabled,onClick:()=>{!(A&&![0,1].includes(x))&&a.onClick()},children:a.icon})},x))})]})},Y=()=>{const{handlers:{refresh:t,search:r}}=v();return e.jsx(Q,{className:"!mb-2",onRefresh:t,onSearch:r,title:"سخن حکیمانه"})},_=()=>{const{states:{loading:t,wiseTextData:r},handlers:{toggleWiseTextState:m,setSelectedWiseText:h}}=v(),c=s=>{m(s)},i=[{title:"متن پیام",dataIndex:"text",key:"text",width:400,render:s=>e.jsx(q,{title:s||"-",maxLengthValue:180})},{title:"وضعیت",dataIndex:"isDelete",key:"isDelete",width:30,render:(s,T)=>e.jsx(J,{className:"w-[100px]",sx:{borderRadius:"20px !important",height:"25px !important"},color:s?"error":"success",onClick:()=>c(T.id),children:s?"غیرفعال":"فعال"})}];return e.jsx(U,{loading:t,rowKey:"id",columns:i||[],data:r||[],onClick:s=>h(s),boxProps:{height:"75vh"}})},$=()=>e.jsx(Z,{children:e.jsxs(X,{className:"!space-y-3 !px-4 !py-2 bg-white",children:[e.jsx(B,{prevRoutes:[{path:"/",title:"داشبورد"}],currentRoute:"سخن حکیمانه"}),e.jsx(Y,{}),e.jsx(z,{}),e.jsx(_,{})]})}),ce=()=>e.jsx($,{});export{ce as default};
