1import{a as d,j as e,e as re,u as I,f as L}from"./vendor-query-C46OD5PZ.js";import{k as A,D as O,l as R,m as P,n as Q,I as h,t as g,u as te,b as M}from"./index-C7Xg1sgt.js";import{T as ne}from"./textarea-D0PDiwbe.js";import{P as G}from"./plus-B49XbSl6.js";import{P as le}from"./pencil-vu5AiH1I.js";import{A as ie}from"./archive-CMyXeU-6.js";import{R as de}from"./rotate-ccw-DYIAmv4O.js";import{u as oe}from"./useBadgeDefinitions-BA1fBNt-.js";import{S as ce}from"./search-DhGbnj7v.js";import{C as me}from"./chart-column-CfAgqyX6.js";import"./vendor-react-BNuWwWSs.js";import"./vendor-charts-CY2GWQyy.js";import"./vendor-dnd-CHRVsb0a.js";const V=["Training","Meisterschaft","ADAC","Rekord","Auszeichnung","Treue"],xe=[{value:"manual",label:"Manuell (Admin vergibt)"},{value:"automatic",label:"Automatisch"},{value:"seasonal",label:"Saisonal"},{value:"hidden",label:"Versteckt (Easter Egg)"}],ue=["#10b981","#3b82f6","#FACC15","#f97316","#ef4444","#a855f7","#06b6d4","#6366f1","#9ca3af"],ge=["ð","ð¥","ð¥","ð¥","ðï¸","â","ð","ð","ð","ð","ð
","â¡","ð¯","ð","â
","ð¥","ð¯","ð","ð","âï¸","ðï¸","ð
","ð","â±ï¸","ð","ð ï¸","ð","ð","ð","ð¿","ð¥³","â¤ï¸","ð«","ð«","ðï¸","ð¤","ðï¸","ð","ð","ð","ðï¸","ð¤º","ð"],q=()=>({id:"",name:"",description:"",emoji:"â",color:"#FACC15",category:"Training",badge_type:"manual",is_hidden:!1,sort_order:0});function pe({badges:c=[],onSave:v,onArchive:N,onRestore:p}){const[b,y]=d.useState(!1),[o,S]=d.useState(null),[r,i]=d.useState(q()),[w,k]=d.use
1State("Training"),[m,C]=d.useState(!1),x=c.filter(a=>a.is_active!==!1&&a.category===w),f=c.filter(a=>a.is_active===!1&&a.category===w),j=m?f:x,E=()=>{if(!r.name||!r.emoji||!o&&!r.id){g.error("Bitte Name, Emoji und ID ausfüllen");return}v({...r,id:r.id.toLowerCase().replace(/\s+/g,"_")},o),_()},u=a=>{S(a),i({...a,id:a.id}),y(!0)},_=()=>{y(!1),S(null),i(q())};return e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"flex gap-2 flex-wrap",children:V.map(a=>e.jsx("button",{onClick:()=>k(a),className:`text-xs px-3 py-1.5 rounded-full border font-bold transition-all ${w===a?"bg-primary text-primary-foreground":"bg-muted text-muted-foreground hover:border-primary"}`,children:a},a))}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(A,{onClick:()=>y(!0),size:"sm",children:[e.jsx(G,{className:"w-4 h-4 mr-1"})," Badge hinzufügen"]}),e.jsx("button",{onClick:()=>C(!m),className:`text-xs px-3 py-1.5 rounded-full border font-bold transition-all ml-auto ${m?"bg-gray-700 text-white border-gray-700":"border-border text-muted-foreground hover:border-primary"}`,children:m?"Aktive anzeigen":`Archivierte (${f.length})`})]}),e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:j.length===0?e.jsx("div",{className:"col-span-full text-center py-8 text-muted-foreground text-sm border border-dashed rounded-xl",children:m?"Keine archivierten Badges":"Keine Badges in dieser Kategorie"}):j.map(a=>e.jsxs("div",{className:`flex items-start gap-3 p-3 rounded-xl border transition-colors ${a.is_active===!1?"opacity-50 bg-muted":"bg-card hover:border-primary/40"}`,style:{borderLeftColor:a.color,borderLeftWidth:"3px"},children:[e.jsx("span",{className:"text-2xl flex-shrink-0",children:a.emoji}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("p",{className:"font-semibold text-sm truncate",children:a.name}),a.is_hidden&&e.jsx("span",{className:"text-[9px] bg-purple-100 text-purple-600 px-1.5 py-0.5 rounded font-bold uppercase",children:"Versteckt"}),a.badge_type!=="manual"&&e.jsx("span",{className:"text-[9px] bg-blue-100 text-blue-600 px-1.5 py-0.5 rounded font-bold uppercase",children:a.badge_type})]}),a.description&&e.jsx("p",{className:"text-[10px] text-muted-foreground mt-0.5 line-clamp-1",children:a.description}),e.jsx("p",{className:"text-[9px] text-muted-foreground/60 mt-0.5",children:a.id})]}),e.jsx("div",{className:"flex gap-1 flex-shrink-0",children:a.is_active!==!1?e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>u(a),className:"w-8 h-8 rounded-lg bg-primary/10 text-primary hover:bg-primary/20 flex items-center justify-center",children:e.jsx(le,{className:"w-3.5 h-3.5"})}),e.jsx("button",{onClick:()=>{confirm(`"${a.name}" archivieren? Bestehende Vergaben bleiben erhalten.`)&&N(a.id)},className:"w-8 h-8 rounded-lg bg-amber-100 text-amber-700 hover:bg-amber-200 flex items-center justify-center",title:"Archivieren",children:e.jsx(ie,{className:"w-3.5 h-3.5"})})]}):e.jsx("button",{onClick:()=>p(a.id),className:"w-8 h-8 rounded-lg bg-green-100 text-green-700 hover:bg-green-200 flex items-center justify-center",title:"Reaktivieren",children:e.jsx(de,{className:"w-3.5 h-3.5"})})})]},a.id))}),e.jsx(O,{open:b,onOpenChange:_,children:e.jsxs(R,{className:"max-w-md max-h-[90vh] flex flex-col",children:[e.jsx(P,{children:e.jsx(Q,{className:"font-heading",children:o?"Badge bearbeiten":"Neues Badge"})}),e.jsxs("div",{className:"space-y-4 overflow-y-auto flex-1 pr-1",children:[!o&&e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"ID *"}),e.jsx(h,{value:r.id,onChange:a=>i(t=>({...t,id:a.target.value})),placeholder:"z.B. club_champion",className:"text-xs"}),e.jsx("p",{className:"text-[10px] text-muted-foreground mt-0.5",children:"Eindeutig, klein, Unterstriche (nicht änderbar)"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"Name *"}),e.jsx(h,{value:r.name,onChange:a=>i(t=>({...t,name:a.target.value})),placeholder:"z.B. Vereinsmeister"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"Beschreibung"}),e.jsx(ne,{value:r.description,onChange:a=>i(t=>({...t,description:a.target.value})),placeholder:"Vergabekriterium...",className:"h-16 resize-none text-sm"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"Emoji *"}),e.jsx("div",{className:"grid grid-cols-8 gap-1 mb-2",children:ge.map(a=>e.jsx("button",{onClick:()=>i(t=>({...t,emoji:a})),className:`text-xl p-1.5 rounded-lg border transition-all ${r.emoji===a?"border-primary bg-primary/10":"border-border hover:border-primary/50"}`,children:a},a))}),e.jsx(h,{value:r.emoji,onChange:a=>i(t=>({...t,emoji:a.target.value})),placeholder:"Emoji eingeben",maxLength:"2"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-2",children:"Farbe"}),e.jsx("div",{className:"flex gap-2 flex-wrap mb-2",children:ue.map(a=>e.jsx("button",{onClick:()=>i(t=>({...t,color:a})),className:`w-8 h-8 rounded-lg border-2 ${r.color===a?"border-foreground scale-110":"border-transparent"}`,style:{backgroundColor:a}},a))}),e.jsx(h,{type:"text",value:r.color,onChange:a=>i(t=>({...t,color:a.target.value})),placeholder:"#FACC15",maxLength:"7",className:"text-xs"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"Kategorie"}),e.jsx("select",{value:r.category,onChange:a=>i(t=>({...t,category:a.target.value})),className:"w-full h-9 px-3 rounded-md border border-input bg-transparent text-sm",children:V.map(a=>e.jsx("option",{value:a,children:a},a))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"Typ"}
1),e.jsx("select",{value:r.badge_type,onChange:a=>i(t=>({...t,badge_type:a.target.value})),className:"w-full h-9 px-3 rounded-md border border-input bg-transparent text-sm",children:xe.map(a=>e.jsx("option",{value:a.value,children:a.label},a.value))})]})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"Sortierung"}),e.jsx(h,{type:"number",value:r.sort_order,onChange:a=>i(t=>({...t,sort_order:parseInt(a.target.value)||0})),className:"text-sm"})]}),e.jsxs("label",{className:"flex items-center gap-2 cursor-pointer mt-4",children:[e.jsx("input",{type:"checkbox",checked:r.is_hidden,onChange:a=>i(t=>({...t,is_hidden:a.target.checked})),className:"w-4 h-4"}),e.jsx("span",{className:"text-sm font-medium",children:"Versteckt"})]})]}),e.jsxs("div",{className:"p-3 rounded-xl bg-muted border text-center",children:[e.jsx("p",{className:"text-[10px] text-muted-foreground mb-2 uppercase tracking-wider",children:"Vorschau im Ausweis"}),e.jsxs("div",{className:"flex items-center justify-center gap-3 p-3 rounded-xl border",style:{backgroundColor:`${r.color}15`,borderColor:`${r.color}40`},children:[e.jsx("span",{className:"text-3xl",children:r.emoji}),e.jsxs("div",{className:"text-left",children:[e.jsx("p",{className:"text-sm font-black",style:{color:r.color},children:r.name||"Badge Name"}),r.description&&e.jsx("p",{className:"text-[10px] text-muted-foreground mt-0.5",children:r.description})]})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(A,{variant:"outline",className:"flex-1",onClick:_,children:"Abbrechen"}),e.jsx(A,{className:"flex-1",onClick:E,children:o?"Speichern":"Erstellen"})]})]})]})})]})}const he=["vergabe","definitionen","auswertung"],be={vergabe:"Badge-Vergabe",definitionen:"Badge-Definitionen",auswertung:"Auswertung"};function De(){const{user:c,isAuthenticated:v,authError:N}=te(),p=re(),[b,y]=d.useState("vergabe"),[o,S]=d.useState(""),[r,i]=d.useState(null),[w,k]=d.use
1State(!1),[m,C]=d.useState(""),[x,f]=d.useState(""),[j,E]=d.useState(""),{badges:u,allBadges:_,createMutation:a,updateMutation:t,archiveMutation:W,restoreMutation:H}=oe(),$=[...new Set(u.map(s=>s.category))],{data:K=[]}=I({queryKey:["users-for-badges"],queryFn:async()=>(await M.functions.invoke("listAllUsers",{})).data.users||[],enabled:v&&!N}),{data:B=[]}=I({queryKey:["all-member-badges"],queryFn:async()=>(await M.functions.invoke("manageBadges",{action:"list"})).data.badges||[],enabled:v&&!N}),T=L({mutationFn:s=>M.functions.invoke("manageBadges",{action:"create",data:s}),onSuccess:()=>{p.invalidateQueries({queryKey:["all-member-badges"]}),p.invalidateQueries({queryKey:["my-badges"]}),g.success("Abzeichen vergeben!"),k(!1),C(""),f("")}}),J=L({mutationFn:s=>M.functions.invoke("manageBadges",{action:"delete",data:{badgeId:s}}),onSuccess:()=>{p.invalidateQueries({queryKey:["all-member-badges"]}),p.invalidateQueries({queryKey:["my-badges"]}),g.success("Abzeichen entfernt")}}),Y=["admin","hauptadmin","vorstand_admin"].includes(c==null?void 0:c.role);if(!v||N||!Y)return e.jsx("div",{className:"p-8 text-center text-muted-foreground",children:"Zugriff verweigert"});const F=K.filter(s=>(s.display_name||s.full_name||"").toLowerCase().includes(o.toLowerCase())||(s.email||"").toLowerCase().includes(o.toLowerCase())),Z=s=>{var n;const l=(n=K.find(D=>D.id===s))==null?void 0:n.email;return B.filter(D=>D.user_id===s||D.user_email===l)},U=()=>{if(!r||!x)return;const s=u.find(n=>n.id===x);if(!s)return;if(B.find(n=>(n.user_id===r.id||n.user_email===r.email)&&n.badge_id===x)){g.error("Bereits vergeben");return}T.mutate({user_id:r.id,user_email:r.email,user_name:r.display_name||r.full_name||"",badge_id:s.id,badge_name:s.name,badge_emoji:s.emoji||"",badge_color:s.color||"",awarded_by:c.display_name||c.full_name||"",awarded_date:new Date().toISOString(),note:m.trim()||""})},X=async(s,l)=>{l?(await t.mutateAsync(s),g.success("Badge aktualisiert")):(await a.mutateAsync(s),g.success("Badge erstellt")),p.invalidateQueries({queryKey:["my-badges"]})},ee=async s=>{await W.mutateAsync(s),g.success("Badge archiviert â bestehende Vergaben bleiben erhalten")},se=async s=>{await H.mutateAsync(s),g.success("Badge reaktiviert")},ae=j?u.filter(s=>s.category===j):u,z=u.map(s=>({...s,count:B.filter(l=>l.badge_id===s.id).length})).sort((s,l)=>l.count-s.count);return e.jsxs("div",{className:"px-4 py-4 max-w-3xl mx-auto",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-5",children:[e.jsx("div",{className:"w-1 h-6 bg-[#FACC15] rounded-full"}),e.jsx("h2",{className:"font-heading font-black text-lg uppercase tracking-wider",children:"Abzeichen verwalten"})]}),e.jsx("div",{className:"flex gap-1 mb-5 bg-muted p-1 rounded-xl",children:he.map(s=>e.jsx("button",{onClick:()=>y(s),className:`flex-1 text-xs py-2 rounded-lg font-bold transition-all ${b===s?"bg-card shadow-sm text-foreground":"text-muted-foreground"}`,children:be[s]},s))}),b==="vergabe"&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"relative",children:[e.jsx(ce,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(h,{placeholder:"Mitglied suchen...",value:o,onChange:s=>S(s.target.value),className:"pl-9"})]}),e.jsxs("div",{className:"space-y-2",children:[F.map(s=>{const l=Z(s.id);return e.jsxs("div",{className:"p-3 rounded-xl bg-card border border-border shadow-sm",children:[e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsxs("div",{className:"flex items-center gap-3 flex-1 min-w-0",children:[e.jsx("div",{className:"w-9 h-9 rounded-full bg-muted flex items-center justify-center flex-shrink-0 overflow-hidden border border-border",children:s.avatar_url?e.jsx("img",{src:s.avatar_url,alt:"",className:"w-full h-full object-cover"}):e.jsx("span",{className:"text-sm font-black text-primary",children:(s.display_name||s.full_name||"?").charAt(0).toUpperCase()})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-bold text-sm truncate",children:s.display_name||s.full_name||"â"}),e.jsx("p",{className:"text-[11px] text-muted-foreground truncate",children:s.email})]})]}),e.jsxs("div",{className:"flex items-center gap-2 flex-shrink-0",children:[e.jsxs("span",{className:"text-xs font-bold text-primary",children:[l.length," ð
"]}),e.jsxs(A,{size:"sm",className:"h-8 bg-primary text-primary-foreground font-bold",onClick:()=>
1{i(s),k(!0),E($[0]||"")},children:[e.jsx(G,{className:"w-3 h-3 mr-1"})," Vergeben"]})]})]}),l.length>0&&e.jsx("div",{className:"flex flex-wrap gap-1.5 mt-3 pt-3 border-t border-border",children:l.map(n=>e.jsxs("div",{className:"flex items-center gap-1 text-[10px] px-2 py-1 rounded-full border group relative",style:{backgroundColor:`${n.badge_color||"#666"}20`,borderColor:`${n.badge_color||"#666"}40`,color:n.badge_color||"#aaa"},children:[e.jsx("span",{children:n.badge_emoji}),e.jsx("span",{className:"font-medium max-w-[80px] truncate",children:n.badge_name}),e.jsx("button",{onClick:()=>{confirm("Abzeichen entfernen?")&&J.mutate(n.id)},className:"ml-1 text-destructive opacity-0 group-hover:opacity-100 transition-opacity",children:"Ã"})]},n.id))})]},s.id)}),F.length===0&&e.jsx("div",{className:"text-center py-8 text-muted-foreground text-sm border border-dashed rounded-xl",children:o?`Kein Mitglied gefunden für "${o}"`:"Keine Mitglieder geladen"})]})]}),b==="definitionen"&&e.jsx(pe,{badges:_,onSave:X,onArchive:ee,onRestore:se}),b==="auswertung"&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-3 mb-4",children:[e.jsxs("div",{className:"bg-card border border-border rounded-xl p-4 text-center",children:[e.jsx("p",{className:"text-3xl font-black text-primary",children:B.length}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1 uppercase tracking-wider",children:"Vergaben gesamt"})]}),e.jsxs("div",{className:"bg-card border border-border rounded-xl p-4 text-center",children:[e.jsx("p",{className:"text-3xl font-black",children:u.length}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1 uppercase tracking-wider",children:"Aktive Badges"})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-[10px] font-black uppercase tracking-widest text-muted-foreground mb-3",children:"Vergaben pro Badge"}),z.map(s=>e.jsxs("div",{className:"flex items-center gap-3 p-3 bg-card border border-border rounded-xl",children:[e.jsx("span",{className:"text-xl flex-shrink-0",children:s.emoji}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-semibold truncate",children:s.name}),e.jsx("div",{className:"mt-1 h-1.5 bg-muted rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full rounded-full",style:{width:`${Math.max(s.count/Math.max(...z.map(l=>l.count),1)*100,2)}%`,backgroundColor:s.color}})})]}),e.jsxs("span",{className:"text-sm font-black flex-shrink-0",style:{color:s.color},children:[s.count,"Ã"]})]},s.id)),z.length===0&&e.jsxs("div",{className:"text-center py-8 text-muted-foreground text-sm border border-dashed rounded-xl",children:[e.jsx(me,{className:"w-8 h-8 mx-auto mb-2 opacity-30"}),"Noch keine Badges vergeben"]})]})]}),e.jsx(O,{open:w,onOpenChange:()=>{k(!1),f(""),C("")},children:e.jsxs(R,{className:"max-w-md h-[85vh] flex flex-col",children:[e.jsx(P,{children:e.jsxs(Q,{className:"font-heading font-black text-base",children:["Abzeichen vergeben â ",(r==null?void 0:r.display_name)||(r==null?void 0:r.full_name)]})}),e.jsx("div",{className:"flex gap-1 flex-wrap",children:$.map(s=>e.jsx("button",{onClick:()=>E(s),className:`text-xs px-3 py-1.5 rounded-full border font-bold transition-all ${j===s?"bg-primary text-primary-foreground border-primary":"bg-muted text-muted-foreground border-border hover:border-primary/40"}`,children:s},s))}),e.jsx("div",{className:"overflow-y-auto flex-1 space-y-2",children:ae.map(s=>{const l=B.find(n=>(n.user_id===(r==null?void 0:r.id)||n.user_email===(r==null?void 0:r.email))&&n.badge_id===s.id);return e.jsxs("button",{onClick:()=>!l&&f(s.id),disabled:!!l,className:`w-full flex items-center gap-3 p-3 rounded-xl border text-left transition-all ${l?"opacity-40 cursor-not-allowed bg-muted border-border":x===s.id?"border-primary bg-primary/10":"bg-card border-border hover:border-primary/40"}`,children:[e.jsx("span",{className:"text-2xl",children:s.emoji}),e.jsxs("div",{children:[e.jsx("p",{className:"text-sm font-bold",children:s.name}),s.description&&e.jsx("p",{className:"text-[10px] text-muted-foreground",children:s.description}),l&&e.jsx("p",{className:"text-[10px] text-primary",children:"Bereits vergeben"})]})]},s.id)})}),x&&e.jsxs("div",{className:"border-t border-border pt-3",children:[e.jsx("label",{className:"text-xs font-bold text-muted-foreground uppercase block mb-1",children:"Notiz (optional)"}),e.jsx(h,{value:m,onChange:s=>C(s.target.value),placeholder:"z.B. Trainingsdatum, Begründung..."})]}),e.jsx("div",{className:"border-t border-border pt-3",children:e.jsx(A,{onClick:U,disabled:!x||T.isPending,className:"w-full font-black",children:T.isPending?"Wird vergeben...":"ð
Abzeichen vergeben"})})]})})]})}export{De as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.