import React, { useState, useEffect } from 'react'; import axios from 'axios'; const BASE_URL = "http://localhost:8000"; const InvestmentForm = ({ show, onClose, title }) => { const [refs, setRefs] = useState([]); const [investment, setInvestment] = useState([]); const [formData, setFormData] = useState({ refId: '', fullName: '', balance: '', username: '', accountName: '', accountNumber: '', method: '', note: '' }); const [isLoading, setIsLoading] = useState(false); const [showModal, setShowModal] = useState(false); const [error, setError] = useState(''); useEffect(() => { const fetchRefs = async () => { const token = localStorage.getItem('token'); try { const response = await axios.get(`${BASE_URL}/refs/`, { headers: { 'Authorization': `Bearer ${token}` } }); setRefs(response.data); } catch (error) { console.error("Error fetching refs:", error); setError(error.response?.data?.detail || "Failed to load references"); } }; fetchRefs(); }, []); const handleChange = (e) => { const { name, value } = e.target; let processedValue = value; // Convert balance to float if it's the balance field if (name === 'balance') { processedValue = parseFloat(value) || 0; } setFormData(prev => ({ ...prev, [name]: processedValue })); }; const handleSubmit = async (e) => { e.preventDefault(); setIsLoading(true); setError(''); const token = localStorage.getItem('token'); // Ensure balance is a number const submissionData = { ...formData, balance: parseFloat(formData.balance) }; try { const response = await axios.post(`${BASE_URL}/investment/`, submissionData, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` } }); setInvestment([...investment, response.data]); setShowModal(true); setFormData({ refId: '', fullName: '', balance: '', username: '', accountName: '', accountNumber: '', method: '', note: '' }); } catch (error) { console.error("Error:", error); setError( error.response?.data?.detail || 'An error occurred during investment creation. Please check all fields.' ); } finally { setIsLoading(false); } }; const handleCloseModal = () => { setShowModal(false); window.location.reload(); }; if (!show) return null; return (