import React, { useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { X, AlertTriangle, Edit2, Trash2 } from 'lucide-react'; import { useResponsive } from '../../hooks/useResponsive'; import { supabase } from '../../supabase/client'; import type { PlanWithSteps } from '../../types'; interface PlanEditModalProps { plan: PlanWithSteps; isOpen: boolean; onClose: () => void; onPlanUpdated?: () => void; } export function PlanEditModal({ plan, isOpen, onClose, onPlanUpdated }: PlanEditModalProps) { const { isMobile } = useResponsive(); const [activeTab, setActiveTab] = useState<'edit' | 'delete'>('edit'); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(''); const [editForm, setEditForm] = useState({ product_name: plan.product_name, color: plan.color, target_quantity: plan.target_quantity, remark: plan.remark || '' }); // 判断是否可以编辑/删除:只有当"确认计划"步骤状态为 pending 时才允许 const confirmStep = plan.processSteps.find(s => s.step_type === 'confirm'); const canModify = confirmStep?.status === 'pending'; const handleUpdate = async (e: React.FormEvent) => { e.preventDefault(); if (!canModify) { setError('计划已被工厂确认,无法修改'); return; } setIsSubmitting(true); setError(''); const { error: updateError } = await supabase .from('production_plans') .update({ product_name: editForm.product_name, color: editForm.color, target_quantity: editForm.target_quantity, remark: editForm.remark || null, updated_at: new Date().toISOString() }) .eq('id', plan.id); setIsSubmitting(false); if (updateError) { setError('更新失败: ' + updateError.message); } else { onPlanUpdated?.(); onClose(); } }; const handleDelete = async () => { if (!canModify) { setError('计划已被工厂确认,无法删除'); return; } setIsSubmitting(true); setError(''); // 先删除关联的 process_steps await supabase.from('plan_process_steps').delete().eq('plan_id', plan.id); // 再删除 plan_factories 关联 await supabase.from('plan_factories').delete().eq('plan_id', plan.id); // 最后删除计划 const { error: deleteError } = await supabase .from('production_plans') .delete() .eq('id', plan.id); setIsSubmitting(false); if (deleteError) { setError('删除失败: ' + deleteError.message); } else { onPlanUpdated?.(); onClose(); } }; if (!isOpen) return null; return (
计划已被工厂确认
已确认的计划无法修改或删除
确认删除此计划?
计划编号: {plan.plan_code}
产品: {plan.product_name}-{plan.color}
此操作不可恢复,请谨慎操作