"use client"; import { Box, Button, Grid, Stack, Typography, Select, MenuItem, FormControl, InputLabel } from "@mui/material"; import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useSession } from "next-auth/react"; import { SessionWithTokens } from "@/config/authConfig"; import { fetchStoreLaneSummary, assignByLane, type StoreLaneSummary } from "@/app/api/pickOrder/actions"; import Swal from "sweetalert2"; import dayjs from "dayjs"; interface Props { onPickOrderAssigned?: () => void; } const FinishedGoodFloorLanePanel: React.FC = ({ onPickOrderAssigned }) => { const { t } = useTranslation("pickOrder"); const { data: session } = useSession() as { data: SessionWithTokens | null }; const currentUserId = session?.id ? parseInt(session.id) : undefined; const [summary2F, setSummary2F] = useState(null); const [summary4F, setSummary4F] = useState(null); const [isLoadingSummary, setIsLoadingSummary] = useState(false); const [isAssigning, setIsAssigning] = useState(false); const [selectedDate, setSelectedDate] = useState("today"); const loadData = async (dateValue: string) => { setIsLoadingSummary(true); try { let dateOffset = 0; if (dateValue === "tomorrow") dateOffset = 1; else if (dateValue === "dayAfterTomorrow") dateOffset = 2; const requiredDate = dayjs().add(dateOffset, "day").format("YYYY-MM-DD"); console.log("🔄 requiredDate:", requiredDate); const [s2, s4] = await Promise.all([ fetchStoreLaneSummary("2/F", requiredDate), fetchStoreLaneSummary("4/F", requiredDate), ]); console.log("🔄 s2:", s2); console.log("🔄 s4:", s4); setSummary2F(s2); setSummary4F(s4); } catch (e) { console.error("load summaries failed:", e); } finally { setIsLoadingSummary(false); } }; // 初始化 useEffect(() => { loadData("today"); }, []); const handleAssignByLane = useCallback(async ( storeId: string, truckDepartureTime: string, truckLanceCode: string ) => { if (!currentUserId) { console.error("Missing user id in session"); return; } setIsAssigning(true); try { const res = await assignByLane(currentUserId, storeId, truckLanceCode, truckDepartureTime); if (res.code === "SUCCESS") { console.log("✅ Successfully assigned pick order from lane", truckLanceCode); window.dispatchEvent(new CustomEvent('pickOrderAssigned')); loadData(selectedDate); // 刷新按钮状态 onPickOrderAssigned?.(); } else if (res.code === "USER_BUSY") { Swal.fire({ icon: "warning", title: t("Warning"), text: t("You already have a pick order in progess. Please complete it first before taking next pick order."), confirmButtonText: t("Confirm"), confirmButtonColor: "#8dba00" }); window.dispatchEvent(new CustomEvent('pickOrderAssigned')); } else if (res.code === "NO_ORDERS") { Swal.fire({ icon: "info", title: t("Info"), text: t("No available pick order(s) for this lane."), confirmButtonText: t("Confirm"), confirmButtonColor: "#8dba00" }); } else { console.log("ℹ️ Assignment result:", res.message); } } catch (error) { console.error("❌ Error assigning by lane:", error); Swal.fire({ icon: "error", title: t("Error"), text: t("Error occurred during assignment."), confirmButtonText: t("Confirm"), confirmButtonColor: "#8dba00" }); } finally { setIsAssigning(false); } }, [currentUserId, t, selectedDate, onPickOrderAssigned]); const getDateLabel = (offset: number) => { return dayjs().add(offset, 'day').format('YYYY-MM-DD'); }; // Flatten rows to create one box per lane const flattenRows = (rows: any[]) => { const flattened: any[] = []; rows.forEach(row => { row.lanes.forEach((lane: any) => { flattened.push({ truckDepartureTime: row.truckDepartureTime, lane: lane }); }); }); return flattened; }; return ( {/* Date Selector Dropdown */} {t("Select Date")} {/* Grid containing both floors */} {/* 2/F 楼层面板 */} {/* Floor Label */} 2/F {/* Content Box */} {isLoadingSummary ? ( Loading... ) : !summary2F?.rows || summary2F.rows.length === 0 ? ( {t("No entries available")} ) : ( {flattenRows(summary2F.rows).map((item, idx) => ( {/* Time on the left */} {item.truckDepartureTime} {/* Single Button on the right */} ))} )} {/* 4/F 楼层面板 */} {/* Floor Label */} 4/F {/* Content Box */} {isLoadingSummary ? ( Loading... ) : !summary4F?.rows || summary4F.rows.length === 0 ? ( {t("No entries available")} ) : ( {flattenRows(summary4F.rows).map((item, idx) => ( {/* Time on the left */} {item.truckDepartureTime} {/* Single Button on the right */} ))} )} ); }; export default FinishedGoodFloorLanePanel;