import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { ChevronDown, ChevronUp } from 'lucide-react'; const BASE_URL = "http://localhost:8000"; const AccountStatsPanel = () => { const [accountStats, setAccountStats] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [isExpanded, setIsExpanded] = useState(true); const [searchTerm, setSearchTerm] = useState(''); const fetchAccountStats = async () => { const token = localStorage.getItem('token'); try { setLoading(true); // Fetch accounts const accountsResponse = await axios.get(`${BASE_URL}/bank/accounts`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); // Fetch investments const investmentsResponse = await axios.get(`${BASE_URL}/investment`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); // Fetch withdrawals const withdrawalsResponse = await axios.get(`${BASE_URL}/withdrawal`, { headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json' } }); // Process and combine the data const stats = accountsResponse.data.data.map(account => { const investments = investmentsResponse.data.data.filter( inv => inv.accountNumber === account.accountNumber ); const withdrawals = withdrawalsResponse.data.data.filter( with_ => with_.accountNumber === account.accountNumber ); return { accountName: account.accountName, accountNumber: account.accountNumber, bankName: account.bankName, balance: account.accountBalance, totalInvestment: investments.reduce((sum, inv) => sum + inv.balance, 0), investmentCount: investments.length, totalWithdrawal: withdrawals.reduce((sum, with_) => sum + with_.balance, 0), withdrawalCount: withdrawals.length }; }); setAccountStats(stats); setError(null); } catch (error) { console.error("Error fetching account statistics:", error); setError("İstatistikler yüklenirken bir hata oluştu. Lütfen daha sonra tekrar deneyin."); } finally { setLoading(false); } }; useEffect(() => { fetchAccountStats(); }, []); const filteredStats = accountStats.filter(stat => { const searchLower = searchTerm.toLowerCase(); return ( stat.accountName.toLowerCase().includes(searchLower) || stat.accountNumber.toLowerCase().includes(searchLower) || stat.bankName.toLowerCase().includes(searchLower) ); }); if (loading) { return (
{error}
Banka Adı | Hesap Adı | Hesap No | Bakiye | Toplam Yatırım | Yatırım Sayısı | Toplam Çekim | Çekim Sayısı |
---|---|---|---|---|---|---|---|
{stat.bankName} | {stat.accountName} | {stat.accountNumber} | {stat.balance.toLocaleString('tr-TR')} ₺ | {stat.totalInvestment.toLocaleString('tr-TR')} ₺ | {stat.investmentCount} | {stat.totalWithdrawal.toLocaleString('tr-TR')} ₺ | {stat.withdrawalCount} |