import React, { useState, useEffect } from 'react'; import { X, FileText, Sprout, Beaker } from 'lucide-react'; import {Task, Plant, Fertilizer, Chemical, PlantTitles, TaskDraft} from '../types'; import { apiService } from '../services/api'; interface TaskFormProps { isOpen: boolean; onClose: () => void; onSubmit: (task: TaskDraft) => void; task?: Task; } const taskTypes = [ { value: 'general', label: 'Общая', icon: FileText, color: 'bg-gray-100 text-gray-800' }, { value: 'fertilizer', label: 'Внесение удобрений', icon: Sprout, color: 'bg-green-100 text-green-800' }, { value: 'chemical', label: 'Применение химикатов', icon: Beaker, color: 'bg-red-100 text-red-800' }, { value: 'watering', label: 'Полив', icon: FileText, color: 'bg-blue-100 text-blue-800' }, { value: 'pruning', label: 'Обрезка', icon: FileText, color: 'bg-purple-100 text-purple-800' }, { value: 'transplanting', label: 'Пересадка', icon: FileText, color: 'bg-orange-100 text-orange-800' }, { value: 'harvesting', label: 'Сбор урожая', icon: FileText, color: 'bg-yellow-100 text-yellow-800' }, { value: 'other', label: 'Другое', icon: FileText, color: 'bg-gray-100 text-gray-800' } ]; export default function TaskForm({ isOpen, onClose, onSubmit, task }: TaskFormProps) { const [formData, setFormData] = useState({ plantId: '', type: 'general', title: '', description: '', deadline: '', completed: false, fertilizerId: '', chemicalId: '' }); const [plants, setPlants] = useState([]); const [fertilizers, setFertilizers] = useState([]); const [chemicals, setChemicals] = useState([]); const [selectedFertilizer, setSelectedFertilizer] = useState(null); const [selectedChemical, setSelectedChemical] = useState(null); useEffect(() => { if (isOpen) { loadData(); } }, [isOpen]); useEffect(() => { if (task) { setFormData({ plantId: task.plantId?.toString() || '', type: task.type || 'general', title: task.title, description: task.description || '', deadline: task.deadline.split('T')[0], completed: task.completed, fertilizerId: task.fertilizerId?.toString() || '', chemicalId: task.chemicalId?.toString() || '' }); } else { setFormData({ plantId: '', type: 'general', title: '', description: '', deadline: '', completed: false, fertilizerId: '', chemicalId: '' }); } }, [task]); const loadData = async () => { try { const [plantsData, fertilizersData, chemicalsData] = await Promise.all([ apiService.getPlants(), apiService.getFertilizers(), apiService.getChemicals() ]); setPlants(plantsData); setFertilizers(fertilizersData); setChemicals(chemicalsData); } catch (error) { console.error('Error loading data:', error); } }; useEffect(() => { if (formData.fertilizerId) { const fertilizer = fertilizers.find(f => f.id.toString() === formData.fertilizerId); setSelectedFertilizer(fertilizer || null); } else { setSelectedFertilizer(null); } }, [formData.fertilizerId, fertilizers]); useEffect(() => { if (formData.chemicalId) { const chemical = chemicals.find(c => c.id.toString() === formData.chemicalId); setSelectedChemical(chemical || null); } else { setSelectedChemical(null); } }, [formData.chemicalId, chemicals]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const taskData = { ...formData, plantId: formData.plantId ? parseInt(formData.plantId) : undefined, fertilizerId: formData.fertilizerId ? parseInt(formData.fertilizerId) : undefined, chemicalId: formData.chemicalId ? parseInt(formData.chemicalId) : undefined }; onSubmit(taskData); onClose(); }; const handleTaskTypeChange = (taskType: string) => { setFormData(prev => ({ ...prev, type: taskType, fertilizerId: taskType !== 'fertilizer' ? '' : prev.fertilizerId, chemicalId: taskType !== 'chemical' ? '' : prev.chemicalId })); }; if (!isOpen) return null; const selectedTaskType = taskTypes.find(type => type.value === formData.type); return (

{task ? 'Изменение работы' : 'Создание новой работы'}

setFormData(prev => ({ ...prev, title: e.target.value }))} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500" placeholder="Enter task title" />
setFormData(prev => ({ ...prev, deadline: e.target.value }))} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500" />
setFormData(prev => ({ ...prev, completed: e.target.checked }))} className="h-4 w-4 text-green-600 focus:ring-green-500 border-gray-300 rounded" />
{formData.type === 'fertilizer' && (
{selectedFertilizer && (

Fertilizer Information

NPK: {selectedFertilizer.npkRatio}

Application Rate: {selectedFertilizer.applicationRate}

Frequency: {selectedFertilizer.frequency}

{selectedFertilizer.notes && (

Заметки: {selectedFertilizer.notes}

)}
)}
)} {formData.type === 'chemical' && (
{selectedChemical && (

Chemical Information

Active Ingredients: {selectedChemical.activeIngredient}

Target Pests: {selectedChemical.targetPests}

Application Rate: {selectedChemical.applicationMethod}

{selectedChemical.safetyPeriod && (

Safety Period: {selectedChemical.safetyPeriod}

)} {selectedChemical.notes && (

Заметки: {selectedChemical.notes}

)}
)}
)}