import { useState, useEffect } from 'react'; import { Target, ChevronRight, ShoppingBag, ArrowRight, Wallet } from 'lucide-react'; import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, PieChart, Pie, Cell } from 'recharts'; import { motion } from 'framer-motion'; const Dashboard = () => { const [activeTab, setActiveTab] = useState('Sales'); const [timeRange, setTimeRange] = useState('Today'); const [customDates, setCustomDates] = useState({ from: '', to: '' }); const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(true); const getRangeDates = (range: string) => { const now = new Date(); const start = new Date(); const end = new Date(); // Set end to end of today end.setHours(23, 59, 59, 999); switch (range) { case 'Today': start.setHours(0, 0, 0, 0); break; case 'Yesterday': start.setDate(now.getDate() - 1); start.setHours(0, 0, 0, 0); end.setDate(now.getDate() - 1); end.setHours(23, 59, 59, 999); break; case 'Week': start.setDate(now.getDate() - 7); start.setHours(0, 0, 0, 0); break; case '30 Days': start.setDate(now.getDate() - 30); start.setHours(0, 0, 0, 0); break; case 'Custom': if (customDates.from && customDates.to) { const from = new Date(customDates.from); from.setHours(0, 0, 0, 0); const to = new Date(customDates.to); to.setHours(23, 59, 59, 999); return { from: from.toISOString(), to: to.toISOString() }; } return null; default: start.setHours(0, 0, 0, 0); } return { from: start.toISOString(), to: end.toISOString() }; }; useEffect(() => { const fetchData = async () => { try { setIsLoading(true); const range = getRangeDates(timeRange); let url = '/api/dashboard/stats'; if (range) { const params = new URLSearchParams(); params.append('from', range.from); params.append('to', range.to); url += `?${params.toString()}`; } const response = await fetch(url); if (response.ok) { const result = await response.json(); setData(result); } } catch (error) { console.error('Error fetching dashboard data:', error); } finally { setIsLoading(false); } }; fetchData(); }, [timeRange, customDates]); if (isLoading || !data) { return (
Loading Business Intelligence...
); } const { stats, storeOverview, hourlySales, insights } = data; const pieData = [ { name: 'Full Payment', value: stats.periodRevenue, color: '#8b5cf6' }, { name: 'Credit', value: 0, color: '#fbbf24' } ]; return (
{/* Top Header */}

Dashboard

Good morning

i The default time settings for the merchant view are from 12:00 AM to 11:59 PM.

{/* Main Stats Grid */}
{/* Sales Chart Section */}
{['Sales', 'Payments'].map(tab => ( ))}
{timeRange}'s Revenue
v >= 1000 ? `₹${v/1000}k` : `₹${v}`} />
{/* Total Sales Gauge */}

Total Revenue

{pieData.map((entry, index) => ( ))}

₹{stats.periodRevenue.toLocaleString()}

{timeRange === 'Today' ? 'Today' : timeRange}

Total: ₹{stats.totalSales.toLocaleString()}

{pieData.map(item => (
{item.name}
))}
{/* Right Stats Column */}
{/* Filters */}
{['Yesterday', 'Today', 'Week', '30 Days', 'Custom'].map(range => ( ))}
{timeRange === 'Custom' && (
setCustomDates({ ...customDates, from: e.target.value })} className="flex-1 bg-white border border-slate-200 rounded-lg px-2 py-1.5 text-[10px] font-bold text-slate-600 outline-none focus:border-[#0f4475]" /> to setCustomDates({ ...customDates, to: e.target.value })} className="flex-1 bg-white border border-slate-200 rounded-lg px-2 py-1.5 text-[10px] font-bold text-slate-600 outline-none focus:border-[#0f4475]" />
)}
{/* Total Orders Card */}

Total Orders

{stats.activeOrders}

{/* Expenses Card */}

Total Expenses

₹0

{/* Bottom Section */}
{/* Store Insights */}

Business Intelligence

{insights.map((insight: string, idx: number) => (

{insight}

))}
{/* Store Overview */}

Store Performance

{(storeOverview.length > 0 ? storeOverview : [{name: 'No active sales currently', sale: 0, orders: 0, taxes: 0, purchase: 0}]).map((store: any, idx: number) => (

{store.name}

Gross Sale

₹{Number(store.sale).toLocaleString()}

Volume

{store.orders} Orders

Taxation

₹{store.taxes}

Procurement

₹{store.purchase}

))}
); }; export default Dashboard;