import{j as e,s as v,u as H}from"./index-3808da28.js";import{I as M,B as w,P as q,F as f,y as I,z as S,r as b,T as A,H as O,J as D,K as V,E as L,Q as B,V as K,X as $,m as _,Y as J,Z as Q,C as X}from"./vendor-61f0f29a.js";const Y=({onSearch:s,onAdd:t})=>e.jsxs("div",{className:"flex justify-between mb-4",children:[e.jsx(M.Search,{placeholder:"搜索团队",allowClear:!0,onSearch:s,style:{width:200}}),e.jsx(w,{type:"primary",icon:e.jsx(q,{}),onClick:t,children:"新增团队"})]}),Z=({editing:s,dataIndex:t,title:r,inputType:a,record:l,index:m,children:i,...x})=>{const j=e.jsx(M,{});return e.jsx("td",{...x,children:s?e.jsx(f.Item,{name:t,style:{margin:0},rules:[{required:!0,message:`请输入${r}!`}],children:j}):i})},G=({nameProps:s,emailProps:t})=>e.jsxs(I.Compact,{block:!0,children:[e.jsx(f.Item,{...s,style:{margin:0,width:"50%"},rules:[{required:!0,message:"请输入姓名!"}],children:e.jsx(M,{placeholder:"姓名"})}),e.jsx(f.Item,{...t,style:{margin:0,width:"50%"},rules:[{required:!0,message:"请输入邮箱!"},{type:"email",message:"请输入有效的邮箱!"}],children:e.jsx(M,{placeholder:"邮箱"})})]}),P={OWNER:"gold",ADMIN:"purple",MEMBER:"blue"},ee=[{label:"Owner",value:"OWNER"},{label:"Admin",value:"ADMIN"},{label:"Member",value:"MEMBER"}],te=({value:s,onChange:t,disabled:r})=>e.jsx(S,{value:s,onChange:t,disabled:r,children:ee.map(({label:a,value:l})=>e.jsx(S.Option,{value:l,children:a},l))}),se=({editing:s,dataIndex:t,title:r,record:a,children:l,...m})=>{let i;return t==="role"?i=e.jsx(te,{}):t==="user"&&(i=e.jsx(G,{nameProps:{name:["user","name"]},emailProps:{name:["user","email"]}})),e.jsx("td",{...m,children:s?e.jsx(f.Item,{name:t,style:{margin:0},rules:[{required:!0,message:`请输入${r}!`}],children:i}):l})},ae=({memberships:s,onUpdate:t,onDelete:r,onAdd:a})=>{const[l]=f.useForm(),[m,i]=b.useState(""),x=n=>n.id===m,j=n=>{l.setFieldsValue({...n}),i(n.id)},y=()=>{i("")},u=async n=>{try{const h=await l.validateFields();i(""),t(n,h)}catch(h){console.error("Save failed:",h)}},p=[{title:"成员",dataIndex:"users",key:"users",editable:!0,render:n=>e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"font-medium",children:n.email}),e.jsx("span",{className:"text-gray-500 text-sm",children:n.email})]})},{title:"角色",dataIndex:"role",key:"role",editable:!0,render:n=>e.jsx(O,{color:P[n],children:n.toLowerCase()})},{title:"创建者",dataIndex:"is_creator",key:"is_creator",render:n=>n?e.jsx(O,{color:"green",children:"是"}):e.jsx(O,{color:"default",children:"否"})},{title:"操作",key:"action",width:180,render:(n,h)=>x(h)?e.jsxs(I,{children:[e.jsx(w,{icon:e.jsx(D,{}),onClick:()=>u(h.id),type:"link",size:"small",children:"保存"}),e.jsx(w,{icon:e.jsx(V,{}),onClick:y,type:"link",size:"small",children:"取消"})]}):e.jsxs(I,{children:[e.jsx(w,{disabled:m!==""||h.isCreator,icon:e.jsx(L,{}),onClick:()=>j(h),type:"link",size:"small",children:"编辑"}),!h.isCreator&&e.jsx(B,{title:"确定要删除该成员吗?",onConfirm:()=>r(h.id),children:e.jsx(w,{icon:e.jsx(K,{}),type:"link",size:"small",danger:!0,children:"删除"})})]})}].map(n=>n.editable?{...n,onCell:h=>({record:h,dataIndex:n.dataIndex,title:n.title,editing:x(h)})}:n);return e.jsxs("div",{className:"space-y-4",children:[e.jsx(w,{type:"primary",icon:e.jsx(q,{}),onClick:a,className:"mb-4",children:"添加成员"}),e.jsx(f,{form:l,component:!1,children:e.jsx(A,{components:{body:{cell:se}},columns:p,dataSource:s,rowKey:"id",pagination:!1})})]})},{Option:R}=S,re=({teamId:s,memberships:t})=>{const[r,a]=b.useState(t),[l,m]=b.useState(!1),[i]=f.useForm(),x=(c,p)=>{a(n=>n.map(h=>h.id===c?{...h,...p}:h)),_.success("成员信息已更新")},j=c=>{a(p=>p.filter(n=>n.id!==c)),_.success("成员已删除")},y=()=>{m(!0),i.resetFields()},u=async()=>{try{const c=await i.validateFields(),p={id:`${Date.now()}`,teamId:s,isCreator:!1,...c};a(n=>[...n,p]),m(!1),_.success("成员已添加")}catch(c){console.error("Add failed:",c)}};return e.jsxs("div",{className:"bg-gray-50 dark:bg-gray-800 p-4 rounded-lg",children:[e.jsx("h3",{className:"text-lg font-medium mb-4",children:"团队成员"}),e.jsx(ae,{memberships:r,onUpdate:x,onDelete:j,onAdd:y}),e.jsx($,{title:"添加成员",open:l,onOk:u,onCancel:()=>m(!1),children:e.jsxs(f,{form:i,layout:"vertical",children:[e.jsx(f.Item,{name:["user","name"],label:"姓名",rules:[{required:!0,message:"请输入姓名!"}],children:e.jsx(M,{})}),e.jsx(f.Item,{name:["user","email"],label:"邮箱",rules:[{required:!0,message:"请输入邮箱!"},{type:"email",message:"请输入有效的邮箱!"}],children:e.jsx(M,{})}),e.jsx(f.Item,{name:"role",label:"角色",rules:[{required:!0,message:"请选择角色!"}],children:e.jsxs(S,{children:[e.jsx(R,{value:"ADMIN",children:"Admin"}),e.jsx(R,{value:"MEMBER",children:"Member"})]})})]})})]})},le=({tableLoading:s,pagination:t,dataSource:r,onTableChange:a,onDelete:l,onUpdate:m})=>{const[i]=f.useForm(),[x,j]=b.useState(""),[y,u]=b.useState(!1),c=o=>o.id===x,p=o=>{i.setFieldsValue({name:o.name,description:o.description}),j(o.id)},n=()=>{j(""),i.resetFields()},h=async o=>{try{u(!0);const d=await i.validateFields();await m(o,{name:d.name,description:d.description,updated_at:new Date().toISOString()}),j(""),_.success("更新成功"),a==null||a()}catch(d){console.error("Save failed:",d),_.error("更新失败: "+d.message)}finally{u(!1)}},T=async o=>{try{u(!0),await l(o),_.success("删除成功"),a==null||a()}catch(d){console.error("Delete failed:",d),_.error("删除失败: "+d.message)}finally{u(!1)}},C=[{title:"团队名称",dataIndex:"name",key:"name",editable:!0,render:(o,d)=>e.jsxs("div",{className:"flex items-center gap-2",children:[d.avatar_url&&e.jsx("img",{src:d.avatar_url,alt:o,className:"w-8 h-8 rounded-full object-cover"}),e.jsx("span",{className:"font-medium",children:o})]})},{title:"描述",dataIndex:"description",key:"description",editable:!0},{title:"成员数量",dataIndex:"team_membership",key:"team_membership",render:o=>(o==null?void 0:o.length)||0},{title:"创建时间",dataIndex:"created_at",key:"created_at",sorter:!0,defaultSortOrder:"descend",render:o=>new Date(o).toLocaleString()},{title:"操作",key:"action",width:180,render:(o,d)=>c(d)?e.jsxs(I,{size:0,children:[e.jsx(w,{icon:e.jsx(D,{}),onClick:()=>h(d.id),type:"link",size:"small",loading:y,children:"保存"}),e.jsx(w,{size:"small",icon:e.jsx(V,{}),onClick:n,type:"link",children:"取消"})]}):e.jsxs(I,{children:[e.jsx(w,{size:"small",disabled:x!=="",icon:e.jsx(L,{}),onClick:()=>p(d),type:"link",children:"编辑"}),e.jsx(B,{title:"确定要删除该团队吗?",onConfirm:()=>T(d.id),children:e.jsx(w,{icon:e.jsx(K,{}),type:"link",danger:!0,size:"small",loading:y,children:"删除"})})]})}].map(o=>o.editable?{...o,onCell:d=>({record:d,dataIndex:o.dataIndex,title:o.title,editing:c(d)})}:o),N=(o,d,k,g)=>{a==null||a(o,d,k,g)};return e.jsx(f,{form:i,component:!1,children:e.jsx(A,{className:"w-full",pagination:t,loading:y||s,components:{body:{cell:Z}},onChange:N,dataSource:r,columns:C,rowKey:"id",expandable:{expandedRowRender:o=>e.jsx(re,{memberships:o.team_membership,teamId:o.id,onMembershipChange:a})}})})},ie=({form:s})=>{const t=async({file:r,onSuccess:a,onError:l})=>{try{const m=r.name.split(".").pop(),x=`team-avatars/${`${Math.random()}.${m}`}`,{error:j}=await v.storage.from("avatars").upload(x,r);if(j)throw j;const{data:{publicUrl:y}}=v.storage.from("avatars").getPublicUrl(x);s.setFieldValue("avatarUrl",y),a(y)}catch(m){console.error("Upload error:",m),l(m)}};return e.jsxs(f,{form:s,layout:"vertical",requiredMark:!1,children:[e.jsx(f.Item,{name:"name",label:"团队名称",rules:[{required:!0,message:"请输入团队名称"}],children:e.jsx(M,{placeholder:"请输入团队名称"})}),e.jsx(f.Item,{name:"avatarUrl",label:"团队头像",children:e.jsx(J,{customRequest:t,showUploadList:!1,maxCount:1,listType:"picture-card",children:s.getFieldValue("avatarUrl")?e.jsx("img",{src:s.getFieldValue("avatarUrl"),alt:"avatar",style:{width:"100%",height:"100%",objectFit:"cover"}}):e.jsxs("div",{children:[e.jsx(q,{}),e.jsx("div",{style:{marginTop:8},children:"上传头像"})]})})}),e.jsx(f.Item,{name:"tags",label:"标签",rules:[{required:!0,message:"请选择至少一个标签"}],children:e.jsx(S,{mode:"tags",placeholder:"请输入或选择标签",options:[{label:"研发",value:"development"},{label:"设计",value:"design"},{label:"运营",value:"operation"},{label:"市场",value:"marketing"}]})})]})},ne=({open:s,onCancel:t,onSubmit:r,confirmLoading:a})=>{const[l]=f.useForm(),m=async()=>{try{const i=await l.validateFields();await r(i),l.resetFields()}catch(i){console.error("Validation failed:",i)}};return e.jsx($,{title:"新增团队",open:s,onCancel:t,onOk:m,confirmLoading:a,width:600,children:e.jsx(ie,{form:l})})},F={async getTeams({page:s=1,pageSize:t=10,orderBy:r="created_at",ascending:a=!1,searchQuery:l=""}={}){const m=(s-1)*t,i=m+t-1;let x=v.from("teams").select(` id, name, description, attributes, created_at, updated_at, deleted_at, schema_version, avatar_url, team_membership( id, user_id, role, is_creator, users( id, email ) ) `,{count:"exact"}).is("deleted_at",null).order(r,{ascending:a}).range(m,i);l&&(x=x.ilike("name",`%${l}%`));const{data:j,error:y,count:u}=await x.order(r,{ascending:a}).range(m,i);if(y)throw console.error("Error fetching teams:",y),y;return{data:j,total:u||0}},async createTeam({name:s,description:t,userId:r}){const{data:a,error:l}=await v.from("teams").insert([{name:s,description:t}]).select().single();if(l)throw l;const{error:m}=await v.from("team_membership").insert([{id:a.id,team_id:a.id,user_id:r,role:"OWNER",is_creator:!0}]);if(m)throw m;return a},async getTeamById(s){const{data:t,error:r}=await v.from("teams").select(` id, name, description, attributes, created_at, updated_at, avatar_url, team_membership( id, user_id, role, is_creator, users( id, email ) ) `).eq("id",s).single();if(r)throw r;return t},async updateTeam(s,t){const{data:r,error:a}=await v.from("teams").update(t).eq("id",s).select().single();if(a)throw a;return r},async addTeamMember(s,t,r="MEMBER"){const{data:a,error:l}=await v.from("team_membership").insert([{team_id:s,user_id:t,role:r,is_creator:!1}]).select().single();if(l)throw l;return a},async updateMemberRole(s,t,r){const{data:a,error:l}=await v.from("team_membership").update({role:r}).match({team_id:s,user_id:t}).select().single();if(l)throw l},async deleteTeam(s){const{error:t}=await v.from("teams").delete().eq("id",s).select();if(t)throw t}},de=(s,t)=>{const[r,a]=b.useState([]),[l,m]=b.useState(!1),i=b.useCallback(async(u={})=>{try{m(!0);const{data:c,total:p}=await F.getTeams({page:u.current||s.current,pageSize:u.pageSize||s.pageSize,orderBy:u.field||t.field,ascending:u.order==="ascend",...(u==null?void 0:u.search)!==""?{searchQuery:u.search}:{}});return a(c),{data:c,total:p}}catch(c){console.error(c),_.error("获取团队列表失败")}finally{m(!1)}},[s.current,s.pageSize,t.field,t.order]);return{teams:r,loading:l,fetchTeams:i,createTeam:async u=>{try{const c=await F.createTeam(u);return a(p=>[...p,c]),c}catch(c){throw _.error("Failed to create team"),c}},updateTeam:async(u,c)=>{try{const p=await F.updateTeam(u,c);return a(n=>n.map(h=>h.id===u?p:h)),p}catch(p){throw _.error("Failed to update team"),p}},deleteTeam:async u=>{try{await F.deleteTeam(u),a(c=>c.filter(p=>p.id!==u))}catch(c){throw c}}}},me=()=>{const[s,t]=b.useState({current:1,pageSize:10,total:0}),[r,a]=b.useState({field:"created_at",order:"descend"}),{teams:l,loading:m,fetchTeams:i,createTeam:x,updateTeam:j,deleteTeam:y}=de(s,r),[u,c]=b.useState(!1),[p,n]=b.useState(!1),{user:h}=H();b.useEffect(()=>{(async()=>{const{total:k}=await i();t(g=>({...g,total:k}))})()},[i]);const T=(d,k,g)=>{const E={current:d==null?void 0:d.current,pageSize:d==null?void 0:d.pageSize,field:g==null?void 0:g.field,order:g==null?void 0:g.order};t(z=>({...z,current:E.current,pageSize:E.pageSize})),a({field:E.field||r.field,order:E.order||r.order}),i(E).then(({total:z})=>{t(W=>({...W,total:z}))})},U=()=>{c(!0)},C=()=>{c(!1)},N=async d=>{try{n(!0),await x({...d,userId:h.id}),c(!1),t(g=>({...g,current:1}));const{total:k}=await i({current:1});t(g=>({...g,total:k}))}catch(k){console.error("Failed to create team:",k)}finally{n(!1)}},o=d=>{i({search:d})};return e.jsx(Q,{children:e.jsxs(X,{title:"团队管理",bordered:!1,children:[e.jsx(Y,{onSearch:o,onAdd:U}),e.jsx(le,{tableLoading:m,dataSource:l,pagination:s,onTableChange:T,onUpdate:j,onDelete:y}),e.jsx(ne,{open:u,onCancel:C,onSubmit:N,confirmLoading:p})]})})};export{me as default}; //# sourceMappingURL=index-be5fed8f.js.map