import React, { useEffect, useState } from 'react'; import { FiFilter, FiX, FiRefreshCw } from 'react-icons/fi'; import { Card, CardContent } from '@/components/ui/card'; import axios from 'axios'; import { format } from 'date-fns'; const BASE_URL = "http://localhost:8000/api"; const YatırımContent = ({ onFiltersChange }) => { const [filters, setFilters] = useState({ account: '', status: '', dateRange: { start: '', end: '' }, amountRange: { min: '', max: '' } }); const [showFilters, setShowFilters] = useState(true); const [loading, setLoading] = useState(false); const [autoRefresh, setAutoRefresh] = useState(true); const [accounts, setAccounts] = useState([]); const [error, setError] = useState(null); useEffect(() => { fetchAccounts(); const interval = autoRefresh ? setInterval(fetchAccounts, 30000) : null; return () => interval && clearInterval(interval); }, [autoRefresh]); useEffect(() => { // Notify parent component about filter changes onFiltersChange(filters); }, [filters, onFiltersChange]); const fetchAccounts = async () => { setLoading(true); try { const token = localStorage.getItem('token'); const response = await axios.get(`${BASE_URL}/bank/accounts`, { headers: { 'Authorization': `Bearer ${token}` } }); setAccounts(response.data.data); setError(null); } catch (err) { setError('Failed to load accounts'); console.error(err); } finally { setLoading(false); } }; const handleFilterChange = (key, value) => { setFilters(prev => ({ ...prev, [key]: value })); }; const clearFilters = () => { setFilters({ account: '', status: '', dateRange: { start: '', end: '' }, amountRange: { min: '', max: '' } }); }; const statusOptions = [ { value: '', label: 'Tüm Durumlar' }, { value: 'Beklemede', label: 'Beklemede' }, { value: 'Onaylandı', label: 'Onaylandı' }, { value: 'İptal', label: 'İptal' } ]; return ( {/* Header Section */}
{/* Filter Section */} {showFilters && (
{/* Account Filter */}
{/* Status Filter */}
{/* Date Range Filter */}
handleFilterChange('dateRange', { ...filters.dateRange, start: e.target.value })} className="w-full p-2 text-sm border border-gray-300 rounded-md focus:ring-emerald-500 focus:border-emerald-500" /> handleFilterChange('dateRange', { ...filters.dateRange, end: e.target.value })} className="w-full p-2 text-sm border border-gray-300 rounded-md focus:ring-emerald-500 focus:border-emerald-500" />
{/* Amount Range Filter */}
handleFilterChange('amountRange', { ...filters.amountRange, min: e.target.value })} className="w-full p-2 text-sm border border-gray-300 rounded-md focus:ring-emerald-500 focus:border-emerald-500" /> handleFilterChange('amountRange', { ...filters.amountRange, max: e.target.value })} className="w-full p-2 text-sm border border-gray-300 rounded-md focus:ring-emerald-500 focus:border-emerald-500" />
)} {/* Active Filters Display */} {Object.values(filters).some(value => value && (typeof value === 'string' ? value.length > 0 : Object.values(value).some(v => v)) ) && (
{filters.account && ( Hesap: {accounts.find(a => a.accountId === filters.account)?.bankName || filters.account} )} {filters.status && ( Durum: {filters.status} )} {(filters.dateRange.start || filters.dateRange.end) && ( Tarih: {filters.dateRange.start && format(new Date(filters.dateRange.start), 'dd/MM/yyyy')} {filters.dateRange.end && ` - ${format(new Date(filters.dateRange.end), 'dd/MM/yyyy')}`} )} {(filters.amountRange.min || filters.amountRange.max) && ( Tutar: {filters.amountRange.min ? `${filters.amountRange.min}₺` : '0₺'} {' - '} {filters.amountRange.max ? `${filters.amountRange.max}₺` : '∞'} )}
)} {/* Error Display */} {error && (
{error}
)}
); }; export default YatırımContent;