import{r,j as t}from"./index-DlcHsm52.js";import{F as j,C as W,B as F,a as _,I as C,S as E,b as f,r as w,s as B}from"./IdurarOs-j0IoqkpA.js";import{W as D}from"./shared-N8zi2N1N.js";import{R as N}from"./ReloadOutlined-C37-gl6w.js";import{M as $}from"./index-Clbw7Jrz.js";import{S as A}from"./ErpApp-7VdZNpBd.js";import{D as L}from"./index-BPgDK0RC.js";const z=(e,o)=>{if(e.options)return e.options;if(!e.resourceKey)return[];const n=o[e.resourceKey]||[],i=e.optionLabel||"name",l=e.optionValue||"_id";return n.map(a=>({value:a[l],label:a[i]||a.name||a.title||a.email||a._id}))},G=(e,o)=>{switch(e.type){case"select":return t.jsx(E,{options:z(e,o),showSearch:e.showSearch!==!1,placeholder:e.placeholder});case"number":return t.jsx(f,{min:e.min,max:e.max,style:{width:"100%"},placeholder:e.placeholder});case"datetime":return t.jsx(L,{showTime:!0,style:{width:"100%"}});case"switch":return t.jsx(A,{});case"tags":return t.jsx(E,{mode:"tags",tokenSeparators:[","],options:e.options,placeholder:e.placeholder});case"textarea":return t.jsx(C.TextArea,{rows:e.rows||3,placeholder:e.placeholder});default:return t.jsx(C,{placeholder:e.placeholder})}};function ee({title:e,description:o,entity:n,permission:i,createLabel:l,resourceDefs:a=[],formFields:I=[],columns:y=[],buildPayload:R=p=>p,afterLoad:S}){const[p,K]=r.useState([]),[m,q]=r.useState({}),[M,h]=r.useState(!1),[O,b]=r.useState(!1),[d]=j.useForm(),u=async()=>{b(!0);const[s,...c]=await Promise.all([w.list({entity:n,options:{page:1,items:100}}),...a.map(x=>w.list({entity:x.entity,options:{page:1,items:100}}))]),v=(s==null?void 0:s.result)||[],g={};a.forEach((x,V)=>{var k;g[x.key]=((k=c[V])==null?void 0:k.result)||[]}),K(S?S(v):v),q(g),b(!1)};r.useEffect(()=>{u()},[n]);const P=async()=>{const s=await d.validateFields(),c=await w.create({entity:n,jsonData:R(s)});c!=null&&c.success&&(B.success(`${e} record created`),h(!1),d.resetFields(),u())},T=r.useMemo(()=>y.map(s=>typeof s=="function"?s({resources:m,reload:u}):s),[y,m]);return t.jsxs(D,{title:e,description:o,permission:i,extra:t.jsxs(W,{children:[t.jsx(F,{icon:t.jsx(N,{}),onClick:u,children:"Refresh"}),t.jsx(F,{type:"primary",onClick:()=>h(!0),children:l})]}),children:[t.jsx(_,{rowKey:"_id",loading:O,dataSource:p,pagination:!1,columns:T,scroll:{x:!0}}),t.jsx($,{title:l,open:M,onCancel:()=>h(!1),onOk:P,width:720,children:t.jsx(j,{form:d,layout:"vertical",children:I.map(s=>t.jsx(j.Item,{name:s.name,label:s.label,rules:s.required?[{required:!0,message:`${s.label} is required`}]:void 0,initialValue:s.initialValue,valuePropName:s.type==="switch"?"checked":"value",children:G(s,m)},s.name))})})]})}export{ee as E};
