FPSMS-frontend
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

FinishedGoodFloorLanePanel.tsx 15 KiB

4 settimane fa
2 mesi fa
2 mesi fa
2 mesi fa
2 mesi fa
2 mesi fa
2 mesi fa
1 mese fa
1 mese fa
2 mesi fa
2 mesi fa
4 settimane fa
1 mese fa
1 mese fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. "use client";
  2. import { Box, Button, Grid, Stack, Typography, Select, MenuItem, FormControl, InputLabel ,Tooltip} from "@mui/material";
  3. import { useCallback, useEffect, useState } from "react";
  4. import { useTranslation } from "react-i18next";
  5. import { useSession } from "next-auth/react";
  6. import { SessionWithTokens } from "@/config/authConfig";
  7. import { fetchStoreLaneSummary, assignByLane, type StoreLaneSummary } from "@/app/api/pickOrder/actions";
  8. import Swal from "sweetalert2";
  9. import dayjs from "dayjs";
  10. interface Props {
  11. onPickOrderAssigned?: () => void;
  12. onSwitchToDetailTab?: () => void;
  13. }
  14. const FinishedGoodFloorLanePanel: React.FC<Props> = ({ onPickOrderAssigned, onSwitchToDetailTab }) => {
  15. const { t } = useTranslation("pickOrder");
  16. const { data: session } = useSession() as { data: SessionWithTokens | null };
  17. const currentUserId = session?.id ? parseInt(session.id) : undefined;
  18. const [summary2F, setSummary2F] = useState<StoreLaneSummary | null>(null);
  19. const [summary4F, setSummary4F] = useState<StoreLaneSummary | null>(null);
  20. const [isLoadingSummary, setIsLoadingSummary] = useState(false);
  21. const [isAssigning, setIsAssigning] = useState(false);
  22. //const [selectedDate, setSelectedDate] = useState<string>("today");
  23. const [selectedDate, setSelectedDate] = useState<string>("today");
  24. const [releaseType, setReleaseType] = useState<string>("batch");
  25. const loadSummaries = useCallback(async () => {
  26. setIsLoadingSummary(true);
  27. try {
  28. // Convert selectedDate to the format needed
  29. let dateParam: string | undefined;
  30. if (selectedDate === "today") {
  31. dateParam = dayjs().format('YYYY-MM-DD');
  32. } else if (selectedDate === "tomorrow") {
  33. dateParam = dayjs().add(1, 'day').format('YYYY-MM-DD');
  34. } else if (selectedDate === "dayAfterTomorrow") {
  35. dateParam = dayjs().add(2, 'day').format('YYYY-MM-DD');
  36. }
  37. const [s2, s4] = await Promise.all([
  38. fetchStoreLaneSummary("2/F", dateParam, releaseType),
  39. fetchStoreLaneSummary("4/F", dateParam, releaseType)
  40. ]);
  41. setSummary2F(s2);
  42. setSummary4F(s4);
  43. } catch (error) {
  44. console.error("Error loading summaries:", error);
  45. } finally {
  46. setIsLoadingSummary(false);
  47. }
  48. }, [selectedDate, releaseType]);
  49. // 初始化
  50. useEffect(() => {
  51. loadSummaries();
  52. }, [loadSummaries]);
  53. const handleAssignByLane = useCallback(async (
  54. storeId: string,
  55. truckDepartureTime: string,
  56. truckLanceCode: string,
  57. requiredDate: string
  58. ) => {
  59. if (!currentUserId) {
  60. console.error("Missing user id in session");
  61. return;
  62. }
  63. let dateParam: string | undefined;
  64. if (requiredDate === "today") {
  65. dateParam = dayjs().format('YYYY-MM-DD');
  66. } else if (requiredDate === "tomorrow") {
  67. dateParam = dayjs().add(1, 'day').format('YYYY-MM-DD');
  68. } else if (requiredDate === "dayAfterTomorrow") {
  69. dateParam = dayjs().add(2, 'day').format('YYYY-MM-DD');
  70. }
  71. setIsAssigning(true);
  72. try {
  73. const res = await assignByLane(currentUserId, storeId, truckLanceCode, truckDepartureTime, dateParam);
  74. if (res.code === "SUCCESS") {
  75. console.log(" Successfully assigned pick order from lane", truckLanceCode);
  76. window.dispatchEvent(new CustomEvent('pickOrderAssigned'));
  77. loadSummaries(); // 刷新按钮状态
  78. onPickOrderAssigned?.();
  79. onSwitchToDetailTab?.();
  80. } else if (res.code === "USER_BUSY") {
  81. Swal.fire({
  82. icon: "warning",
  83. title: t("Warning"),
  84. text: t("You already have a pick order in progess. Please complete it first before taking next pick order."),
  85. confirmButtonText: t("Confirm"),
  86. confirmButtonColor: "#8dba00"
  87. });
  88. window.dispatchEvent(new CustomEvent('pickOrderAssigned'));
  89. } else if (res.code === "NO_ORDERS") {
  90. Swal.fire({
  91. icon: "info",
  92. title: t("Info"),
  93. text: t("No available pick order(s) for this lane."),
  94. confirmButtonText: t("Confirm"),
  95. confirmButtonColor: "#8dba00"
  96. });
  97. } else {
  98. console.log("ℹ️ Assignment result:", res.message);
  99. }
  100. } catch (error) {
  101. console.error("❌ Error assigning by lane:", error);
  102. Swal.fire({
  103. icon: "error",
  104. title: t("Error"),
  105. text: t("Error occurred during assignment."),
  106. confirmButtonText: t("Confirm"),
  107. confirmButtonColor: "#8dba00"
  108. });
  109. } finally {
  110. setIsAssigning(false);
  111. }
  112. }, [currentUserId, t, selectedDate, onPickOrderAssigned, onSwitchToDetailTab, loadSummaries]);
  113. const getDateLabel = (offset: number) => {
  114. return dayjs().add(offset, 'day').format('YYYY-MM-DD');
  115. };
  116. // Flatten rows to create one box per lane
  117. const flattenRows = (rows: any[]) => {
  118. const flattened: any[] = [];
  119. rows.forEach(row => {
  120. row.lanes.forEach((lane: any) => {
  121. flattened.push({
  122. truckDepartureTime: row.truckDepartureTime,
  123. lane: lane
  124. });
  125. });
  126. });
  127. return flattened;
  128. };
  129. return (
  130. <Box sx={{ mb: 2 }}>
  131. {/* Date Selector Dropdown and Legend */}
  132. <Stack direction="row" spacing={2} sx={{ mb: 2, alignItems: 'flex-start' }}>
  133. <Box sx={{ maxWidth: 300 }}>
  134. <FormControl fullWidth size="small">
  135. <InputLabel id="date-select-label">{t("Select Date")}</InputLabel>
  136. <Select
  137. labelId="date-select-label"
  138. id="date-select"
  139. value={selectedDate}
  140. label={t("Select Date")}
  141. onChange={(e) => { {
  142. setSelectedDate(e.target.value);
  143. loadSummaries();
  144. }}}
  145. >
  146. <MenuItem value="today">
  147. {t("Today")} ({getDateLabel(0)})
  148. </MenuItem>
  149. <MenuItem value="tomorrow">
  150. {t("Tomorrow")} ({getDateLabel(1)})
  151. </MenuItem>
  152. <MenuItem value="dayAfterTomorrow">
  153. {t("Day After Tomorrow")} ({getDateLabel(2)})
  154. </MenuItem>
  155. </Select>
  156. </FormControl>
  157. </Box>
  158. <Box sx={{minWidth: 140, maxWidth: 300 }}>
  159. <FormControl fullWidth size="small">
  160. <InputLabel id="release-type-select-label">{t("Release Type")}</InputLabel>
  161. <Select
  162. labelId="release-type-select-label"
  163. id="release-type-select"
  164. value={releaseType}
  165. label={t("Release Type")}
  166. onChange={(e) => { {
  167. setReleaseType(e.target.value);
  168. loadSummaries();
  169. }}}
  170. >
  171. <MenuItem value="batch">
  172. {t("Batch")}
  173. </MenuItem>
  174. <MenuItem value="single">
  175. {t("Single")}
  176. </MenuItem>
  177. </Select>
  178. </FormControl>
  179. </Box>
  180. <Box
  181. sx={{
  182. p: 1,
  183. backgroundColor: '#fafafa',
  184. borderRadius: 1,
  185. border: '1px solid #e0e0e0',
  186. flex: 1,
  187. maxWidth: 400
  188. }}
  189. >
  190. <Typography variant="body2" sx={{ display: 'block', color: 'text.secondary', fontWeight: 600 }}>
  191. {t("EDT - Lane Code (Unassigned/Total)")}
  192. </Typography>
  193. </Box>
  194. </Stack>
  195. <Stack direction="row" spacing={2} sx={{ mb: 2, alignItems: 'flex-start' }}>
  196. </Stack>
  197. {/* Grid containing both floors */}
  198. <Grid container spacing={2}>
  199. {/* 2/F 楼层面板 */}
  200. <Grid item xs={12}>
  201. <Stack direction="row" spacing={2} alignItems="flex-start">
  202. {/* Floor Label */}
  203. <Typography
  204. variant="h6"
  205. sx={{
  206. fontWeight: 600,
  207. minWidth: 60,
  208. pt: 1
  209. }}
  210. >
  211. 2/F
  212. </Typography>
  213. {/* Content Box */}
  214. <Box
  215. sx={{
  216. border: '1px solid #e0e0e0',
  217. borderRadius: 1,
  218. p: 1,
  219. backgroundColor: '#fafafa',
  220. flex: 1
  221. }}
  222. >
  223. {isLoadingSummary ? (
  224. <Typography variant="caption"> {t("Loading...")}</Typography>
  225. ) : !summary2F?.rows || summary2F.rows.length === 0 ? (
  226. <Typography
  227. variant="body2"
  228. color="text.secondary"
  229. sx={{
  230. fontWeight: 600,
  231. fontSize: '1rem',
  232. textAlign: 'center',
  233. py: 1
  234. }}
  235. >
  236. {t("No entries available")}
  237. </Typography>
  238. ) : (
  239. <Grid container spacing={1}>
  240. {flattenRows(summary2F.rows).map((item, idx) => (
  241. <Grid item xs={12} sm={6} md={3} key={idx}>
  242. <Stack
  243. direction="row"
  244. spacing={1}
  245. alignItems="center"
  246. sx={{
  247. border: '1px solid #e0e0e0',
  248. borderRadius: 0.5,
  249. p: 1,
  250. backgroundColor: '#fff',
  251. height: '100%'
  252. }}
  253. >
  254. {/* Time on the left */}
  255. <Typography
  256. variant="body2"
  257. sx={{
  258. fontWeight: 600,
  259. fontSize: '1rem',
  260. minWidth: 50,
  261. whiteSpace: 'nowrap'
  262. }}
  263. >
  264. {item.truckDepartureTime}
  265. </Typography>
  266. {/* Single Button on the right */}
  267. <Button
  268. variant="outlined"
  269. size="medium"
  270. disabled={item.lane.unassigned === 0 || isAssigning}
  271. onClick={() => handleAssignByLane("2/F", item.truckDepartureTime, item.lane.truckLanceCode, selectedDate)}
  272. sx={{
  273. flex: 1,
  274. fontSize: '1.1rem',
  275. py: 1,
  276. px: 1.5,
  277. borderWidth: 1,
  278. borderColor: '#ccc',
  279. fontWeight: 500,
  280. '&:hover': {
  281. borderColor: '#999',
  282. backgroundColor: '#f5f5f5'
  283. }
  284. }}
  285. >
  286. {`${item.lane.truckLanceCode} (${item.lane.unassigned}/${item.lane.total})`}
  287. </Button>
  288. </Stack>
  289. </Grid>
  290. ))}
  291. </Grid>
  292. )}
  293. </Box>
  294. </Stack>
  295. </Grid>
  296. {/* 4/F 楼层面板 */}
  297. <Grid item xs={12}>
  298. <Stack direction="row" spacing={2} alignItems="flex-start">
  299. {/* Floor Label */}
  300. <Typography
  301. variant="h6"
  302. sx={{
  303. fontWeight: 600,
  304. minWidth: 60,
  305. pt: 1
  306. }}
  307. >
  308. 4/F
  309. </Typography>
  310. {/* Content Box */}
  311. <Box
  312. sx={{
  313. border: '1px solid #e0e0e0',
  314. borderRadius: 1,
  315. p: 1,
  316. backgroundColor: '#fafafa',
  317. flex: 1
  318. }}
  319. >
  320. {isLoadingSummary ? (
  321. <Typography variant="caption">{t("Loading...")}</Typography>
  322. ) : !summary4F?.rows || summary4F.rows.length === 0 ? (
  323. <Typography
  324. variant="body2"
  325. color="text.secondary"
  326. sx={{
  327. fontWeight: 600,
  328. fontSize: '1rem',
  329. textAlign: 'center',
  330. py: 1
  331. }}
  332. >
  333. {t("No entries available")}
  334. </Typography>
  335. ) : (
  336. <Grid container spacing={1}>
  337. {flattenRows(summary4F.rows).map((item, idx) => (
  338. <Grid item xs={12} sm={6} md={3} key={idx}>
  339. <Stack
  340. direction="row"
  341. spacing={1}
  342. alignItems="center"
  343. sx={{
  344. border: '1px solid #e0e0e0',
  345. borderRadius: 0.5,
  346. p: 1,
  347. backgroundColor: '#fff',
  348. height: '100%'
  349. }}
  350. >
  351. {/* Time on the left */}
  352. <Typography
  353. variant="body2"
  354. sx={{
  355. fontWeight: 600,
  356. fontSize: '1rem',
  357. minWidth: 50,
  358. whiteSpace: 'nowrap'
  359. }}
  360. >
  361. {item.truckDepartureTime}
  362. </Typography>
  363. {/* Single Button on the right */}
  364. <Button
  365. variant="outlined"
  366. size="medium"
  367. disabled={item.lane.unassigned === 0 || isAssigning}
  368. onClick={() => handleAssignByLane("4/F", item.truckDepartureTime, item.lane.truckLanceCode, selectedDate)}
  369. sx={{
  370. flex: 1,
  371. fontSize: '1.1rem',
  372. py: 1,
  373. px: 1.5,
  374. borderWidth: 1,
  375. borderColor: '#ccc',
  376. fontWeight: 500,
  377. '&:hover': {
  378. borderColor: '#999',
  379. backgroundColor: '#f5f5f5'
  380. }
  381. }}
  382. >
  383. {`${item.lane.truckLanceCode} (${item.lane.unassigned}/${item.lane.total})`}
  384. </Button>
  385. </Stack>
  386. </Grid>
  387. ))}
  388. </Grid>
  389. )}
  390. </Box>
  391. </Stack>
  392. </Grid>
  393. </Grid>
  394. </Box>
  395. );
  396. };
  397. export default FinishedGoodFloorLanePanel;