import React, { useState } from 'react'; import { usePaymentMethodsReport } from '../core/_hooks'; import { PaymentMethodsFilters } from '../core/_models'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { Table } from '@/components/ui/Table'; import { TableColumn } from '@/types'; import { JalaliDateTimePicker } from '@/components/ui/JalaliDateTimePicker'; import { PageContainer, PageTitle } from '@/components/ui/Typography'; import { Pagination } from '@/components/ui/Pagination'; import { Filter, TrendingUp, Users, DollarSign, CreditCard, CheckCircle, XCircle, X } from 'lucide-react'; import { formatWithThousands, persianToEnglish } from '@/utils/numberUtils'; import { PieChart } from '@/components/charts/PieChart'; const formatCurrency = (amount: number) => { return formatWithThousands(amount) + ' تومان'; }; const formatDate = (dateString: string) => { if (!dateString) return '-'; return new Date(dateString).toLocaleDateString('fa-IR', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', }); }; const formatPercentage = (value: number) => { return formatWithThousands(value.toFixed(2)) + '%'; }; const getPaymentTypeLabel = (type: string): string => { const labels: Record = { 'bank-topup': 'افزایش موجودی کیف پول', 'card-to-card': 'پرداخت به روش کارت به کارت', 'debit-rial-wallet': 'پرداخت از کیف ریالی', 'debit-gold18k-wallet': 'پرداخت از کیف طلا', }; return labels[type] || type; }; const PaymentMethodsReportSkeleton = () => ( <> {/* Summary Cards Skeleton */}
{[...Array(4)].map((_, i) => (
))}
{/* Pie Chart and Total Amount Skeleton */}
{/* Payment Type Cards Skeleton */}
{[...Array(5)].map((_, i) => (
{[...Array(5)].map((_, j) => (
))}
))}
{/* Table Skeleton */}
{[...Array(10)].map((_, i) => ( ))} {[...Array(5)].map((_, i) => ( {[...Array(10)].map((_, j) => ( ))} ))}
); const PaymentMethodsReportPage = () => { const [filters, setFilters] = useState({ limit: 50, offset: 0, group_by_user: false, }); const { data, isLoading, error } = usePaymentMethodsReport(filters); const handleFilterChange = (key: keyof PaymentMethodsFilters, value: any) => { setFilters(prev => ({ ...prev, [key]: value, offset: 0, })); }; const handleDateRangeChange = (from: string | undefined, to: string | undefined) => { setFilters(prev => ({ ...prev, date_range: { from, to, }, offset: 0, })); }; const handleNumericFilterChange = (key: 'user_id', raw: string) => { const converted = persianToEnglish(raw).replace(/[^\d]/g, ''); const numeric = converted ? Number(converted) : undefined; handleFilterChange(key, numeric); }; const handlePageChange = (page: number) => { setFilters(prev => ({ ...prev, offset: (page - 1) * prev.limit, })); }; const handleClearFilters = () => { setFilters({ limit: 50, offset: 0, group_by_user: false, }); }; const columns: TableColumn[] = [ { key: 'customer_name', label: 'نام مشتری', align: 'right', }, { key: 'customer_phone', label: 'شماره تماس', align: 'right', }, { key: 'payment_type', label: 'نوع پرداخت', align: 'right', }, { key: 'successful_count', label: 'موفق', align: 'right', }, { key: 'failed_count', label: 'ناموفق', align: 'right', }, { key: 'total_attempts', label: 'کل تلاش‌ها', align: 'right', }, { key: 'total_amount', label: 'مجموع مبلغ', align: 'right', }, { key: 'success_rate', label: 'نرخ موفقیت', align: 'right', }, { key: 'first_used_at', label: 'اولین استفاده', align: 'right', }, { key: 'last_used_at', label: 'آخرین استفاده', align: 'right', }, ]; const tableData = (data?.payment_methods || []).map(method => ({ customer_name: method.customer_name || '-', customer_phone: method.customer_phone || '-', payment_type: getPaymentTypeLabel(method.payment_type), successful_count: formatWithThousands(method.successful_count), failed_count: formatWithThousands(method.failed_count), total_attempts: formatWithThousands(method.total_attempts), total_amount: formatCurrency(method.total_amount), success_rate: formatPercentage(method.success_rate), first_used_at: formatDate(method.first_used_at), last_used_at: formatDate(method.last_used_at), })) || []; const currentPage = Math.floor(filters.offset / filters.limit) + 1; const totalPages = data ? Math.ceil(data.total / filters.limit) : 1; return ( گزارش روش‌های پرداخت {/* Filters */}

فیلترها

handleNumericFilterChange('user_id', e.target.value)} placeholder="مثلاً 456" numeric />
handleDateRangeChange(value, filters.date_range?.to)} placeholder="انتخاب تاریخ شروع" />
handleDateRangeChange(filters.date_range?.from, value)} placeholder="انتخاب تاریخ پایان" />
{/* Summary Cards */} {data?.summary && ( <>

کل تراکنش‌ها

{formatWithThousands(data.summary.total_transactions)}

تراکنش‌های موفق

{formatWithThousands(data.summary.successful_transactions)}

تراکنش‌های ناموفق

{formatWithThousands(data.summary.failed_transactions)}

نرخ موفقیت کلی

{formatPercentage(data.summary.overall_success_rate)}

{/* Payment Type Breakdown */} {Object.keys(data.summary.by_payment_type).length > 0 && ( <> {/* Pie Chart and Total Amount */}
{/* Pie Chart */}

نمودار توزیع روش‌های پرداخت

({ name: getPaymentTypeLabel(type), value: stats.percentage, }))} title="درصد استفاده از هر روش پرداخت" colors={['#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899', '#06b6d4', '#14b8a6', '#f97316']} />
{/* Total Amount Card */}

مجموع مبلغ

{formatCurrency(data.summary.total_amount)}

{/* Payment Type Cards */}

آمار تفکیکی هر روش پرداخت

{Object.entries(data.summary.by_payment_type).map(([type, stats]) => (

{getPaymentTypeLabel(type)}

کل: {formatWithThousands(stats.count)}
موفق: {formatWithThousands(stats.success_count)}
ناموفق: {formatWithThousands(stats.failed_count)}
نرخ موفقیت: {formatPercentage(stats.success_rate)}
درصد از کل: {formatPercentage(stats.percentage)}
))}
)} )} {/* Table */} {isLoading ? ( ) : error ? (

خطا در دریافت داده‌ها

) : ( <>
{data && data.total > 0 && totalPages > 1 && (
)} {data && data.total === 0 && (

داده‌ای یافت نشد

)} )} ); }; export default PaymentMethodsReportPage;