"use client"; import React, { useState, useEffect, useCallback } from "react"; import { useSession } from "next-auth/react"; import { SessionWithTokens } from "@/config/authConfig"; import { Box, Tabs, Tab, Stack, Typography, Autocomplete, TextField } from "@mui/material"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import QcStockInModal from "@/components/Qc/QcStockInModal"; import ProductionProcessList, { createDefaultProductionProcessListPersistedState, } from "@/components/ProductionProcess/ProductionProcessList"; import ProductionProcessDetail from "@/components/ProductionProcess/ProductionProcessDetail"; import ProductionProcessJobOrderDetail from "@/components/ProductionProcess/ProductionProcessJobOrderDetail"; import JobPickExecutionsecondscan from "@/components/Jodetail/JobPickExecutionsecondscan"; import JobProcessStatus from "@/components/ProductionProcess/JobProcessStatus"; import OperatorKpiDashboard from "@/components/ProductionProcess/OperatorKpiDashboard"; import EquipmentStatusDashboard from "@/components/ProductionProcess/EquipmentStatusDashboard"; import type { PrinterCombo } from "@/app/api/settings/printer"; import { useTranslation } from "react-i18next"; interface ProductionProcessPageProps { printerCombo: PrinterCombo[]; } const STORAGE_KEY = 'productionProcess_selectedMatchingStock'; const ProductionProcessPage: React.FC = ({ printerCombo }) => { const { t } = useTranslation(["common"]); const [selectedProcessId, setSelectedProcessId] = useState(null); const [selectedMatchingStock, setSelectedMatchingStock] = useState<{ jobOrderId: number; productProcessId: number; pickOrderId: number; } | null>(null); const [tabIndex, setTabIndex] = useState(0); /** 列表搜尋/分頁:保留在切換工單詳情時,返回後仍為同一條件 */ const [productionListState, setProductionListState] = useState( createDefaultProductionProcessListPersistedState, ); const [waitingPutawayListState, setWaitingPutawayListState] = useState( createDefaultProductionProcessListPersistedState, ); const [putawayedListState, setPutawayedListState] = useState( createDefaultProductionProcessListPersistedState, ); const { data: session } = useSession() as { data: SessionWithTokens | null }; const sessionToken = session as SessionWithTokens | null; const searchParams = useSearchParams(); const pathname = usePathname(); const router = useRouter(); const [linkQcOpen, setLinkQcOpen] = useState(false); const [linkQcSilId, setLinkQcSilId] = useState(null); // Add printer selection state const [selectedPrinter, setSelectedPrinter] = useState( printerCombo && printerCombo.length > 0 ? printerCombo[0] : null ); // 从 sessionStorage 恢复状态(仅在客户端) useEffect(() => { if (typeof window !== 'undefined') { try { const saved = sessionStorage.getItem(STORAGE_KEY); if (saved) { const parsed = JSON.parse(saved); // 验证数据有效性 if (parsed && typeof parsed.jobOrderId === 'number' && typeof parsed.productProcessId === 'number') { setSelectedMatchingStock(parsed); console.log(" Restored selectedMatchingStock from sessionStorage:", parsed); } } } catch (error) { console.error("Error restoring selectedMatchingStock:", error); sessionStorage.removeItem(STORAGE_KEY); } } }, []); // 保存状态到 sessionStorage useEffect(() => { if (typeof window !== 'undefined') { if (selectedMatchingStock) { sessionStorage.setItem(STORAGE_KEY, JSON.stringify(selectedMatchingStock)); console.log(" Saved selectedMatchingStock to sessionStorage:", selectedMatchingStock); } else { sessionStorage.removeItem(STORAGE_KEY); } } }, [selectedMatchingStock]); // 处理返回列表时清除存储 const handleBackFromSecondScan = useCallback(() => { setSelectedMatchingStock(null); if (typeof window !== 'undefined') { sessionStorage.removeItem(STORAGE_KEY); } }, []); const handleTabChange = useCallback((event: React.SyntheticEvent, newValue: number) => { setTabIndex(newValue); }, []); const openStockInLineIdQ = searchParams.get("openStockInLineId"); /** Deep link from nav alert: /productionProcess?openStockInLineId=… → 「完成QC工單」tab + FG QC modal */ useEffect(() => { if (!openStockInLineIdQ) { setLinkQcOpen(false); setLinkQcSilId(null); return; } const id = parseInt(openStockInLineIdQ, 10); if (!Number.isFinite(id) || id <= 0) return; setSelectedProcessId(null); setSelectedMatchingStock(null); setTabIndex(1); setLinkQcSilId(id); setLinkQcOpen(true); }, [openStockInLineIdQ]); const closeLinkQc = useCallback(() => { setLinkQcOpen(false); setLinkQcSilId(null); const p = new URLSearchParams(searchParams.toString()); p.delete("openStockInLineId"); const q = p.toString(); router.replace(q ? `${pathname}?${q}` : pathname, { scroll: false }); }, [pathname, router, searchParams]); if (selectedMatchingStock) { return ( ); } if (selectedProcessId !== null) { return ( setSelectedProcessId(null)} /> ); } return ( <> {/* Header section with printer selection */} {tabIndex === 1 && ( {t("Select Printer")}: option.name || option.label || option.code || `Printer ${option.id}` } value={selectedPrinter || undefined} onChange={(_, newValue) => setSelectedPrinter(newValue)} sx={{ minWidth: 200 }} size="small" renderInput={(params) => ( )} /> )} {tabIndex === 0 && ( { const id = jobOrderId ?? null; if (id !== null) { setSelectedProcessId(id); } }} onSelectMatchingStock={(jobOrderId, productProcessId,pickOrderId) => { setSelectedMatchingStock({ jobOrderId: jobOrderId || 0, productProcessId: productProcessId || 0 , pickOrderId: pickOrderId || 0 }); }} /> )} {tabIndex === 1 && ( { const id = jobOrderId ?? null; if (id !== null) { setSelectedProcessId(id); } }} onSelectMatchingStock={(jobOrderId, productProcessId, pickOrderId) => { setSelectedMatchingStock({ jobOrderId: jobOrderId || 0, productProcessId: productProcessId || 0, pickOrderId: pickOrderId || 0, }); }} /> )} {tabIndex === 2 && ( { const id = jobOrderId ?? null; if (id !== null) { setSelectedProcessId(id); } }} onSelectMatchingStock={(jobOrderId, productProcessId, pickOrderId) => { setSelectedMatchingStock({ jobOrderId: jobOrderId || 0, productProcessId: productProcessId || 0, pickOrderId: pickOrderId || 0, }); }} /> )} {tabIndex === 3 && ( )} {tabIndex === 4 && ( )} {tabIndex === 5 && ( )} 0)} onClose={closeLinkQc} inputDetail={linkQcSilId != null && linkQcSilId > 0 ? { id: linkQcSilId } : undefined} printerCombo={printerCombo} warehouse={[]} printSource="productionProcess" uiMode="default" /> ); }; export default ProductionProcessPage;