import{j as e,S as m,c as N,M as W}from"./vendor-mui-CBBEb17G.js";import{P as q}from"./index-DrXn43TP.js";import{at as G,aO as O,Z as V,n as k,dD as I,c4 as X,et as K,bx as U,v as Y,bL as L,x as Z,aX as J,z as Q,gE as C,al as S}from"./index-DdWwwZXu.js";import{a as o}from"./vendor-react-BBazpZul.js";import{b as R}from"./index-DhzNyzZF.js";import{B as _}from"./index-DQbmBtLN.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const D=o.createContext(void 0),$=({children:s})=>{const{getBookLetInsuranceTypesAsync:a}=R(),[c,t]=o.useState(!1),[r,n]=o.useState([]),[h,p]=o.useState(),x=async i=>{t(!0);try{const{data:l}=await a(i);n(l)}catch(l){console.log(l)}finally{t(!1)}},g={loading:c,BookLetInsuranceTypesList:r,getData:x,search:i=>{x(i)},rowSelected:h,setRowSelected:p};return e.jsx(D.Provider,{value:g,children:s})},b=()=>o.useContext(D),ee=()=>{const{deleteBookLetInsuranceTypeAsync:s,addBookLetInsuranceType:a,editBookLetInsuranceTypeAsync:c}=R(),{rowSelected:t,getData:r,setRowSelected:n}=b(),h=G(),{register:p,handleSubmit:x,formState:{errors:f},reset:g,setValue:i}=O(),[l,d]=o.useState(!1),[j,B]=o.useState(!1),[w,T]=o.useState(!1),v=()=>{B(!1),d(!0),g()},A=()=>{t&&(B(!0),d(!0),i("title",t==null?void 0:t.title),i("comment",t==null?void 0:t.comment))},P=()=>{t&&h({content:"آیا از حذف این آیتم مطمئن هستید؟"}).then(async()=>{await s(t.id),r(),n(void 0)}).catch(()=>{})},E=async u=>{try{T(!0);const{title:z,comment:F}=u,y={title:z,comment:F};j?(await c({...y,id:t==null?void 0:t.id}),n({...y,id:t==null?void 0:t.id})):await a(y),d(!1),r(),g()}finally{T(!1)}},{isInRole:H}=V(),M=[{item:e.jsx(I,{onClick:v,children:e.jsx(X,{fontSize:"large",color:"success"})}),expectedRoles:k.AddBookLetInsuranceType},{item:e.jsx(I,{onClick:A,children:e.jsx(K,{fontSize:"large",color:t?"primary":"disabled"})}),expectedRoles:k.EditBookLetInsuranceType},{item:e.jsx(I,{onClick:P,children:e.jsx(U,{fontSize:"large",color:t?"error":"disabled"})}),expectedRoles:k.DeleteBookLetInsuranceType}];return e.jsxs(e.Fragment,{children:[e.jsx(m,{direction:"row",paddingY:1,spacing:1,children:M.filter(u=>H(void 0,[u.expectedRoles])===!0).map(u=>u.item)}),l&&e.jsxs(Y,{title:j?"ویرایش نوع دفترچه":"افزودن نوع دفترچه",maxWidth:"xs",onClose:()=>d(!1),dialogProps:{classes:{paper:"pb-2"}},children:[e.jsx(N,{className:"!pb-2",children:e.jsxs(m,{component:"form",paddingX:0,marginBottom:0,alignItems:"center",children:[e.jsx(m,{width:"100%",children:e.jsx(L,{fullWidth:!0,name:"title",register:p,validation:{required:!0},error:!!f.title,labelText:"عنوان"})}),e.jsx(m,{width:"100%",children:e.jsx(L,{fullWidth:!0,name:"comment",register:p,validation:{required:!1},error:!!f.comment,labelText:"توضیحات"})})]})}),e.jsx(Z,{onClose:()=>d(!1),onSubmit:x(E),loading:w})]})]})},te=[{title:"کد",dataIndex:"id",key:"id",width:50,render:s=>Q(s)},{title:"عنوان",dataIndex:"title",key:"title",width:90,render:s=>s?C(s):"-"},{title:"توضیحات",dataIndex:"comment",key:"comment",width:130,render:s=>s?C(s):"-"}],se=()=>{const{BookLetInsuranceTypesList:s,getData:a,loading:c,setRowSelected:t}=b();o.useEffect(()=>{a()},[]);const r=n=>{t(n)};return e.jsx(J,{loading:c,rowKey:"id",columns:te||[],data:s||[],onClick:n=>r(n),boxProps:{height:"75vh"}})},oe=S(W)`
 box-shadow: 0 3px 20px rgb(0 0 0 / 4%);
  background-color: rgb(255, 255, 255);
  padding: 10px 20px 10px;
  min-height: 100%;

  .MuiDataGrid-cell {
    cursor: pointer !important;
  }
`;S(m)`
  margin-bottom: 0;

  .btc-icons {
    background: rgba(51, 83, 178, 0.04);
    min-width: 49px;
    margin: 0 5px;
  }
`;const ne=()=>{const{getData:s,search:a}=b();return e.jsxs(oe,{children:[e.jsx(_,{prevRoutes:[{path:"/",title:"داشبورد"}],currentRoute:"انواع دفترچه بیمه"}),e.jsx(q,{onRefresh:s,onSearch:a,title:"انواع دفترچه بیمه"}),e.jsx(ee,{}),e.jsx(se,{})]})},pe=()=>e.jsx($,{children:e.jsx(ne,{})});export{pe as default};
