'use client' import { useEffect, useState } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' import { DollarSign, TrendingUp, Calendar, Download } from 'lucide-react' import { Loading } from '@/components/ui/loading' interface Commission { id: string amount: number level: number type: string status: string createdAt: string fromUser?: { name: string email: string } } interface EarningsData { totalEarnings: number currentBalance: number totalWithdrawn: number monthlyEarnings: number commissions: Commission[] } export default function EarningsPage() { const [earningsData, setEarningsData] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { fetchEarningsData() }, []) const fetchEarningsData = async () => { try { const response = await fetch('/api/dashboard/commissions') if (response.ok) { const data = await response.json() setEarningsData({ totalEarnings: data.totalEarnings || 0, currentBalance: data.currentBalance || 0, totalWithdrawn: data.totalWithdrawn || 0, monthlyEarnings: data.monthlyEarnings || 0, commissions: data.commissions || [], }) } } catch (error) { console.error('Error fetching earnings data:', error) } finally { setLoading(false) } } if (loading) { return (
) } return (

Earnings & Reports

Track your commission earnings and financial performance

{/* Earnings Stats */}
Total Earnings
₹{earningsData?.totalEarnings.toFixed(2) || '0.00'}

Lifetime earnings

Current Balance
₹{earningsData?.currentBalance.toFixed(2) || '0.00'}

Available for withdrawal

Monthly Earnings
₹{earningsData?.monthlyEarnings.toFixed(2) || '0.00'}

This month

Total Withdrawn
₹{earningsData?.totalWithdrawn.toFixed(2) || '0.00'}

All time withdrawals

Overview Commission History Payout Requests Reports
Earnings Breakdown Commission distribution by level
{[1, 2, 3, 4, 5].map((level) => { const percentage = level === 1 ? 40 : level === 2 ? 25 : level === 3 ? 20 : level === 4 ? 10 : 5 const amount = (earningsData?.totalEarnings || 0) * (percentage / 100) return (
{level}
Level {level} Commission
₹{amount.toFixed(2)}
{percentage}%
) })}
Quick Actions Manage your earnings
Commission History Your recent commission earnings {(earningsData?.commissions || []).length === 0 ? (

No commissions yet

Start referring customers to earn commissions

) : (
{(earningsData?.commissions || []).map((commission) => (

Level {commission.level} {commission.type} Commission {commission.fromUser && ` from ${commission.fromUser.name}`}

{new Date(commission.createdAt).toLocaleDateString()}

+₹{commission.amount.toFixed(2)}
{commission.status}
))}
)}
Payout Requests Manage your withdrawal requests

No payout requests

Request a payout when you're ready to withdraw your earnings

Earnings Reports Download detailed reports of your earnings
) }