import React, { useState, useEffect } from 'react'; import { useForm, Controller } from 'react-hook-form'; import { Modal, Button, Input, Textarea, Select, Badge, Card } from '../../ui'; import { QualityCheckType, ProcessStage, type QualityCheckTemplate, type QualityCheckTemplateUpdate } from '../../../api/types/qualityTemplates'; interface EditQualityTemplateModalProps { isOpen: boolean; onClose: () => void; template: QualityCheckTemplate; onUpdateTemplate: (templateData: QualityCheckTemplateUpdate) => Promise; isLoading?: boolean; } const QUALITY_CHECK_TYPE_OPTIONS = [ { value: QualityCheckType.VISUAL, label: 'Visual - Inspección visual' }, { value: QualityCheckType.MEASUREMENT, label: 'Medición - Medidas precisas' }, { value: QualityCheckType.TEMPERATURE, label: 'Temperatura - Control térmico' }, { value: QualityCheckType.WEIGHT, label: 'Peso - Control de peso' }, { value: QualityCheckType.BOOLEAN, label: 'Sí/No - Verificación binaria' }, { value: QualityCheckType.TIMING, label: 'Tiempo - Control temporal' } ]; const PROCESS_STAGE_OPTIONS = [ { value: ProcessStage.MIXING, label: 'Mezclado' }, { value: ProcessStage.PROOFING, label: 'Fermentación' }, { value: ProcessStage.SHAPING, label: 'Formado' }, { value: ProcessStage.BAKING, label: 'Horneado' }, { value: ProcessStage.COOLING, label: 'Enfriado' }, { value: ProcessStage.PACKAGING, label: 'Empaquetado' }, { value: ProcessStage.FINISHING, label: 'Acabado' } ]; const CATEGORY_OPTIONS = [ { value: 'appearance', label: 'Apariencia' }, { value: 'structure', label: 'Estructura' }, { value: 'texture', label: 'Textura' }, { value: 'flavor', label: 'Sabor' }, { value: 'safety', label: 'Seguridad' }, { value: 'packaging', label: 'Empaque' }, { value: 'temperature', label: 'Temperatura' }, { value: 'weight', label: 'Peso' }, { value: 'dimensions', label: 'Dimensiones' } ]; export const EditQualityTemplateModal: React.FC = ({ isOpen, onClose, template, onUpdateTemplate, isLoading = false }) => { const [selectedStages, setSelectedStages] = useState( template.applicable_stages || [] ); const { register, control, handleSubmit, watch, reset, formState: { errors, isDirty } } = useForm({ defaultValues: { name: template.name, template_code: template.template_code || '', check_type: template.check_type, category: template.category || '', description: template.description || '', instructions: template.instructions || '', is_active: template.is_active, is_required: template.is_required, is_critical: template.is_critical, weight: template.weight, min_value: template.min_value, max_value: template.max_value, target_value: template.target_value, unit: template.unit || '', tolerance_percentage: template.tolerance_percentage } }); const checkType = watch('check_type'); const showMeasurementFields = [ QualityCheckType.MEASUREMENT, QualityCheckType.TEMPERATURE, QualityCheckType.WEIGHT ].includes(checkType || template.check_type); // Update form when template changes useEffect(() => { if (template) { reset({ name: template.name, template_code: template.template_code || '', check_type: template.check_type, category: template.category || '', description: template.description || '', instructions: template.instructions || '', is_active: template.is_active, is_required: template.is_required, is_critical: template.is_critical, weight: template.weight, min_value: template.min_value, max_value: template.max_value, target_value: template.target_value, unit: template.unit || '', tolerance_percentage: template.tolerance_percentage }); setSelectedStages(template.applicable_stages || []); } }, [template, reset]); const handleStageToggle = (stage: ProcessStage) => { const newStages = selectedStages.includes(stage) ? selectedStages.filter(s => s !== stage) : [...selectedStages, stage]; setSelectedStages(newStages); }; const onSubmit = async (data: QualityCheckTemplateUpdate) => { try { // Only include changed fields const updates: QualityCheckTemplateUpdate = {}; Object.entries(data).forEach(([key, value]) => { const originalValue = (template as any)[key]; if (value !== originalValue) { (updates as any)[key] = value; } }); // Handle applicable stages const stagesChanged = JSON.stringify(selectedStages.sort()) !== JSON.stringify((template.applicable_stages || []).sort()); if (stagesChanged) { updates.applicable_stages = selectedStages.length > 0 ? selectedStages : undefined; } // Only submit if there are actual changes if (Object.keys(updates).length > 0) { await onUpdateTemplate(updates); } else { onClose(); } } catch (error) { console.error('Error updating template:', error); } }; const handleClose = () => { reset(); setSelectedStages(template.applicable_stages || []); onClose(); }; return (
{/* Basic Information */}

Información Básica

( )} />
( )} />