'use client' import { useState, useEffect } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Separator } from '@/components/ui/separator' import { Input } from '@/components/ui/input' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { DollarSign, TrendingUp, Users, Eye, Filter, Download, Calendar } from 'lucide-react' import { motion } from 'framer-motion' import { toast } from 'sonner' import { DashboardHeader } from '@/components/dashboard/DashboardHeader' interface Commission { id: string amount: number level: number type: 'REFERRAL' | 'LEVEL' | 'BONUS' status: 'PENDING' | 'APPROVED' | 'PAID' | 'CANCELLED' createdAt: string fromUser: { name: string email: string } order?: { id: string total: number } } interface CommissionStats { totalEarnings: number pendingAmount: number thisMonthEarnings: number totalCommissions: number byLevel: { level: number amount: number count: number }[] } export default function CommissionsPage() { const [commissions, setCommissions] = useState([]) const [stats, setStats] = useState(null) const [loading, setLoading] = useState(true) const [filter, setFilter] = useState({ status: 'all', type: 'all', level: 'all', search: '' }) useEffect(() => { fetchCommissions() fetchStats() }, []) const fetchCommissions = async () => { try { const response = await fetch('/api/commissions') if (!response.ok) throw new Error('Failed to fetch commissions') const data = await response.json() setCommissions(data.commissions || []) } catch (error) { console.error('Error fetching commissions:', error) toast.error('Failed to load commissions') } finally { setLoading(false) } } const fetchStats = async () => { try { const response = await fetch('/api/commissions/stats') if (!response.ok) throw new Error('Failed to fetch stats') const data = await response.json() setStats(data) } catch (error) { console.error('Error fetching stats:', error) } } const getStatusColor = (status: string) => { switch (status) { case 'PENDING': return 'bg-yellow-500' case 'APPROVED': return 'bg-blue-500' case 'PAID': return 'bg-green-500' case 'CANCELLED': return 'bg-red-500' default: return 'bg-gray-500' } } const getTypeColor = (type: string) => { switch (type) { case 'REFERRAL': return 'bg-blue-100 text-blue-800' case 'LEVEL': return 'bg-purple-100 text-purple-800' case 'BONUS': return 'bg-green-100 text-green-800' default: return 'bg-gray-100 text-gray-800' } } const filteredCommissions = commissions.filter(commission => { const matchesStatus = filter.status === 'all' || commission.status === filter.status const matchesType = filter.type === 'all' || commission.type === filter.type const matchesLevel = filter.level === 'all' || commission.level.toString() === filter.level const matchesSearch = !filter.search || commission.fromUser.name.toLowerCase().includes(filter.search.toLowerCase()) || commission.fromUser.email.toLowerCase().includes(filter.search.toLowerCase()) return matchesStatus && matchesType && matchesLevel && matchesSearch }) if (loading) { return (
) } return (
{/* Header */} } /> {/* Stats Cards */} {stats && (

Total Earnings

₹{stats.totalEarnings.toFixed(2)}

Pending Amount

₹{stats.pendingAmount.toFixed(2)}

This Month

₹{stats.thisMonthEarnings.toFixed(2)}

Total Commissions

{stats.totalCommissions}

)}
{/* Level Breakdown */} {stats && ( Earnings by Level Commission breakdown by referral levels {stats.byLevel.map((level, index) => (

Level {level.level}

{level.count} commissions

₹{level.amount.toFixed(2)}
))}
)} {/* Commissions List */}
Commission History All your commission earnings and transactions
{/* Filters */}
setFilter(prev => ({ ...prev, search: e.target.value }))} />
{filteredCommissions.length === 0 ? (

No commissions found

Start referring users to earn commissions!

) : (
{filteredCommissions.map((commission, index) => (
{commission.type} Level {commission.level} {commission.status}

{commission.fromUser.name}

{commission.fromUser.email}

{commission.order && (

Order: ₹{commission.order.total.toFixed(2)}

)}

₹{commission.amount.toFixed(2)}

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

))}
)}
) }