'use client'; import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import api from '@/lib/api'; import { toast } from 'react-toastify'; import type { WalletTransaction, TransactionType } from '@/types'; import { Wallet, Plus, ArrowDownCircle, ArrowUpCircle, RotateCcw, Clock } from 'lucide-react'; const txTypeLabels: Record = { charge: 'شارژ', deduction: 'کسر', refund: 'بازگشت', }; const txTypeColors: Record = { charge: 'text-green-600', deduction: 'text-red-600', refund: 'text-blue-600', }; const txTypeIcons: Record = { charge: , deduction: , refund: , }; export default function WalletPage() { const queryClient = useQueryClient(); const [chargeAmount, setChargeAmount] = useState(''); const [showCharge, setShowCharge] = useState(false); const { data: walletData, isLoading: walletLoading } = useQuery<{ balance: number }>({ queryKey: ['wallet-balance'], queryFn: () => api.get('/billing/wallet').then((r) => r.data), }); const { data: transactions = [], isLoading: txLoading } = useQuery({ queryKey: ['wallet-transactions'], queryFn: () => api.get('/billing/wallet/transactions').then((r) => r.data), }); const chargeMutation = useMutation({ mutationFn: (amount: number) => api.post('/billing/wallet/charge', { amount, description: 'شارژ کیف پول' }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['wallet-balance'] }); queryClient.invalidateQueries({ queryKey: ['wallet-transactions'] }); toast.success('کیف پول شارژ شد'); setChargeAmount(''); setShowCharge(false); }, onError: (err: any) => toast.error(err.response?.data?.message || 'خطا در شارژ'), }); const handleCharge = () => { const amount = Number(chargeAmount); if (!amount || amount < 1000) { toast.error('حداقل مبلغ شارژ ۱,۰۰۰ تومان'); return; } chargeMutation.mutate(amount); }; const formatPrice = (n: number) => Number(n).toLocaleString('fa-IR'); const formatDate = (d: string) => new Date(d).toLocaleDateString('fa-IR', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }); return (

کیف پول

مدیریت موجودی و تراکنش‌ها

{/* Balance Card */}

موجودی فعلی

{walletLoading ? '...' : `${formatPrice(walletData?.balance ?? 0)}`} تومان

{!showCharge && ( )}
{showCharge && (
setChargeAmount(e.target.value)} min={1000} />
)} {/* Quick charge amounts */} {showCharge && (
{[10000, 50000, 100000, 500000].map((amt) => ( ))}
)}
{/* Transactions */}

تاریخچه تراکنش‌ها

{txLoading ? (
در حال بارگذاری...
) : transactions.length === 0 ? (
هنوز تراکنشی ثبت نشده
) : (
{transactions.map((tx) => (
{txTypeIcons[tx.type]}

{txTypeLabels[tx.type]} {tx.description && — {tx.description}}

{formatDate(tx.createdAt)}

{tx.type === 'deduction' ? '−' : '+'}{formatPrice(tx.amount)} ت

مانده: {formatPrice(tx.balanceAfter)} ت

))}
)}
); }