import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { ChevronDown, ChevronUp, AlertCircle, RefreshCw } from 'lucide-react';
// Ensure HTTPS is always used
const BASE_URL = "https://demfix.group/api";
const CustomAlert = ({ children }) => (
);
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 [retrying, setRetrying] = useState(false);
const fetchAccountStats = async () => {
const token = localStorage.getItem('token');
if (!token) {
setError("Oturum bilgisi bulunamadı. Lütfen tekrar giriş yapın.");
setLoading(false);
return;
}
const headers = {
'Authorization': `Bearer ${token}`,
'Accept': 'application/json'
};
try {
setLoading(true);
setError(null);
const [accountsResponse, investmentsResponse, withdrawalsResponse] = await Promise.all([
axios.get(`${BASE_URL}/bank/accounts`, { headers }),
axios.get(`${BASE_URL}/investment`, { headers }),
axios.get(`${BASE_URL}/withdrawal`, { headers })
]);
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) {
let errorMessage = "İstatistikler yüklenirken bir hata oluştu.";
if (error.response) {
// Server responded with error
if (error.response.status === 401) {
errorMessage = "Oturum süreniz dolmuş. Lütfen tekrar giriş yapın.";
} else if (error.response.status === 403) {
errorMessage = "Bu bilgilere erişim yetkiniz bulunmamaktadır.";
} else {
errorMessage = `Sunucu hatası: ${error.response.data?.detail || error.response.statusText}`;
}
} else if (error.request) {
// No response received
errorMessage = "Sunucuya bağlanılamadı. Lütfen internet bağlantınızı kontrol edin.";
}
setError(errorMessage);
} finally {
setLoading(false);
setRetrying(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)
);
});
const handleRetry = () => {
setRetrying(true);
fetchAccountStats();
};
if (loading) {
return (
);
}
return (
{error &&
{error}}
{isExpanded && !error && (
Hesap Adı |
Banka Adı |
Hesap No |
Bakiye |
Toplam Yatırım |
Yatırım Sayısı |
Toplam Çekim |
Çekim Sayısı |
{filteredStats.map((stat, index) => (
{stat.accountName} |
{stat.bankName} |
{stat.accountNumber} |
{stat.balance.toLocaleString('tr-TR')} ₺
|
{stat.totalInvestment.toLocaleString('tr-TR')} ₺
|
{stat.investmentCount}
|
{stat.totalWithdrawal.toLocaleString('tr-TR')} ₺
|
{stat.withdrawalCount}
|
))}
{filteredStats.length === 0 && (
Aradığınız kriterlere uygun hesap bulunamadı.
)}
)}
);
};
export default AccountStatsPanel;