1import{i as M,f as z,r as H,b as B,c as O,t as P,g as v,j as e,e as Q,B as w}from"./main-DaCJV-qv.js";import{T as C,a as S,b as c,c as d,d as A,e as r}from"./table-CN4F4qeH.js";import{C as R}from"./checkbox-CCdXlQuY.js";import{C as f,a as j,b as p,d as g}from"./card-Dr429RDF.js";import"./index-CXUJWoUO.js";import"./index-8Lin4XP7.js";const U=[["line",{x1:"12",x2:"12",y1:"2",y2:"22",key:"7eqyqh"}],["path",{d:"M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6",key:"1b0p4s"}]],G=M("dollar-sign",U);function Y(){const F=z(),[n,m]=H.useState(new Set),{data:N,isLoading:k,error:D}=B({queryKey:["admin-commissions"],queryFn:async()=>{const s=await fetch(`${v()}/v1/admin/commissions`,{credentials:"include"});if(!s.ok)throw new Error("Failed to fetch commissions");return(await s.json()).data}}),x=O({mutationFn:async s=>{const t=await fetch(`${v()}/v1/admin/commissions/mark-paid`,{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include",body:JSON.stringify({commissionIds:s})});if(!t.ok){const i=await t.json();throw new Error(i.error||"Failed to mark commissions as paid")}return t.json()},onSuccess:()=>{F.invalidateQueries({queryKey:["admin-commissions"]}),m(new Set),P.success("Commissions marked as paid")},onError:s=>{P.error(s.message||"Failed to mark commissions as paid")}}),E=s=>{m(t=>{const i=new Set(t);return i.has(s)?i.delete(s):i.add(s),i})},I=s=>{const t=s.filter(a=>a.status==="pending").map(a=>a.id),i=t.every(a=>n.has(a));m(a=>{const h=new Set(a);return i?t.forEach(u=>h.delete(u)):t.forEach(u=>h.add(u)),h})},T=()=>{n.size!==0&&x.mutate(Array.from(n))};if(k)return e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(Q,{className:"h-6 w-6 animate-spin text-muted-foreground"})});if(D)return e.jsx("div",{className:"py-12 text-center text-muted-foreground",children:"Failed to load commissions. Please try again."});const l=N?.filter(s=>s.status==="pending")||[],o=N?.filter(s=>s.status==="paid")||[],L=l.reduce((s,t)=>(s[t.affiliateId]||(s[t.affiliateId]={affiliateId:t.affiliateId,affiliateName:t.affiliateName,affiliatePaypalEmail:t.affiliatePaypalEmail,commissions:[],totalAmount:0}),s[t.affiliateId].commissions.push(t),s[t.affiliateId].totalAmount+=parseFloat(t.amount),s),{}),y=Object.values(L),$=l.reduce((s,t)=>s+parseFloat(t.amount),0),q=o.reduce((s,t)=>s+parseFloat(t.amount),0),b=Array.from(n).reduce((s,t)=>{const i=l.find(a=>a.id===t);return s+(i?parseFloat(i.amount):0)},0);return e.jsxs("div",{className:"space-y-8",children:[e.jsxs("div",{className:"grid gap-4 sm:grid-cols-3",children:[e.jsxs(f,{children:[e.jsx(j,{className:"pb-2",children:e.jsx(p,{className:"text-sm font-medium text-muted-foreground",children:"Pending Payouts"})}),e.jsxs(g,{children:[e.jsxs("div",{className:"text-2xl font-bold",children:["$",$.toFixed(2)]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[l.length," commission(s)"]})]})]}),e.jsxs(f,{children:[e.jsx(j,{className:"pb-2",children:e.jsx(p,{className:"text-sm font-medium text-muted-foreground",children:"Total Paid"})}),e.jsxs(g,{children:[e.jsxs("div",{className:"text-2xl font-bold",children:["$",q.toFixed(2)]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[o.length," commission(s)"]})]})]}),e.jsxs(f,{children:[e.jsx(j,{className:"pb-2",children:e.jsx(p,{className:"text-sm font-medium text-muted-foreground",children:"Selected to Pay"})}),e.jsxs(g,{children:[e.jsxs("div",{className:"text-2xl font-bold",children:["$",b.toFixed(2)]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[n.size," commission(s)"]})]})]})]}),n.size>
10&&e.jsxs("div",{className:"flex items-center gap-4 rounded-lg border border-primary/20 bg-primary/5 p-4",children:[e.jsx(G,{className:"h-5 w-5 text-primary"}),e.jsxs("div",{className:"flex-1",children:[e.jsxs("p",{className:"font-medium",children:[n.size," commission(s) selected ($",b.toFixed(2),")"]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Mark these as paid after sending PayPal payments"})]}),e.jsx(w,{onClick:T,disabled:x.isPending,children:x.isPending?"Processing...":"Mark as Paid"})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsx("h3",{className:"text-lg font-medium",children:"Pending Payouts"}),y.length>0?e.jsx("div",{className:"space-y-6",children:y.map(s=>{const i=s.commissions.filter(a=>a.status==="pending").every(a=>n.has(a.id));return e.jsxs("div",{className:"rounded-lg border",children:[e.jsxs("div",{className:"flex items-center justify-between border-b p-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.affiliateName}),e.jsx("p",{className:"text-sm text-muted-foreground",children:s.affiliatePaypalEmail})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"text-right",children:[e.jsxs("p",{className:"font-semibold",children:["$",s.totalAmount.toFixed(2)]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[s.commissions.length," commission(s)"]})]}),e.jsx(w,{variant:"outline",size:"sm",onClick:()=>I(s.commissions),children:i?"Deselect All":"Select All"})]})]}),e.jsxs(C,{children:[e.jsx(S,{children:e.jsxs(c,{children:[e.jsx(d,{className:"w-12"}),e.jsx(d,{children:"Period"}),e.jsx(d,{children:"Amount"}),e.jsx(d,{children:"Created"})]})}),e.jsx(A,{children:s.commissions.map(a=>e.jsxs(c,{children:[e.jsx(r,{children:e.jsx(R,{checked:n.has(a.id),onCheckedChange:()=>E(a.id)})}),e.jsxs(r,{children:[new Date(a.periodStart).toLocaleDateString()," -"," ",new Date(a.periodEnd).toLocaleDateString()]}),e.jsxs(r,{className:"font-medium",children:["$",parseFloat(a.amount).toFixed(2)," ",a.currency.toUpperCase()]}),e.jsx(r,{className:"text-muted-foreground",children:new Date(a.createdAt).toLocaleDateString()})]},a.id))})]})]},s.affiliateId)})}):e.jsx("div",{className:"rounded-md border p-8 text-center text-muted-foreground",children:"No pending commissions"})]}),o.length>0&&e.jsxs("div",{className:"space-y-4",children:[e.jsx("h3",{className:"text-lg font-medium",children:"Recently Paid"}),e.jsx("div",{className:"rounded-md border",children:e.jsxs(C,{children:[e.jsx(S,{children:e.jsxs(c,{children:[e.jsx(d,{children:"Affiliate"}),e.jsx(d,{children:"Period"}),e.jsx(d,{children:"Amount"}),e.jsx(d,{children:"Paid At"})]})}),e.jsx(A,{children:o.slice(0,10).map(s=>e.jsxs(c,{children:[e.jsx(r,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.affiliateName}),e.jsx("p",{className:"text-sm text-muted-foreground",children:s.affiliatePaypalEmail})]})}),e.jsxs(r,{children:[new Date(s.periodStart).toLocaleDateString()," -"," ",new Date(s.periodEnd).toLocaleDateString()]}),e.jsxs(r,{className:"font-medium",children:["$",parseFloat(s.amount).toFixed(2)," ",s.currency.toUpperCase()]}),e.jsx(r,{className:"text-muted-foreground",children:s.paidAt?new Date(s.paidAt).toLocaleDateString():"-"})]},s.id))})]})})]})]})}export{Y as component};
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.