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 { purchaserApi } from '../../api'; 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(''); try { await purchaserApi.updatePlan(plan.id, { product_name: editForm.product_name, color_code: editForm.color, target_quantity: editForm.target_quantity, notes: editForm.remark || undefined, }); setIsSubmitting(false); onPlanUpdated?.(); onClose(); } catch (err: any) { setIsSubmitting(false); setError('更新失败: ' + (err.message || '未知错误')); } }; const handleDelete = async () => { if (!canModify) { setError('计划已被工厂确认,无法删除'); return; } setIsSubmitting(true); setError(''); try { await purchaserApi.deletePlan(plan.id); setIsSubmitting(false); onPlanUpdated?.(); onClose(); } catch (err: any) { setIsSubmitting(false); setError('删除失败: ' + (err.message || '未知错误')); } }; if (!isOpen) return null; return (
{/* 头部 */}
{activeTab === 'edit' ? ( ) : ( )}

{activeTab === 'edit' ? '编辑计划' : '删除计划'}

{/* Tab 切换 */} {canModify && (
)} {/* 内容区域 */}
{!canModify ? (

计划已被工厂确认

已确认的计划无法修改或删除

) : activeTab === 'edit' ? (
setEditForm({ ...editForm, product_name: e.target.value })} className="w-full px-3 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all" required />
setEditForm({ ...editForm, color: e.target.value })} className="w-full px-3 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all" required />
setEditForm({ ...editForm, target_quantity: parseInt(e.target.value) || 0 })} className="w-full px-3 py-2 border border-gray-200 rounded-lg focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition-all" min="1" required />