import React, { useState, useEffect } from 'react'; import { IoIosClose } from "react-icons/io"; import { FaUser, FaMoneyBillWave } from "react-icons/fa"; import { FaMoneyBillTransfer } from "react-icons/fa6"; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; const TransferBox = ({ show, onClose }) => { const [users, setUsers] = useState([]); const [loading, setLoading] = useState(false); const [searchTermSender, setSearchTermSender] = useState(''); const [searchTermRecipient, setSearchTermRecipient] = useState(''); const [selectedSenderAccount, setSelectedSenderAccount] = useState(null); const [selectedRecipientAccount, setSelectedRecipientAccount] = useState(null); const [amount, setAmount] = useState(''); const [note, setNote] = useState(''); const [transferSuccess, setTransferSuccess] = useState(false); const [showSenderList, setShowSenderList] = useState(false); const [showRecipientList, setShowRecipientList] = useState(false); const fetchData = async () => { try { setLoading(true); const response = await axios.get(`${BASE_URL}/bank/accounts`); const sortedUsers = response.data.data.sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); setUsers(sortedUsers); } catch (error) { console.error("Error fetching accounts:", error); } finally { setLoading(false); } }; useEffect(() => { fetchData(); }, []); const filterUsers = (users, searchTerm) => { return users.filter(user => user.accountNumber.includes(searchTerm) || user.accountName.toLowerCase().includes(searchTerm.toLowerCase()) ); }; const handleTransfer = async () => { if (!selectedSenderAccount || !selectedRecipientAccount || !amount) { alert("Please select both accounts and enter an amount."); return; } if (parseFloat(amount) > selectedSenderAccount.accountBalance) { alert("Insufficient balance in sender's account!"); return; } try { const transfer = { sender_account_id: selectedSenderAccount.accountId, receiver_account_id: selectedRecipientAccount.accountId, amount: parseInt(amount), note: note }; const token = localStorage.getItem('token'); await axios.post(`${BASE_URL}/bank/transfer`, transfer, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, }); setTransferSuccess(true); fetchData(); setSearchTermSender(null); setSearchTermRecipient(null); setTimeout(() => { setTransferSuccess(false); onClose(); window.location.reload(); }, 1000); } catch (error) { console.error("Transfer error:", error); alert("An error occurred. Please try again."); } }; const handleAccountSelect = (user, isSender) => { if (isSender) { setSelectedSenderAccount(user); setSearchTermSender(`${user.accountName} (${user.accountNumber})`); setShowSenderList(false); } else { setSelectedRecipientAccount(user); setSearchTermRecipient(`${user.accountName} (${user.accountNumber})`); setShowRecipientList(false); } }; const renderAccountList = (filteredUsers, isSender) => (