import React, { useState, useEffect } from 'react'; import axios from 'axios'; import PopBox from './PopBox'; import '../css/bankForm.css'; const BASE_URL = "http://localhost:8000"; const BankaUpdateForm = ({ show, onClose, title, accountId }) => { const [formData, setFormData] = useState({ bankName: '', accountName: '', accountNumber: '', accountExplanation: '', isActive: false, isBlocked: false, isInvest: false, isPull: false, minInvest: '', maxInvest: '', minPull: '', maxPull: '', maxTransfer: '', maxAmount: '' }); const [showModal, setShowModal] = useState(false); const [isFormVisible, setIsFormVisible] = useState(true); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (accountId) { fetchAccountDetails(); } }, [accountId]); const fetchAccountDetails = async () => { setIsLoading(true); setError(null); try { const token = localStorage.getItem('token'); if (!token) { throw new Error('No authentication token found'); } const response = await axios.get(`${BASE_URL}/bank/accounts`, { headers: { 'Authorization': `Bearer ${token}` } }); const account = response.data.data.find(acc => acc.accountId === accountId); if (!account) { throw new Error('Account not found'); } setFormData({ bankName: account.bankName || '', accountName: account.accountName || '', accountNumber: account.accountNumber || '', accountExplanation: account.accountExplanation || '', isActive: Boolean(account.isActive), isBlocked: Boolean(account.isBlocked), isInvest: Boolean(account.isInvest), isPull: Boolean(account.isPull), minInvest: account.minInvest ? Number(account.minInvest) : '', maxInvest: account.maxInvest ? Number(account.maxInvest) : '', minPull: account.minPull ? Number(account.minPull) : '', maxPull: account.maxPull ? Number(account.maxPull) : '', maxTransfer: account.maxTransfer ? Number(account.maxTransfer) : '', maxAmount: account.maxAmount ? Number(account.maxAmount) : '' }); } catch (error) { console.error('Error fetching account details:', error); setError(error.message || 'Failed to fetch account details'); } finally { setIsLoading(false); } }; const handleUpdate = async (e) => { e.preventDefault(); setIsLoading(true); setError(null); try { const token = localStorage.getItem('token'); if (!token) { throw new Error('No authentication token found'); } const updatedFields = Object.entries(formData).reduce((acc, [key, value]) => { if (typeof value === 'boolean' || typeof value === 'number' || (typeof value === 'string' && value.trim() !== '')) { acc[key] = value; } return acc; }, {}); await axios.patch( `${BASE_URL}/bank/${accountId}`, updatedFields, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` } } ); setShowModal(true); } catch (error) { console.error('Error updating account:', error); setError(error.message || 'Failed to update account'); } finally { setIsLoading(false); } }; const handleInputChange = (e) => { const { name, value, type, checked } = e.target; setFormData(prev => ({ ...prev, [name]: type === 'checkbox' ? checked : type === 'number' ? (value === '' ? '' : Number(value)) : value })); }; const resetForm = () => { if (accountId) { fetchAccountDetails(); } else { setFormData({ bankName: '', accountName: '', accountNumber: '', accountExplanation: '', isActive: false, isBlocked: false, isInvest: false, isPull: false, minInvest: '', maxInvest: '', minPull: '', maxPull: '', maxTransfer: '', maxAmount: '' }); } }; const handleCloseModal = () => { setShowModal(false); setIsFormVisible(false); onClose(); }; if (!show) { return null; } if (isLoading) { return (

Loading...

); } return ( <> {isFormVisible && (
{error && (
Error! {error}
)}

{title}

{/* Bank Information Card */}

Hesap Bilgileri

{/* Bank Selection */}
{/* Account Name */}
{/* Account Number */}
{/* Account Explanation */}
{/* Is Active Switch */}
Hesap Aktif
{/* Account Settings Card */}

Hesap Ayarları

{/* Toggle Switches */}
{/* Is Blocked */}
Hesap Bloke mi?
{/* Is Investment */}
Yatırım Aktif mi?
{/* Is Pull */}
Çekim Aktif mi?
{/* Amount Inputs */}
{/* Max Investment */}
{/* Min Investment */}
{/* Max Pull */}
{/* Min Pull */}
{/* Max Transfer */}
{/* Max Amount */}
{/* Action Buttons */}
{/* Form Info */}

* Lütfen bilgileri eksiksiz doldurunuz. *

Anlayışınız için teşekkür ederiz.

DemSettle ®

)} {/* Success Modal */} {showModal && ( )} ); }; export default BankaUpdateForm;