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; if (name === 'balance') { const numericValue = value.replace(/[^\d.]/g, ''); processedValue = numericValue ? parseFloat(numericValue) : ''; } setFormData(prev => ({ ...prev, [name]: processedValue })); }; const validateForm = () => { if (!formData.refId || formData.refId.trim() === '') return "Referans seçimi zorunludur"; if (!formData.fullName || formData.fullName.trim() === '') return "Ad Soyad alanı zorunludur"; if (!formData.balance || isNaN(formData.balance) || formData.balance <= 0) return "Geçerli bir miktar giriniz"; if (!formData.username || formData.username.trim() === '') return "Kullanıcı adı zorunludur"; if (!formData.accountName || formData.accountName.trim() === '') return "Hesap adı zorunludur"; if (!formData.accountNumber || formData.accountNumber.trim() === '') return "Hesap numarası zorunludur"; if (!formData.method || formData.method.trim() === '') return "Ödeme yöntemi zorunludur"; return null; }; const handleSubmit = async (e) => { e.preventDefault(); const validationError = validateForm(); if (validationError) { setError(validationError); return; } setIsLoading(true); setError(''); const token = localStorage.getItem('token'); // Format the data to match the backend expectations const submissionData = { refId: formData.refId.trim(), fullName: formData.fullName.trim(), balance: parseFloat(formData.balance), username: formData.username.trim(), accountName: formData.accountName.trim(), accountNumber: formData.accountNumber.trim(), method: formData.method.trim(), note: formData.note.trim() || '' }; console.log('Submitting data:', submissionData); try { const response = await axios.post( `${BASE_URL}/investment/`, submissionData, { headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json', 'Accept': 'application/json' } } ); console.log('Success response:', response.data); setInvestment([...investment, response.data]); setShowModal(true); setFormData({ refId: '', fullName: '', balance: '', username: '', accountName: '', accountNumber: '', method: '', note: '' }); } catch (error) { console.error('Full error object:', error); console.error('Error response data:', error.response?.data); console.error('Error status:', error.response?.status); console.error('Error headers:', error.response?.headers); let errorMessage = 'Yatırım oluşturulurken bir hata oluştu.'; if (error.response?.data?.detail) { if (Array.isArray(error.response.data.detail)) { const fieldErrors = error.response.data.detail.map(err => `${err.loc[1]}: ${err.msg}` ).join(', '); errorMessage += ` Hatalı alanlar: ${fieldErrors}`; } else { errorMessage += ` Detay: ${error.response.data.detail}`; } } setError(errorMessage); } finally { setIsLoading(false); } }; const handleCloseModal = () => { setShowModal(false); window.location.reload(); }; if (!show) return null; return (