import { useState, useEffect } from 'react'; import { ChevronRight, ShoppingBag, Wallet, ArrowRight, RotateCcw, Star } from 'lucide-react'; import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, PieChart, Pie, Cell } from 'recharts'; import { motion } from 'framer-motion'; const StoreDashboard = () => { const [activeTab, setActiveTab] = useState('Sales'); const [timeRange, setTimeRange] = useState('Today'); const [stats, setStats] = useState({ totalSales: 0, activeOrders: 0, dailyCustomers: 0, revenueGrowth: 0 }); const [trendingItems, setTrendingItems] = useState([]); const [salesData, setSalesData] = useState([]); const [insights, setInsights] = useState([]); const [isLoading, setIsLoading] = useState(true); const formatCurrency = (val: any) => { const num = Number(val); return isNaN(num) ? '0' : num.toLocaleString(); }; useEffect(() => { const fetchStats = async () => { try { setIsLoading(true); const response = await fetch('/api/dashboard/stats'); if (response.ok) { const data = await response.json(); console.log('Dashboard data received successfully:', data); // Set basic stats from general stats if available if (data.stats) { setStats({ totalSales: data.stats.totalSales, activeOrders: data.stats.activeOrders, dailyCustomers: data.stats.dailyCustomers, revenueGrowth: data.stats.growth }); } // Override with RIT Canteen specific data if found in overview if (data.storeOverview && data.storeOverview.length > 0) { const ritStore = data.storeOverview.find((s: any) => s.name === 'RIT Canteen'); if (ritStore) { setStats(prev => ({ ...prev, totalSales: ritStore.sale, activeOrders: ritStore.orders, dailyCustomers: ritStore.orders * 0.9, // Approximation })); } } // Set other dynamic data if (data.trendingItems) setTrendingItems(data.trendingItems); if (data.hourlySales) setSalesData(data.hourlySales); if (data.insights) setInsights(data.insights); } } catch (error) { console.error('Error fetching dashboard stats:', error); } finally { setIsLoading(false); } }; fetchStats(); }, []); const pieData = [ { name: 'Full Payment', value: Number(stats.totalSales) || 0, color: '#8b5cf6' }, { name: 'Credit', value: 0, color: '#fbbf24' } ]; if (isLoading) { return (
Analyzing Store Data...
); } return (
{/* Top Header */}

Store Dashboard

Good morning, RIT Canteen

A
{/* Main Stats Grid */}
{/* Sales Chart Section */}
{['Sales', 'Payments'].map(tab => ( ))}
v >= 1000 ? `R${v/1000}k` : `R${v}`} />
{/* Total Sales Gauge */}
{pieData.map((entry, index) => ( ))}

R{formatCurrency(stats.totalSales)}

Full-Payment
Credit
{/* Right Stats Column */}
{/* Filters */}
{['Yesterday', 'Today', 'Week', '30 Days', 'Custom'].map(range => ( ))}
{/* Total Orders Card */}

Total Sales

{stats.activeOrders}

{/* Expenses Card */}

Expenses

₹0

{/* Bottom Section */}
{/* Trending Items */}

Trending Items

{trendingItems.map((item, idx) => ( ))}
Items Sold Quantity
{item.name}

{item.category}

{item.name}

{item.qty}
{/* Store Insights */}

Store Insights

{insights.map((insight, idx) => (

{insight.text}

))}
); }; export default StoreDashboard;