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') { // Remove any non-numeric characters except decimal point const numericValue = value.replace(/[^\d.]/g, ''); processedValue = numericValue ? parseFloat(numericValue) : ''; } setFormData(prev => ({ ...prev, [name]: processedValue })); }; const validateForm = () => { if (!formData.refId) return "Referans seçimi zorunludur"; if (!formData.fullName) return "Ad Soyad alanı zorunludur"; if (!formData.balance || formData.balance <= 0) return "Geçerli bir miktar giriniz"; if (!formData.username) return "Kullanıcı adı zorunludur"; if (!formData.accountName) return "Hesap adı zorunludur"; if (!formData.accountNumber) return "Hesap numarası zorunludur"; if (!formData.method) return "Ödeme yöntemi zorunludur"; return null; }; const handleSubmit = async (e) => { e.preventDefault(); // Validate form const validationError = validateForm(); if (validationError) { setError(validationError); return; } setIsLoading(true); setError(''); const token = localStorage.getItem('token'); // Format the data according to the API requirements const submissionData = { refId: formData.refId, fullName: formData.fullName.trim(), balance: parseFloat(formData.balance), username: formData.username.trim(), accountName: formData.accountName.trim(), accountNumber: formData.accountNumber.trim(), method: formData.method, note: formData.note.trim() || '' // Ensure note is never undefined }; try { const response = await axios.post(`${BASE_URL}/investment/`, submissionData, { headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' } }); setInvestment([...investment, response.data]); setShowModal(true); setFormData({ refId: '', fullName: '', balance: '', username: '', accountName: '', accountNumber: '', method: '', note: '' }); } catch (error) { console.error("Error:", error.response?.data); setError( error.response?.data?.detail || 'Yatırım oluşturulurken bir hata oluştu. Lütfen tüm alanları kontrol ediniz.' ); } finally { setIsLoading(false); } }; const handleCloseModal = () => { setShowModal(false); window.location.reload(); }; if (!show) return null; return (

{title}

{error && (
{error}
)}