import{a as D,r,j as e}from"./index-DlcHsm52.js";import{C as w,A as P,e as V,B as g,P as $,a as G,F as z,I as q,f as K,T as L,l as U,m as H,r as f}from"./IdurarOs-j0IoqkpA.js";import{a as Q}from"./entityRegistry-D0487dfU.js";import{M as W}from"./index-Clbw7Jrz.js";import{R as X}from"./ReloadOutlined-C37-gl6w.js";import"./ErpApp-7VdZNpBd.js";const{Text:Y,Title:Z}=K,k=l=>l==null?"":typeof l=="object"?JSON.stringify(l):String(l),ee=(l,x,j)=>[...Array.from(new Set(l.flatMap(n=>Object.keys(n).filter(s=>!["_id","__v"].includes(s))))).slice(0,6).map(n=>({title:n,dataIndex:n,ellipsis:!0,render:s=>n==="status"||n==="type"?e.jsx(L,{children:k(s)}):k(s)})),{title:"Actions",key:"actions",fixed:"right",render:(n,s)=>e.jsxs(w,{children:[e.jsx(g,{size:"small",icon:e.jsx(U,{}),onClick:()=>x(s),children:"Edit"}),e.jsx(g,{size:"small",danger:!0,icon:e.jsx(H,{}),onClick:()=>j(s),children:"Delete"})]})}];function le({entity:l,title:x,description:j,sampleOverride:E}){const{entity:n}=D(),s=l||n,h=Q[s]||{entity:s,label:s,sample:{}},c={...h,sample:E||h.sample,label:x||h.label,description:j},S=c.sample||{},[O,N]=r.useState([]),[i,I]=r.useState({current:1,pageSize:10}),[M,_]=r.useState(!1),[A,u]=r.useState(!1),[o,C]=r.useState(null),[J,p]=r.useState(JSON.stringify(S,null,2)),[T,m]=r.useState(""),d=async(t=1,b=10)=>{var y,v;_(!0),m("");const a=await f.list({entity:s,options:{page:t,items:b}});a!=null&&a.success?(N(a.result||[]),I({current:Number(((y=a.pagination)==null?void 0:y.page)||t),pageSize:b,total:((v=a.pagination)==null?void 0:v.count)||0})):(N([]),m((a==null?void 0:a.message)||"Unable to load records.")),_(!1)};r.useEffect(()=>{C(null),p(JSON.stringify(S,null,2)),d()},[s]);const R=r.useMemo(()=>ee(O,t=>{C(t);const{_id:b,__v:a,...y}=t;p(JSON.stringify(y,null,2)),u(!0)},async t=>{await f.delete({entity:s,id:t._id}),d(i.current,i.pageSize)}),[O,s,i.current,i.pageSize]),F=()=>{C(null),p(JSON.stringify(S,null,2)),m(""),u(!0)},B=async()=>{try{const t=JSON.parse(J);o!=null&&o._id?await f.update({entity:s,id:o._id,jsonData:t}):await f.create({entity:s,jsonData:t}),u(!1),d(i.current,i.pageSize)}catch(t){m(t.message||"JSON payload is invalid.")}};return e.jsxs("div",{style:{padding:"24px 32px",width:"100%"},children:[e.jsxs(w,{direction:"vertical",size:16,style:{width:"100%"},children:[e.jsxs("div",{children:[e.jsx(Z,{level:2,style:{marginBottom:4},children:c.label}),e.jsx(Y,{type:"secondary",children:c.description||`Generic REST manager for /api/${s}/list, create, update, and delete.`})]}),T&&e.jsx(P,{type:"warning",showIcon:!0,message:T}),e.jsx(V,{extra:e.jsxs(w,{children:[e.jsx(g,{icon:e.jsx(X,{}),onClick:()=>d(i.current,i.pageSize),children:"Refresh"}),e.jsx(g,{type:"primary",icon:e.jsx($,{}),onClick:F,children:"New record"})]}),children:e.jsx(G,{rowKey:t=>t._id||JSON.stringify(t),columns:R,dataSource:O,loading:M,pagination:i,onChange:t=>d(t.current,t.pageSize),scroll:{x:!0}})})]}),e.jsx(W,{title:o?`Edit ${c.label}`:`Create ${c.label}`,open:A,onCancel:()=>u(!1),onOk:B,width:760,okText:o?"Save changes":"Create record",children:e.jsx(z,{layout:"vertical",children:e.jsx(z.Item,{label:"JSON payload",children:e.jsx(q.TextArea,{value:J,onChange:t=>p(t.target.value),rows:14,style:{fontFamily:"monospace"}})})})})]})}export{le as default};
