import React, { useState, useEffect } from 'react'; import StatCard from './Components/StatCard'; import AccountsTable from './Components/AccountsTable'; import { DollarSign, TrendingDown, TrendingUp, Calendar, ChevronDown } from 'lucide-react'; export default function Dashboard() { const [stats, setStats] = useState({ total_income: 0, total_expense: 0, net_profit: 0, low_stock_count: 0, transactions: [] }); const [branches, setBranches] = useState([]); const [loading, setLoading] = useState(true); const [filterBranch, setFilterBranch] = useState(''); const [currentPage, setCurrentPage] = useState(1); const [paginationData, setPaginationData] = useState(null); const [startDate, setStartDate] = useState(new Date().toISOString().split('T')[0]); const [endDate, setEndDate] = useState(new Date().toISOString().split('T')[0]); useEffect(() => { fetch('/api/branches?status=Active') .then(res => res.json()) .then(data => setBranches(data)) .catch(err => console.error("Error fetching branches:", err)); }, []); useEffect(() => { setLoading(true); const params = new URLSearchParams({ branch_id: filterBranch, start_date: startDate, end_date: endDate, page: currentPage }); fetch(`/api/reports/profit?${params}`) .then(res => res.json()) .then(data => { setStats({ total_income: data.total_income, total_expense: data.total_expense, net_profit: data.net_profit, low_stock_count: data.low_stock_count, transactions: data.transactions || [] }); setPaginationData(data.pagination); setLoading(false); }) .catch(err => { console.error("Error fetching dashboard stats:", err); setLoading(false); }); }, [filterBranch, startDate, endDate, currentPage]); // Reset page when filters change useEffect(() => { setCurrentPage(1); }, [filterBranch, startDate, endDate]); const formatCurrency = (val) => { if (val === undefined || val === null || isNaN(val)) return 'AED 0.00'; return new Intl.NumberFormat('en-AE', { style: 'currency', currency: 'AED' }).format(val); }; return ( <>
{/* Dashboard Title & Filters */}

Owner Dashboard

{/* Branch Selector */}
{/* Date Range */}
From setStartDate(e.target.value)} className="bg-transparent border-none p-0 focus:ring-0 text-gray-700 outline-none w-28" />
To setEndDate(e.target.value)} className="bg-transparent border-none p-0 focus:ring-0 text-gray-700 outline-none w-28" />
{/* Stat Cards Grid */}
{/* Main Content Area */}
setCurrentPage(p)} />
); }