1import{r as l,j as e,L as v,T as _,W as O,b as o,t as u}from"./index-Dsd8GAzx.js";import{B as p}from"./badge-Ccbxu4a-.js";import{B as j}from"./button-BvA20N-A.js";import{U as D}from"./users-Oahu2G-8.js";import{C as H}from"./clock-wyqLImoB.js";import{C as M}from"./circle-check-BmsR8PtY.js";import{C as S}from"./circle-x-B95yijeT.js";import{f as W,e as L}from"./format-Ds46GObI.js";function ee(){const[X,R]=l.useState(null),[x,w]=l.useState([]),[b,A]=l.useState([]),[c,k]=l.useState([]),[U,B]=l.useState(!0),[f,$]=l.useState("affiliates"),[m,g]=l.useState(null);l.useEffect(()=>{async function t(){try{const s=await o.auth.me();if((s==null?void 0:s.role)!=="admin"){window.location.href="/app";return}R(s);const[a,r,i]=await Promise.all([o.entities.AffiliateApplication.list().catch(()=>[]),o.entities.AffiliateTransaction.list().catch(()=>[]),o.entities.WithdrawalRequest.list().catch(()=>[])]);w(a),A(r),k(i)}catch(s){console.error("Admin load error",s),u.error("Failed to load admin data")}finally{B(!1)}}t()},[]);const n=l.useMemo(()=>x.filter(s=>s.status==="accepted").map(s=>{const a=b.filter(d=>d.affiliateEmail===s.email),r=c.filter(d=>d.affiliateEmail===s.email&&d.status==="paid"),i=a.reduce((d,y)=>d+(y.commission||0),0),h=r.reduce((d,y)=>d+(y.amount||0),0),N=Math.max(0,i-h),z=c.find(d=>d.affiliateEmail===s.email&&d.status==="pending");return{...s,totalEarned:i,totalPaid:h,wallet:N,pendingWd:z,txCount:a.length}}).sort((s,a)=>a.wallet-s.wallet),[x,b,c]),E=l.useMemo(()=>n.reduce((t,s)=>t+s.wallet,0),[n]),C=l.useMemo(()=>n.reduce((t,s)=>t+s.totalEarned,0),[n]),q=x.filter(t=>t.status==="pending"),T=c.filter(t=>t.status==="pending"),F=async(t,s)=>{g(t);try{if(await o.entities.AffiliateApplication.update(t,{status:s}),s==="accepted"){const a=x.find(r=>r.id===t);if(a!=null&&a.email){const r=await o.entities.User.filter({email:a.email}).catch(()=>[]);r!=null&&r[0]&&await o.entities.User.update(r[0].id,{role:"affiliate"})}}w(a=>a.map(r=>r.id===t?{...r,status:s}:r)),u.success(s==="accepted"?"Application accepted â":"Application rejected")}catch{u.error("Update failed")}finally{g(null)}},P=async(t,s)=>{g(t);try{if(await o.entities.WithdrawalRequest.update(t,{status:s}),s==="paid"){const a=c.find(i=>i.id===t),r=b.filter(i=>i.affiliateEmail===(a==null?void 0:a.affiliateEmail)&&!i.paid);await Promise.all(r.map(i=>o.entities.AffiliateTransaction.update(i.id,{paid:!0}))),A(i=>i.map(h=>r.find(N=>N.id===h.id)?{...h,paid:!0}:h))}k(a=>a.map(r=>r.id===t?{...r,status:s}:r)),u.success(s==="paid"?"Marked as paid â":"Request rejected")}catch{u.error("Error")}finally{g(null)}};return U?e.jsx("div",{className:"flex items-center justify-center h-64",children:e.jsx(v,{className:"h-6 w-6 animate-spin text-muted-foreground"})}):e.jsxs("div",{children:[e.jsxs("div",{className:"mb-6",children:[e.jsx("h1",{className:"font-space font-bold text-2xl sm:text-3xl text-foreground mb-1",children:"Affiliate Program"}),e.jsx("p",{className:"text-muted-foreground text-sm",children:"Overview of all your affiliates and their earnings."})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3 sm:gap-4 mb-8",children:[e.jsxs("div",{className:"bg-card border border-border/50 rounded-xl p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground text-xs mb-1",children:[e.jsx(D,{className:"h-3.5 w-3.5"})," Active affiliates"]}),e.jsx("div",{className:"text-2xl font-bold text-foreground",children:n.length})]}),e.jsxs("div",{className:"bg-card border border-border/50 rounded-xl p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground text-xs mb-1",children:[e.jsx(_,{className:"h-3.5 w-3.5"})," Total commissions"]}),e.jsxs("div",{className:"text-2xl font-bold text-foreground",children:[C.toFixed(2),"â¬"]})]}),e.jsxs("div",{className:"bg-card border border-primary/40 rounded-xl p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground text-xs mb-1",children:[e.jsx(O,{className:"h-3.5 w-3.5"})," ð° Total owed"]}),e.jsxs("div",{className:"text-2xl font-bold text-primary",children:[E.toFixed(2),"â¬"]})]}),e.jsxs("div",{className:"bg-card border border-border/50 rounded-xl p-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground text-xs mb-1",children:[e.jsx(H,{className:"h-3.5 w-3.5"})," Pending payouts"]}),e.jsx("div",{className:"text-2xl font-bold text-orange-400",children:T.length})]})]}),e.jsx("div",{className:"flex gap-1 p-1 bg-secondary/50 rounded-xl border border-border/50 mb-6 w-full sm:w-fit overflow-x-auto",children:[{key:"affiliates",label:`Affiliates (${n.length})`},{key:"withdrawals",label:`Payouts (${T.length})`},{key:"applications",label:`Applications (${q.length})`}].map(t=>e.jsx("button",{onClick:()=>$(t.key),className:`px-3 sm:px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap transition-all ${f===t.key?"bg-primary text-primary-foreground":"text-muted-foreground hover:text-foreground"}`,children:t.label},t.key))}),f==="affiliates"&&e.jsx("div",{className:"bg-card border border-border/50 rounded-xl overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm min-w-[640px]",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-border/50 text-muted-foreground text-xs",children:[e.jsx("th",{className:"text-left px-4 py-3 font-medium",children:"Affiliate"}),e.jsx("th",{className:"text-left px-4 py-3 font-medium hidden md:table-cell",children:"Network"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"Sales"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"Total earned"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"Paid out"}),e.jsx("th",{className:"text-right px-4 py-3 font-medium",children:"ð° Wallet"}),e.jsx("th",{className:"text-center px-4 py-3 font-medium hidden md:table-cell",children:"Payout"})]})}),e.jsxs("tbody",{children:[n.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"text-center py-10 text-muted-foreground",children:"No active affiliates"})}),n.map(t=>e.jsxs("tr",{className:"border-b border-border/30 hover:bg-secondary/20 transition-colors",children:[e.jsxs("td",{className:"px-4 py-3",children:[e.jsx("div",{className:"font-medium text-foreground",children:t.fullName}),e.jsx("div",{className:"text-xs text-muted-foreground",children:t.email})]}),e.jsxs("td",{className:"px-4 py-3 hidden md:table-cell text-muted-foreground",children:[t.socialNetwork," · ",t.socialHandle]}),e.jsx("td",{className:"px-4 py-3 text-right text-muted-foreground",children:t.txCount}),e.jsxs("td",{className:"px-4 py-3 text-right text-green-400 font-medium",children:[t.totalEarned.toFixed(2),"â¬"]}),e.jsxs("td",{className:"px-4 py-3 text-right text-muted-foreground",children:[t.totalPaid.toFixed(2),"â¬"]}),e.jsx("td",{className:"px-4 py-3 text-right",children:e.jsxs("span",{className:`font-bold text-base ${t.wallet>0?"text-primary":"text-muted-foreground"}`,children:[t.wallet.toFixed(2),"â¬"]})}),e.jsx("td",{className:"px-4 py-3 text-center hidden md:table-cell",children:t.pendingWd?e.jsx(p,{variant:"outline",className:"text-orange-400 border-orange-400/50",children:"Pending"}):t.wallet>=70?e.jsx(p,{variant:"outline",className:"text-green-400 border-green-400/50",children:"Eligible"}):e.jsx("span",{className:"text-xs text-muted-foreground",children:"â"})})]},t.id))]}),n.length>0&&e.jsx("tfoot",{children:e.jsxs("tr",{className:"bg-secondary/30 font-semibold text-sm",children:[e.jsx("td",{className:"px-4 py-3 text-foreground",colSpan:3,children:"TOTAL"}),e.jsxs("td",{className:"px-4 py-3 text-right text-green-400",children:[C.toFixed(2),"â¬"]}),e.jsxs("td",{className:"px-4 py-3 text-right text-muted-foreground",children:[n.reduce((t,s)=>t+s.totalPaid,0).toFixed(2),"â¬"]}),e.jsxs("td",{className:"px-4 py-3 text-right text-primary font-bold text-base",children:[E.toFixed(2),"â¬"]}),e.jsx("td",{className:"hidden md:table-cell"})]})})]})}),f==="withdrawals"&&e.jsxs("div",{className:"space-y-3",children:[c.length===0&&e.jsx("div",{className:"text-center py-12 text-muted-foreground bg-card border border-border/50 rounded-xl",children:"No payout requests"}),c.map(t=>{var s;return e.jsxs("div",{className:"bg-card border border-border/50 rounded-xl p-4 flex items-center justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("div",{className:"font-medium text-foreground",children:t.affiliateName||t.affiliateEmail}),e.jsx("div",{className:"text-xs text-muted-foreground",children:t.affiliateEmail}),e.jsxs("div",{className:"text-xs text-muted-foreground mt-1",children:["IBAN: ",t.iban]}),t.created_date&&e.jsx("div",{className:"text-xs text-muted-foreground",children:W(new Date(t.created_date),"dd MMM yyyy",{locale:L})})]}),e.jsxs("div",{className:"text-2xl font-bold text-primary",children:[(s=t.amount)==null?void 0:s.toFixed(2),"â¬"]}),e.jsx("div",{className:"flex items-center gap-2",children:t.status==="pending"?e.jsxs(e.Fragment,{children:[e.jsx(j,{size:"sm",onClick:()=>P(t.id,"paid"),disabled:m===t.id,className:"bg-green-600 hover:bg-green-700 text-white",children:m===t.id?e.jsx(v,{className:"h-3.5 w-3.5 animate-spin"}):e.jsxs(e.Fragment,{children:[e.jsx(M,{className:"h-3.5 w-3.5 mr-1"}),"Mark as paid"]})}),e.jsxs(j,{size:"sm",variant:"outline",onClick:()=>P(t.id,"refused"),disabled:m===t.id,className:"border-red-500/50 text-red-400 hover:bg-red-500/10",children:[e.jsx(S,{className:"h-3.5 w-3.5 mr-1"}),"Reject"]})]}
1):e.jsx(p,{className:t.status==="paid"?"bg-green-600":"bg-red-600",children:t.status==="paid"?"Paid":"Rejected"})})]},t.id)})]}),f==="applications"&&e.jsxs("div",{className:"space-y-3",children:[x.length===0&&e.jsx("div",{className:"text-center py-12 text-muted-foreground bg-card border border-border/50 rounded-xl",children:"No applications"}),x.map(t=>{var s;return e.jsx("div",{className:"bg-card border border-border/50 rounded-xl p-4",children:e.jsxs("div",{className:"flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("span",{className:"font-medium text-foreground",children:t.fullName}),e.jsx(p,{variant:"outline",className:t.status==="pending"?"text-orange-400 border-orange-400/50":t.status==="accepted"?"text-green-400 border-green-400/50":"text-red-400 border-red-400/50",children:t.status==="pending"?"Pending":t.status==="accepted"?"Accepted":"Rejected"})]}),e.jsx("div",{className:"text-xs text-muted-foreground",children:t.email}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[t.socialNetwork," · ",t.socialHandle," · ",(s=t.followers)==null?void 0:s.toLocaleString()," followers"]}),t.motivation&&e.jsxs("div",{className:"text-sm text-muted-foreground mt-2 italic",children:['"',t.motivation,'"']}),t.created_date&&e.jsx("div",{className:"text-xs text-muted-foreground mt-1",children:W(new Date(t.created_date),"dd MMM yyyy",{locale:L})})]}),t.status==="pending"&&e.jsxs("div",{className:"flex gap-2",children:[e.jsx(j,{size:"sm",onClick:()=>F(t.id,"accepted"),disabled:m===t.id,className:"bg-green-600 hover:bg-green-700 text-white",children:m===t.id?e.jsx(v,{className:"h-3.5 w-3.5 animate-spin"}):e.jsxs(e.Fragment,{children:[e.jsx(M,{className:"h-3.5 w-3.5 mr-1"}),"Accept"]})}),e.jsxs(j,{size:"sm",variant:"outline",onClick:()=>F(t.id,"refused"),disabled:m===t.id,className:"border-red-500/50 text-red-400 hover:bg-red-500/10",children:[e.jsx(S,{className:"h-3.5 w-3.5 mr-1"}),"Reject"]})]})]})},t.id)})]})]})}export{ee 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.