import React, { useState, useEffect } from 'react'; import axios from 'axios'; import ProgressBox from './ConfirmBox'; import ConfirmBox from './ConfirmBox'; const BASE_URL = "http://localhost:8000"; const AccountSelectionModal = ({ show, onClose, onConfirm, accounts }) => { const [selectedAccount, setSelectedAccount] = useState(''); const [, setMessage] = useState(''); if (!show) return null; return (

Çekimi Yapacağınız Hesabı Seçin

); }; const WithdrawalTable = () => { const [withdrawals, setWithdrawals] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchTerm, setSearchTerm] = useState(''); const [statusFilter, setStatusFilter] = useState('all'); const [refs, setRefs] = useState({}); const [users, setUsers] = useState({}); const [accounts, setAccounts] = useState([]); const [updateLoading, setUpdateLoading] = useState(null); const [showModal, setShowModal] = useState(false); const [showModal3, setShowModal3] = useState(false); const [showAccountSelection, setShowAccountSelection] = useState(false); const [selectedWithdrawalId, setSelectedWithdrawalId] = useState(null); const [selectedWithdrawalForApproval, setSelectedWithdrawalForApproval] = useState(null); const [currentUser, setCurrentUser] = useState(null); const fetchWithdrawals = async () => { const token = localStorage.getItem('token'); try { setLoading(true); const response = await axios.get(`${BASE_URL}/withdrawal/`, { headers: { 'Authorization': `Bearer ${token}` } }); const sortedWithdrawals = response.data.data.sort((a, b) => new Date(b.transactionDate) - new Date(a.transactionDate) ); setWithdrawals(sortedWithdrawals); setError(null); } catch (error) { console.error("Error fetching withdrawals:", error); setError("Çekimler yüklenirken bir hata oluştu. Lütfen tekrar deneyin."); } finally { setLoading(false); } }; const fetchCurrentUser = async () => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/users/me`, { headers: { 'Authorization': `Bearer ${token}` } }); setCurrentUser(response.data); } catch (error) { console.error("Error fetching current user:", error); } }; const fetchRefs = async () => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/refs/`, { headers: { 'Authorization': `Bearer ${token}` } }); const refsMap = {}; response.data.forEach(ref => { refsMap[ref.refId] = ref.refName; }); setRefs(refsMap); } catch (error) { console.error("Error fetching refs:", error); } }; const fetchUsers = async () => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/users`, { headers: { 'Authorization': `Bearer ${token}` } }); const usersMap = {}; response.data.data.forEach(user => { usersMap[user.id] = user.username; }); setUsers(usersMap); } catch (error) { console.error("Error fetching users:", error); } }; const fetchAccounts = async () => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/bank/accounts`, { headers: { 'Authorization': `Bearer ${token}` } }); setAccounts(response.data.data); } catch (error) { console.error("Error fetching accounts:", error); } }; useEffect(() => { fetchWithdrawals(); fetchRefs(); fetchUsers(); fetchAccounts(); fetchCurrentUser(); const interval = setInterval(() => { fetchWithdrawals(); }, 10000); return () => clearInterval(interval); }, []); const handleTakeOwnership = async (withdrawalId) => { const token = localStorage.getItem('token'); try { setUpdateLoading(withdrawalId); await axios.post(`${BASE_URL}/withdrawal/${withdrawalId}/take-ownership`, null, { headers: { 'Authorization': `Bearer ${token}` } }); await fetchWithdrawals(); setShowModal(false); } catch (error) { console.error("Error taking ownership:", error); setError("Çekim sahiplenirken bir hata oluştu."); } finally { setUpdateLoading(null); } }; const handleStatusUpdate = async (withdrawalId, newStatus, accountName = null, accountNumber = null) => { const token = localStorage.getItem('token'); setUpdateLoading(withdrawalId); try { const withdrawal = withdrawals.find(w => w.id === withdrawalId); if (!withdrawal.userID) { await handleTakeOwnership(withdrawalId); } const updateData = { withdrawalStatus: newStatus, note: newStatus === 'Onaylandı' ? 'Çekim onaylandı' : newStatus === 'İptal' ? 'Çekim iptal edildi' : 'Çekim beklemede', }; if (accountName && accountNumber) { updateData.accountName = accountName; updateData.accountNumber = accountNumber; } await axios.patch(`${BASE_URL}/withdrawal/${withdrawalId}`, updateData, { headers: { 'Accept': 'application/json', 'Authorization': `Bearer ${token}` } }); await fetchWithdrawals(); setShowModal3(false); setShowAccountSelection(false); } catch (error) { setError("Bu Çekimi Onaylamaya Yetkiniz Yok."); } finally { setUpdateLoading(null); } }; const handleApprovalClick = (withdrawalId) => { setSelectedWithdrawalForApproval(withdrawalId); setShowAccountSelection(true); }; const getStatusBadgeClass = (status) => { switch (status) { case 'Onaylandı': return 'bg-emerald-600 text-slate-200'; case 'İptal': return 'bg-red-600 text-slate-200'; case 'Beklemede': default: return 'bg-yellow-600 text-slate-100'; } }; const getStatusTableClass = (status) => { switch (status) { case 'Onaylandı': return 'bg-emerald-200 text-white'; case 'İptal': return 'bg-red-200 text-white'; case 'Beklemede': return 'bg-amber-100 text-white'; default: return 'bg-gray-100 text-slate-100'; } }; const handleCloseShowModal = () => { setShowModal(false); setSelectedWithdrawalId(null); }; const handleCloseShowModal3 = () => { setShowModal3(false); setSelectedWithdrawalId(null); }; const filteredWithdrawals = withdrawals.filter(withdrawal => { const searchLower = searchTerm.toLowerCase(); const matchesSearch = ( withdrawal.id.toString().includes(searchLower) || (refs[withdrawal.refId] || '').toLowerCase().includes(searchLower) || withdrawal.fullName.toLowerCase().includes(searchLower) || withdrawal.username.toLowerCase().includes(searchLower) || withdrawal.accountNumber.includes(searchLower) || withdrawal.targetAccountNumber.includes(searchLower) ); const matchesStatus = statusFilter === 'all' || withdrawal.withdrawalStatus === statusFilter; return matchesSearch && matchesStatus; }); if (loading) { return (
); } return (
setSearchTerm(e.target.value)} />
{error && (

Hata!

{error}

)}
{filteredWithdrawals.map((withdrawal) => ( ))}
ID Site Adı Soyadı Miktar Kullanıcı Adı Yöntem Kaynak Hesap Kaynak No Hedef Hesap Hedef No İşlem Tarihi Sonuç Tarihi İşlemi Yapan Durum İşlemler
{withdrawal.id} {refs[withdrawal.refId] || '-'} {withdrawal.fullName} {withdrawal.balance.toLocaleString()} ₺ {withdrawal.username} {withdrawal.method} {withdrawal.accountName} TR {withdrawal.accountNumber} {withdrawal.targetAccountName} TR {withdrawal.targetAccountNumber} {new Date(withdrawal.transactionDate).toLocaleString("tr-TR")} {withdrawal.resultDate && new Date(withdrawal.resultDate).toLocaleString("tr-TR")} {users[withdrawal.userID] || 'Seçilmedi'} {withdrawal.withdrawalStatus}
{withdrawal.withdrawalStatus === 'Beklemede' && ( <> {!withdrawal.userID && ( )} {withdrawal.userID && (
{withdrawal.userID === currentUser?.id ? ( <> ) : ( )}
)} )}
{filteredWithdrawals.length === 0 && (
Çekim Bulunamadı.
)} {/* Take Ownership Modal */} {showModal && ( handleTakeOwnership(selectedWithdrawalId)} onClose={handleCloseShowModal} /> )} {/* Account Selection Modal */} setShowAccountSelection(false)} accounts={accounts} onConfirm={(accountName, accountNumber) => { handleStatusUpdate(selectedWithdrawalForApproval, 'Onaylandı', accountName, accountNumber); }} /> {/* Reject Modal */} {showModal3 && ( { handleStatusUpdate(selectedWithdrawalId, 'İptal'); setShowModal3(false); }} onClose={handleCloseShowModal3} /> )}
); }; export default WithdrawalTable;