'use client' import { useEffect, useState } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Users, Package, ShoppingCart, DollarSign, TrendingUp, TrendingDown } from 'lucide-react' import { motion } from 'framer-motion' import { Badge } from '@/components/ui/badge' interface DashboardStats { totalUsers: number totalProducts: number totalOrders: number totalRevenue: number userGrowth: number productGrowth: number orderGrowth: number revenueGrowth: number } interface RecentOrder { id: string total: number status: string createdAt: string user: { name: string email: string } } interface TopProduct { id: string name: string price: number orderCount: number totalRevenue: number } export default function AdminDashboard() { const [stats, setStats] = useState(null) const [recentOrders, setRecentOrders] = useState([]) const [topProducts, setTopProducts] = useState([]) const [loading, setLoading] = useState(true) useEffect(() => { fetchDashboardData() }, []) const fetchDashboardData = async () => { try { const [statsRes, ordersRes, productsRes] = await Promise.all([ fetch('/api/admin/dashboard/stats'), fetch('/api/admin/dashboard/recent-orders'), fetch('/api/admin/dashboard/top-products') ]) const [statsData, ordersData, productsData] = await Promise.all([ statsRes.json(), ordersRes.json(), productsRes.json() ]) setStats(statsData) setRecentOrders(ordersData.orders || []) setTopProducts(productsData.products || []) } catch (error) { console.error('Error fetching dashboard data:', error) } finally { setLoading(false) } } const getStatusColor = (status: string) => { switch (status) { case 'PENDING': return 'bg-yellow-100 text-yellow-800' case 'PAID': return 'bg-blue-100 text-blue-800' case 'SHIPPED': return 'bg-purple-100 text-purple-800' case 'DELIVERED': return 'bg-green-100 text-green-800' case 'CANCELLED': return 'bg-red-100 text-red-800' default: return 'bg-gray-100 text-gray-800' } } if (loading) { return (
) } const dashboardStats = stats ? [ { title: 'Total Users', value: stats.totalUsers.toLocaleString(), change: `${stats.userGrowth >= 0 ? '+' : ''}${stats.userGrowth.toFixed(1)}%`, trend: stats.userGrowth >= 0 ? 'up' : 'down', icon: Users, }, { title: 'Products', value: stats.totalProducts.toLocaleString(), change: `${stats.productGrowth >= 0 ? '+' : ''}${stats.productGrowth.toFixed(1)}%`, trend: stats.productGrowth >= 0 ? 'up' : 'down', icon: Package, }, { title: 'Orders', value: stats.totalOrders.toLocaleString(), change: `${stats.orderGrowth >= 0 ? '+' : ''}${stats.orderGrowth.toFixed(1)}%`, trend: stats.orderGrowth >= 0 ? 'up' : 'down', icon: ShoppingCart, }, { title: 'Revenue', value: `₹${stats.totalRevenue.toLocaleString()}`, change: `${stats.revenueGrowth >= 0 ? '+' : ''}${stats.revenueGrowth.toFixed(1)}%`, trend: stats.revenueGrowth >= 0 ? 'up' : 'down', icon: DollarSign, }, ] : [] return (

Admin Dashboard

Welcome to your admin dashboard

{dashboardStats.map((stat, index) => ( {stat.title}
{stat.value}

{stat.trend === 'up' ? ( ) : ( )} {stat.change} from last month

))}
{/* Recent Orders */} Recent Orders Latest orders from your store {recentOrders.length === 0 ? (

No recent orders

) : (
{recentOrders.map((order) => (

Order #{order.id.slice(-8)}

{order.status}

{order.user.name}

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

₹{order.total.toFixed(2)}
))}
)}
{/* Top Products */} Top Products Best selling products this month {topProducts.length === 0 ? (

No product data available

) : (
{topProducts.map((product, index) => (
#{index + 1}

{product.name}

{product.orderCount} sold

₹{product.price.toFixed(2)}

₹{product.totalRevenue.toFixed(0)} revenue

))}
)}
) }