import{r as d,m as i}from"./vendor-61f0f29a.js";import{s as g}from"./index-3808da28.js";const R={async getResources({page:a=1,pageSize:o=10,orderBy:e="created_at",ascending:n=!1,searchQuery:l="",type:S=""}){try{let c=g.from("resources").select("*",{count:"exact"}).eq("type",S);l&&(c=c.or(`external_id.ilike.%${l}%`)),e&&(c=c.order(e,{ascending:n}));const w=(a-1)*o;c=c.range(w,w+o-1);const{data:p,count:s,error:h}=await c;if(h)throw h;return{data:p,total:s||0}}catch(c){throw c}},async createResource(a){try{const{data:o,error:e}=await g.from("resources").insert([{...a,type:"quota",created_at:new Date().toISOString(),updated_at:new Date().toISOString()}]).select().single();if(e)throw e;return o}catch(o){throw o}},async updateResource(a,o){try{const{data:e,error:n}=await g.from("resources").update({...o,updated_at:new Date().toISOString()}).eq("id",a).eq("type","quota").select().single();if(n)throw n;return e}catch(e){throw e}},async deleteResource(a,o){try{const{error:e}=await g.from("resources").delete().eq("id",a).eq("type",o).select();if(e)throw e;return!0}catch(e){throw e}}},P=(a,o,e)=>{const[n,l]=d.useState([]),[S,c]=d.useState(!1),[w,p]=d.useState(0),[s,h]=d.useState(a),[f,x]=d.useState(o),y=d.useCallback(async(t={})=>{try{c(!0);const r={current:t.current||s.current,pageSize:t.pageSize||s.pageSize},u={field:t.field||f.field,order:t.order||f.order};h(r),x(u);const{data:q,total:_}=await R.getResources({page:r.current,pageSize:r.pageSize,orderBy:u.field,ascending:u.order==="ascend",type:e,...(t==null?void 0:t.search)!==""?{searchQuery:t.search}:{}});return l(q||[]),p(_||0),{data:q,total:_}}catch(r){console.error("获取列表失败:",r),i.error("获取列表失败")}finally{c(!1)}},[s,f]);return{resources:n,loading:S,total:w,currentPagination:s,currentSorter:f,fetchResources:y,createResource:async t=>{try{const r=await R.createResource(t);return await y({current:1}),i.success("创建成功"),r}catch(r){throw i.error("创建失败"),r}},updateResource:async(t,r)=>{try{const u=await R.updateResource(t,r);return await y({current:s.current}),i.success("更新成功"),u}catch(u){throw i.error("更新失败"),u}},deleteResource:async t=>{try{await R.deleteResource(t,e);const r=n.length===1&&s.current>1?s.current-1:s.current;await y({current:r}),i.success("删除成功")}catch(r){throw i.error("删除失败"),r}}}};export{P as u};
//# sourceMappingURL=useResource-9c541112.js.map
