import { useEffect, useState } from 'react' import type { CreateExperimentPhaseRequest, MachineType } from '../lib/supabase' import { machineTypeManagement } from '../lib/supabase' interface PhaseFormProps { onSubmit: (data: CreateExperimentPhaseRequest) => void onCancel: () => void loading?: boolean } export function PhaseForm({ onSubmit, onCancel, loading = false }: PhaseFormProps) { const [formData, setFormData] = useState({ name: '', description: '', has_soaking: false, has_airdrying: false, has_cracking: false, has_shelling: false }) const [errors, setErrors] = useState>({}) const [machineTypes, setMachineTypes] = useState([]) const [machinesLoading, setMachinesLoading] = useState(false) const [selectedCrackingMachineId, setSelectedCrackingMachineId] = useState(undefined) useEffect(() => { // Preload machine types for cracking selection const loadMachines = async () => { try { setMachinesLoading(true) const types = await machineTypeManagement.getAllMachineTypes() setMachineTypes(types) } catch (e) { // Non-fatal: we will show no options if it fails console.warn('Failed to load machine types', e) } finally { setMachinesLoading(false) } } loadMachines() }, []) const validateForm = (): boolean => { const newErrors: Record = {} // Name is required if (!formData.name.trim()) { newErrors.name = 'Phase name is required' } // At least one phase must be selected if (!formData.has_soaking && !formData.has_airdrying && !formData.has_cracking && !formData.has_shelling) { newErrors.phases = 'At least one phase must be selected' } // If cracking is enabled, require a machine selection if (formData.has_cracking && !selectedCrackingMachineId) { newErrors.cracking_machine_type_id = 'Select a cracking machine' } setErrors(newErrors) return Object.keys(newErrors).length === 0 } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (validateForm()) { // Include cracking machine selection in payload when cracking is enabled const payload: CreateExperimentPhaseRequest = formData.has_cracking ? { ...formData, cracking_machine_type_id: selectedCrackingMachineId } : formData onSubmit(payload) } } const handleInputChange = (field: keyof CreateExperimentPhaseRequest, value: string | boolean | undefined) => { setFormData(prev => ({ ...prev, [field]: value })) // Clear error when user starts typing if (errors[field]) { setErrors(prev => ({ ...prev, [field]: '' })) } } const phaseOptions = [ { key: 'has_soaking' as const, label: 'Soaking', description: 'Initial soaking process for pecans', icon: '🌰' }, { key: 'has_airdrying' as const, label: 'Air-Drying', description: 'Post-soak air-drying process', icon: '💨' }, { key: 'has_cracking' as const, label: 'Cracking', description: 'Pecan shell cracking process', icon: '🔨' }, { key: 'has_shelling' as const, label: 'Shelling', description: 'Final shelling and yield measurement', icon: '📊' } ] return (
{/* Phase Name */}
handleInputChange('name', e.target.value)} className={`w-full px-3 py-2 border rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 ${errors.name ? 'border-red-300' : 'border-gray-300' }`} placeholder="Enter phase name (e.g., 'Full Process Experiment')" disabled={loading} /> {errors.name && (

{errors.name}

)}
{/* Description */}