From 75f3e6a819995b0643238911cf7dbfc111934a75 Mon Sep 17 00:00:00 2001 From: "CANCERYS\\kw093" Date: Tue, 3 Mar 2026 16:53:21 +0800 Subject: [PATCH] update --- .../ReleasedDoPickOrderSelectModal.tsx | 228 ++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 src/components/FinishedGoodSearch/ReleasedDoPickOrderSelectModal.tsx diff --git a/src/components/FinishedGoodSearch/ReleasedDoPickOrderSelectModal.tsx b/src/components/FinishedGoodSearch/ReleasedDoPickOrderSelectModal.tsx new file mode 100644 index 0000000..beefe38 --- /dev/null +++ b/src/components/FinishedGoodSearch/ReleasedDoPickOrderSelectModal.tsx @@ -0,0 +1,228 @@ +// 新建檔案 +"use client"; + +import { + Modal, + Box, + Typography, + TextField, + Table, + TableBody, + TableCell, + TableContainer, + TableHead, + TableRow, + Paper, + CircularProgress, + Button, +} from "@mui/material"; +import { useCallback, useEffect, useState } from "react"; +import { useTranslation } from "react-i18next"; +import { + fetchReleasedDoPickOrdersForSelection, + assignByDoPickOrderId, + type ReleasedDoPickOrderListItem, +} from "@/app/api/pickOrder/actions"; +import { useSession } from "next-auth/react"; +import { SessionWithTokens } from "@/config/authConfig"; +import Swal from "sweetalert2"; +import dayjs from "dayjs"; + +interface Props { + open: boolean; + onClose: () => void; + onAssigned: () => void; + storeId: string; + truck: string; +} + +const ReleasedDoPickOrderSelectModal: React.FC = ({ + open, + onClose, + onAssigned, + storeId, + truck, +}) => { + const { t } = useTranslation("pickOrder"); + const { data: session } = useSession() as { data: SessionWithTokens | null }; + const currentUserId = session?.id ? parseInt(session.id) : undefined; + + const [list, setList] = useState([]); + const [loading, setLoading] = useState(false); + const [shopSearch, setShopSearch] = useState(""); + const [isAssigning, setIsAssigning] = useState(false); + + const loadList = useCallback(async () => { + if (!open) return; + setLoading(true); + try { + const data = await fetchReleasedDoPickOrdersForSelection( + shopSearch.trim() || undefined, + storeId, + truck?.trim() || undefined // 傳入 truck + ); + setList(data); + } catch (e) { + console.error(e); + setList([]); + } finally { + setLoading(false); + } + }, [open, shopSearch, storeId, truck]); + + useEffect(() => { + loadList(); + }, [loadList]); + + const handleSelectRow = useCallback( + async (item: ReleasedDoPickOrderListItem) => { + if (!currentUserId) return; + const confirmResult = await Swal.fire({ + title: t("Confirm Assignment"), + html: ` +
+

${t("Date")}: ${item.requiredDeliveryDate ?? "-"}

+

${t("Shop")}: ${item.shopName ?? item.shopCode ?? "-"}

+

${t("Truck")}: ${item.truckLanceCode ?? "-"}

+

${t("Delivery Order")}: ${(item.deliveryOrderCodes ?? []).join(", ")}

+
+ `, + icon: "question", + showCancelButton: true, + confirmButtonText: t("Confirm"), + cancelButtonText: t("Cancel"), + confirmButtonColor: "#8dba00", + cancelButtonColor: "#F04438", + didOpen: () => { + const container = document.querySelector('.swal2-container'); + const popup = Swal.getPopup(); + if (container) (container as HTMLElement).style.zIndex = '9999'; + if (popup) popup.style.zIndex = '9999'; + } + }); + + if (!confirmResult.isConfirmed) return; + + setIsAssigning(true); + try { + const res = await assignByDoPickOrderId(currentUserId, item.id); + if (res?.code === "SUCCESS") { + Swal.fire({ + icon: "success", + text: t("Assigned successfully"), + timer: 1500, + showConfirmButton: false, + }); + onAssigned?.(); + onClose(); + } else { + Swal.fire({ + icon: "error", + text: res?.message ?? t("Assignment failed"), + }); + } + } catch (e) { + console.error(e); + Swal.fire({ icon: "error", text: t("Error occurred during assignment.") }); + } finally { + setIsAssigning(false); + } + }, + [currentUserId, t, onAssigned, onClose] + ); + + return ( + + + + {t("Not Yet Finished Released Do Pick Orders")} + + + setShopSearch(e.target.value)} + sx={{ mb: 2 }} + fullWidth + /> + + {loading ? ( + + + + ) : ( + + + + + {t("Date")} + {t("Shop")} + {t("Truck")} + {t("Delivery Order Code")} + {t("Action")} + + + + {list.map((row) => ( + + + {row.requiredDeliveryDate + ? dayjs(row.requiredDeliveryDate).format("YYYY-MM-DD") + : "-"} + + {row.shopName ?? row.shopCode ?? "-"} + {row.truckLanceCode ?? "-"} + + {(row.deliveryOrderCodes ?? []).join("\n") || "-"} + + + + + + ))} + +
+
+ )} + + {!loading && list.length === 0 && ( + + {t("No entries available")} + + )} + + + + +
+
+ ); +}; + +export default ReleasedDoPickOrderSelectModal; \ No newline at end of file