diff --git a/src/pages/statistics/components/RevenueChart.tsx b/src/pages/statistics/components/RevenueChart.tsx index ec89af1..12ca6fc 100644 --- a/src/pages/statistics/components/RevenueChart.tsx +++ b/src/pages/statistics/components/RevenueChart.tsx @@ -1,5 +1,5 @@ -import { ArrowUp, DocumentDownload } from 'iconsax-react' -import { type FC, useState } from 'react' +import { ArrowUp, DocumentDownload, ArrowDown } from 'iconsax-react' +import { type FC, useState, useMemo } from 'react' import { Area, AreaChart, @@ -11,32 +11,79 @@ import { } from 'recharts' import DatePicker from '@/components/DatePicker' import Button from '@/components/Button' +import { useGetPaymentsChart } from '../hooks/useStatsData' +import { formatPrice } from '@/helpers/func' +import PageLoading from '@/components/PageLoading' -interface RevenueChartProps { - data?: Array<{ month: string; online: number; cash: number }> -} - -const RevenueChart: FC = ({ data }) => { +const RevenueChart: FC = () => { const [selectedTab, setSelectedTab] = useState< 'daily' | 'monthly' | 'yearly' >('monthly') + const [startDate, setStartDate] = useState('') + const [endDate, setEndDate] = useState('') - const defaultData = [ - { month: 'فروردین', online: 10000000, cash: 8000000 }, - { month: 'اردیبهشت', online: 15000000, cash: 11000000 }, - { month: 'خرداد', online: 12000000, cash: 9000000 }, - { month: 'تیر', online: 18000000, cash: 13000000 }, - { month: 'مرداد', online: 16000000, cash: 12000000 }, - { month: 'شهریور', online: 22000000, cash: 15000000 }, - { month: 'مهر', online: 20000000, cash: 14000000 }, - { month: 'آبان', online: 17000000, cash: 13000000 }, - { month: 'آذر', online: 14000000, cash: 11000000 }, - { month: 'دی', online: 12000000, cash: 10000000 }, - { month: 'بهمن', online: 18000000, cash: 13000000 }, - { month: 'اسفند', online: 16000000, cash: 12000000 } - ] + const apiParams = useMemo(() => { + const params: { + period?: 'daily' | 'monthly' | 'yearly'; + startDate?: string; + endDate?: string; + } = { + period: selectedTab, + } + if (startDate) params.startDate = startDate + if (endDate) params.endDate = endDate + return params + }, [selectedTab, startDate, endDate]) - const chartData = data || defaultData + const { data: paymentsChartData, isLoading } = useGetPaymentsChart(apiParams) + + const formatDateLabel = (dateString: string, period: 'daily' | 'monthly' | 'yearly'): string => { + const date = new Date(dateString) + const options: Intl.DateTimeFormatOptions = { + year: 'numeric', + } + if (period === 'monthly') { + options.month = 'long' + } else if (period === 'daily') { + options.month = 'short' + options.day = 'numeric' + } + const persianDate = new Intl.DateTimeFormat('fa-IR', options).format(date) + return persianDate + } + + const chartData = useMemo(() => { + if (!paymentsChartData?.data) return [] + return paymentsChartData.data.map((item) => ({ + month: formatDateLabel(item.date, selectedTab), + online: item.online, + cash: item.cash, + })) + }, [paymentsChartData?.data, selectedTab]) + + const xAxisInterval = useMemo(() => { + const dataLength = chartData.length + if (dataLength <= 7) return 0 + if (dataLength <= 15) return 1 + if (dataLength <= 30) return 2 + if (dataLength <= 60) return 4 + return Math.floor(dataLength / 12) + }, [chartData.length]) + + const totalRevenue = useMemo(() => { + return chartData.reduce((sum, item) => sum + item.online + item.cash, 0) + }, [chartData]) + + const revenueChange = useMemo(() => { + if (chartData.length < 2) return null + const currentPeriod = chartData[chartData.length - 1] + const previousPeriod = chartData[chartData.length - 2] + const currentTotal = currentPeriod.online + currentPeriod.cash + const previousTotal = previousPeriod.online + previousPeriod.cash + if (previousTotal === 0) return null + const change = ((currentTotal - previousTotal) / previousTotal) * 100 + return change + }, [chartData]) const formatYAxis = (value: number) => { if (value === 0) return '۰' @@ -55,6 +102,14 @@ const RevenueChart: FC = ({ data }) => { return value.toLocaleString('fa-IR') + ' تومان' } + if (isLoading) { + return ( +
+ +
+ ) + } + return (
{/* Header */} @@ -67,13 +122,13 @@ const RevenueChart: FC = ({ data }) => {
{ }} + placeholder='انتخاب تاریخ شروع' + onChange={setStartDate} /> { }} + placeholder='انتخاب تاریخ پایان' + onChange={setEndDate} />