import React, { useState, useEffect } from 'react'; import axios from 'axios'; import ProgressBox from './ConfirmBox'; import ConfirmBox from './ConfirmBox'; import YatırımContent from './YatırımContent'; const BASE_URL = "http://localhost:8000/api"; const InvestmentTable = () => { const [investments, setInvestments] = useState([]); const [filteredInvestments, setFilteredInvestments] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [refs, setRefs] = useState({}); const [users, setUsers] = useState({}); const [updateLoading, setUpdateLoading] = useState(null); const [showModal, setShowModal] = useState(false); const [showModal2, setShowModal2] = useState(false); const [showModal3, setShowModal3] = useState(false); const [selectedInvestmentId, setSelectedInvestmentId] = useState(null); const [currentUser, setCurrentUser] = useState(null); useEffect(() => { const fetchData = async () => { await Promise.all([ fetchInvestments(), fetchRefs(), fetchUsers(), fetchCurrentUser() ]); }; fetchData(); }, []); const fetchInvestments = async () => { const token = localStorage.getItem('token'); try { setLoading(true); const response = await axios.get(`${BASE_URL}/investment/`, { headers: { 'Authorization': `Bearer ${token}` } }); const sortedInvestments = response.data.data.sort((a, b) => new Date(b.transactionDate) - new Date(a.transactionDate) ); setInvestments(sortedInvestments); setFilteredInvestments(sortedInvestments); setError(null); } catch (error) { console.error("Error fetching investments:", error); setError("Yatırımlar 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 handleTakeOwnership = async (investmentId) => { const token = localStorage.getItem('token'); try { setUpdateLoading(investmentId); await axios.post(`${BASE_URL}/investment/${investmentId}/take-ownership`, null, { headers: { 'Authorization': `Bearer ${token}` } }); await fetchInvestments(); setShowModal(false); } catch (error) { console.error("Error taking ownership:", error); setError("Yatırım sahiplenirken bir hata oluştu."); } finally { setUpdateLoading(null); } }; const handleStatusUpdate = async (investmentId, newStatus) => { const token = localStorage.getItem('token'); setUpdateLoading(investmentId); try { const investment = investments.find(inv => inv.id === investmentId); if (!investment.userID) { await handleTakeOwnership(investmentId); } await axios.patch(`${BASE_URL}/investment/${investmentId}`, { investmentStatus: newStatus, note: newStatus === 'Onaylandı' ? 'Yatırım onaylandı' : newStatus === 'İptal' ? 'Yatırım iptal edildi' : 'Yatırım beklemede', }, { headers: { 'Accept': 'application/json', 'Authorization': `Bearer ${token}` } }); await fetchInvestments(); } catch (error) { setError("Bu Yatırımı Onaylamaya Yetkiniz Yok."); } finally { setUpdateLoading(null); } }; const handleFiltersChange = (filters) => { let filtered = [...investments]; // Filter by account if (filters.account) { filtered = filtered.filter(inv => inv.accountNumber === filters.account); } // Filter by status if (filters.status) { filtered = filtered.filter(inv => inv.investmentStatus === filters.status); } // Filter by date range if (filters.dateRange.start || filters.dateRange.end) { filtered = filtered.filter(inv => { const invDate = new Date(inv.transactionDate); const startDate = filters.dateRange.start ? new Date(filters.dateRange.start) : null; const endDate = filters.dateRange.end ? new Date(filters.dateRange.end) : null; if (startDate && endDate) { return invDate >= startDate && invDate <= endDate; } else if (startDate) { return invDate >= startDate; } else if (endDate) { return invDate <= endDate; } return true; }); } // Filter by amount range if (filters.amountRange.min || filters.amountRange.max) { filtered = filtered.filter(inv => { const min = filters.amountRange.min ? parseFloat(filters.amountRange.min) : 0; const max = filters.amountRange.max ? parseFloat(filters.amountRange.max) : Infinity; return inv.balance >= min && inv.balance <= max; }); } setFilteredInvestments(filtered); }; 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'; } }; if (loading) { return (
Hata!
{error}
ID | Site | Adı Soyadı | Miktar | Kullanıcı Adı | Yöntem | Hesap Adı | Hesap No | İşlem Tarihi | İşlemi Yapan | Durum | İşlemler |
---|---|---|---|---|---|---|---|---|---|---|---|
{investment.id} | {refs[investment.refId] || '-'} | {investment.fullName} | {investment.balance.toLocaleString('tr-TR')} ₺ | {investment.username} | {investment.method} | {investment.accountName} | TR {investment.accountNumber} | {new Date(investment.transactionDate).toLocaleString("tr-TR")} | {users[investment.userID] || 'Seçilmedi'} | {investment.investmentStatus} |
{investment.investmentStatus === 'Beklemede' && (
<>
{!investment.userID && (
)}
{investment.userID === currentUser?.id && (
<>
>
)}
{investment.userID && investment.userID !== currentUser?.id && (
)}
>
)}
|