import React, { useState, useEffect } from 'react'; import { Plant } from '../types'; import { X } from 'lucide-react'; import { apiService } from '../services/api'; import ImageUpload from './ImageUpload'; interface PlantFormProps { plant?: Plant | null; onSave: (plant: Omit) => void; onCancel: () => void; } const PlantForm: React.FC = ({ plant, onSave, onCancel }) => { const [formData, setFormData] = useState({ type: '', variety: '', purchaseLocation: '', seedlingAge: '', seedlingHeight: '', plantingDate: '', healthStatus: 'good' as Plant['healthStatus'], currentHeight: '', photoUrl: '', notes: '' }); const [uploading, setUploading] = useState(false); const [selectedFile, setSelectedFile] = useState(null); useEffect(() => { if (plant) { setFormData({ type: plant.type, variety: plant.variety, purchaseLocation: plant.purchaseLocation, seedlingAge: plant.seedlingAge.toString(), seedlingHeight: plant.seedlingHeight.toString(), plantingDate: plant.plantingDate, healthStatus: plant.healthStatus, currentHeight: plant.currentHeight?.toString() || '', photoUrl: plant.photoUrl || '', notes: plant.notes }); } }, [plant]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); let finalPhotoUrl = formData.photoUrl; // Upload image if a file is selected if (selectedFile) { try { setUploading(true); const result = await apiService.uploadPhoto(selectedFile); finalPhotoUrl = result.photoUrl; } catch (error) { console.error('Error uploading photo:', error); alert('Failed to upload photo. Please try again.'); setUploading(false); return; } finally { setUploading(false); } } onSave({ ...formData, seedlingAge: parseInt(formData.seedlingAge), seedlingHeight: parseFloat(formData.seedlingHeight), currentHeight: formData.currentHeight ? parseFloat(formData.currentHeight) : undefined, photoUrl: finalPhotoUrl || undefined }); }; const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const handleFileSelect = (file: File) => { setSelectedFile(file); }; const handleImageChange = (imageUrl: string) => { setFormData(prev => ({ ...prev, photoUrl: imageUrl })); }; return (

{plant ? 'Редактирование данных' : 'Новое растение'}