import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../../contexts/AuthContext'; import { supabase } from '../../supabase/client'; import { motion } from 'framer-motion'; import { FileText, Plus, Warehouse, Factory, ArrowLeft, Users, TrendingUp, Package, Clock, Building2, CheckCircle2, DollarSign, Droplets, LogOut, HelpCircle } from 'lucide-react'; import { useResponsive } from '../../hooks/useResponsive'; import { OnboardingGuide, shouldShowOnboarding } from '../../components/OnboardingGuide'; import { NotificationCenter } from '../../components/NotificationCenter'; import { HelpCenter, HelpButton } from '../../components/HelpCenter'; import { VersionUpdate, useVersionCheck, AboutModal } from '../../components/VersionUpdate'; import { FeedbackModal } from '../../components/FeedbackModal'; import { UserManual } from '../../components/UserManual'; import type { ProductionPlan } from '../../types'; const statusMap: Record = { pending: { label: '待确定', color: 'text-amber-600', bgColor: 'bg-amber-50', icon: Clock }, producing: { label: '生产中', color: 'text-blue-600', bgColor: 'bg-blue-50', icon: TrendingUp }, completed: { label: '已完成', color: 'text-emerald-600', bgColor: 'bg-emerald-50', icon: Package } }; const getQuickActions = (isMaster: boolean) => { const actions = [ { id: 'plans', icon: FileText, label: '计划总览', path: '/purchaser/plans', gradient: 'from-blue-500 to-cyan-500', description: '查看所有计划' }, { id: 'new', icon: Plus, label: '新建纺织计划', path: '/purchaser/plans/new', gradient: 'from-emerald-500 to-green-500', description: '创建新纺织计划' }, { id: 'washing', icon: Droplets, label: '新建水洗', path: '/purchaser/washing-plans/new', gradient: 'from-cyan-500 to-blue-500', description: '创建水洗计划' }, { id: 'warehouse', icon: Warehouse, label: '原坯布仓库', path: '/purchaser/warehouse', gradient: 'from-amber-500 to-orange-500', description: '管理原坯布库存' }, { id: 'finished', icon: Package, label: '水洗仓库', path: '/purchaser/finished-warehouse', gradient: 'from-emerald-500 to-teal-500', description: '管理水洗厂内坯布库存' }, { id: 'factories', icon: Factory, label: '工厂管理', path: '/purchaser/factories', gradient: 'from-purple-500 to-violet-500', description: '管理合作工厂' }, { id: 'accounts-payable', icon: DollarSign, label: '应付账款', path: '/purchaser/accounts-payable', gradient: 'from-rose-500 to-pink-500', description: '查看应付账款' } ]; if (isMaster) { actions.push({ id: 'members', icon: Users, label: '账号管理', path: '/members', gradient: 'from-gray-500 to-slate-500', description: '管理团队成员' }); } return actions; }; const containerVariants = { hidden: { opacity: 0 }, visible: { opacity: 1, transition: { staggerChildren: 0.1 } } }; const itemVariants = { hidden: { opacity: 0, y: 20 }, visible: { opacity: 1, y: 0, transition: { duration: 0.4 } } }; export function PurchaserDashboard() { const navigate = useNavigate(); const { auth, logout } = useAuth(); const { isMobile } = useResponsive(); const [plans, setPlans] = useState([]); const [loading, setLoading] = useState(true); // 帮助中心状态 const [showHelp, setShowHelp] = useState(false); const [showOnboarding, setShowOnboarding] = useState(false); const [showUserManual, setShowUserManual] = useState(false); const [showFeedback, setShowFeedback] = useState(false); const [showAbout, setShowAbout] = useState(false); // 版本更新检查 const { showUpdate, setShowUpdate } = useVersionCheck(); // 检查是否显示新手引导 useEffect(() => { if (shouldShowOnboarding('purchaser')) { setShowOnboarding(true); } }, []); useEffect(() => { if (!auth.company) return; fetchPlans(); }, [auth.company]); const fetchPlans = async () => { const { data: planData } = await supabase .from('production_plans') .select('*') .eq('purchaser_id', auth.company!.id) .order('created_at', { ascending: false }); // 获取流程步骤,检测 rejected 状态 const planIds = (planData || []).map(p => p.id); const { data: stepsData } = await supabase .from('plan_process_steps') .select('plan_id, status') .in('plan_id', planIds); // 过滤掉有 rejected 步骤的计划 const rejectedPlanIds = new Set(); (stepsData || []).forEach(step => { if (step.status === 'rejected') { rejectedPlanIds.add(step.plan_id); } }); const filteredPlans = (planData || []).filter(p => !rejectedPlanIds.has(p.id)); setPlans(filteredPlans); setLoading(false); }; const stats = { all: plans.length, producing: plans.filter(p => p.status === 'producing').length, completed: plans.filter(p => p.status === 'completed').length }; const recentPlans = plans.slice(0, 5); return (
{/* 新用户引导动画 - 已集成到下方弹窗 */} {/* Header - 移动端优化 */}

{auth.company?.name || '采购商'}工作台 限时免费

{(auth.user as any)?.display_name || auth.user?.username || '用户'},欢迎回来

setShowHelp(true)} />
{loading ? (
) : ( {/* 统计卡片 - 移动端优化 */}
navigate('/purchaser/plans')} className="bg-white rounded-xl sm:rounded-2xl p-3 sm:p-5 shadow-sm border border-gray-100 cursor-pointer hover:border-blue-300 hover:shadow-md transition-all" >

所有计划

{stats.all}

累计创建

navigate('/purchaser/plans?filter=producing')} className="bg-white rounded-xl sm:rounded-2xl p-3 sm:p-5 shadow-sm border border-gray-100 cursor-pointer hover:border-emerald-300 hover:shadow-md transition-all" >

进行中

{stats.producing}

正在生产

navigate('/purchaser/plans?filter=completed')} className="bg-white rounded-xl sm:rounded-2xl p-3 sm:p-5 shadow-sm border border-gray-100 cursor-pointer hover:border-amber-300 hover:shadow-md transition-all" >

已完成

{stats.completed}

已入库

{/* 快捷操作 - 移动端优化 */}

快捷操作

{getQuickActions(auth.user?.is_master || false).map((action, index) => ( navigate(action.path)} className="group flex flex-col items-center p-2 sm:p-3 rounded-lg sm:rounded-xl border border-gray-100 hover:border-transparent hover:shadow-md transition-all bg-gray-50/50 hover:bg-white" >
{action.label}
))}
{/* 最近计划 - 移动端优化 */}

最近计划

{recentPlans.length === 0 ? (

暂无计划

) : (
{recentPlans.map((plan, i) => { const progress = plan.target_quantity > 0 ? Math.round((plan.completed_quantity / plan.target_quantity) * 100) : 0; const status = statusMap[plan.status] || statusMap.pending; const StatusIcon = status.icon; return ( navigate(`/purchaser/plans?id=${plan.id}`)} className="group flex items-center gap-2 sm:gap-4 p-3 sm:p-4 rounded-xl border border-gray-100 hover:border-blue-200 hover:bg-blue-50/30 cursor-pointer transition-all" >

{plan.product_name}-{plan.color}

{status.label}

{plan.fabric_code}-{plan.color_code}

{plan.target_quantity}米

计划产量

进度 {progress}%
); })}
)}
)}
{/* 帮助中心弹窗 */} setShowHelp(false)} /> {/* 新手引导 */} setShowOnboarding(false)} /> {/* 用户手册 */} setShowUserManual(false)} /> {/* 反馈弹窗 */} setShowFeedback(false)} /> {/* 关于窗口 */} setShowAbout(false)} /> {/* 版本更新弹窗 */} setShowUpdate(false)} />
); }