FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

219 lines
6.0 KiB

  1. "use client";
  2. import { useCallback, useEffect, useMemo, useState } from "react";
  3. import { useRouter, useSearchParams } from "next/navigation";
  4. import { useTranslation } from "react-i18next";
  5. import {
  6. FormProvider,
  7. SubmitErrorHandler,
  8. SubmitHandler,
  9. useForm,
  10. } from "react-hook-form";
  11. import {
  12. Box,
  13. Button,
  14. Grid,
  15. Link,
  16. Stack,
  17. Tab,
  18. Tabs,
  19. TabsProps,
  20. Typography,
  21. } from "@mui/material";
  22. import { Add, Check, Close, EditNote } from "@mui/icons-material";
  23. import DetailInfoCard from "@/components/RoughScheduleDetail/DetailInfoCard";
  24. import ViewByFGDetails from "@/components/RoughScheduleDetail/ViewByFGDetails";
  25. import ViewByBomDetails from "@/components/RoughScheduleDetail/ViewByBomDetails";
  26. import { RoughProdScheduleResult, ScheduleType } from "@/app/api/scheduling";
  27. import { arrayToDayjs, dayjsToDateString } from "@/app/utils/formatUtil";
  28. import { useGridApiRef } from "@mui/x-data-grid";
  29. type Props = {
  30. isEditMode: boolean;
  31. type: ScheduleType;
  32. defaultValues: Partial<RoughProdScheduleResult> | undefined;
  33. // qcChecks: ItemQc[]
  34. };
  35. const RoughScheduleDetailView: React.FC<Props> = ({
  36. isEditMode,
  37. type,
  38. defaultValues,
  39. }) => {
  40. // console.log(type)
  41. const apiRef = useGridApiRef();
  42. const params = useSearchParams();
  43. const [serverError, setServerError] = useState("");
  44. const [tabIndex, setTabIndex] = useState(0);
  45. const { t } = useTranslation("schedule");
  46. const router = useRouter();
  47. const [isEdit, setIsEdit] = useState(false);
  48. //const title = "Demand Forecast Detail"
  49. // console.log(typeId)
  50. const formProps = useForm<RoughProdScheduleResult>({
  51. defaultValues: defaultValues ? defaultValues : {},
  52. });
  53. const errors = formProps.formState.errors;
  54. const handleTabChange = useCallback<NonNullable<TabsProps["onChange"]>>(
  55. (_e, newValue) => {
  56. setTabIndex(newValue);
  57. },
  58. [],
  59. );
  60. const dayPeriod = useMemo<string[]>(() => {
  61. const from = arrayToDayjs(formProps.getValues("schedulePeriod"));
  62. const to = arrayToDayjs(formProps.getValues("schedulePeriodTo"));
  63. const diffDays = Math.abs(from.diff(to, "day"));
  64. const result: string[] = [];
  65. for (let i = 0; i <= diffDays; i++) {
  66. result.push(dayjsToDateString(from.add(i, "day")));
  67. }
  68. return result;
  69. }, []);
  70. const [pagingController, setPagingController] = useState({
  71. pageNum: 1,
  72. pageSize: 10,
  73. totalCount: 0,
  74. });
  75. const handleCancel = () => {
  76. router.replace(`/scheduling/rough`);
  77. };
  78. const onSubmit = useCallback<SubmitHandler<RoughProdScheduleResult>>(
  79. async (data, event) => {
  80. const hasErrors = false;
  81. console.log(errors);
  82. // console.log(apiRef.current.getCellValue(2, "lowerLimit"))
  83. // apiRef.current.
  84. try {
  85. if (hasErrors) {
  86. setServerError(t("An error has occurred. Please try again later."));
  87. return false;
  88. }
  89. } catch (e) {
  90. // backend error
  91. setServerError(t("An error has occurred. Please try again later."));
  92. console.log(e);
  93. }
  94. },
  95. [apiRef, router, t],
  96. );
  97. // multiple tabs
  98. const onSubmitError = useCallback<
  99. SubmitErrorHandler<RoughProdScheduleResult>
  100. >((errors) => {}, []);
  101. const onClickEdit = () => {
  102. setIsEdit(!isEdit);
  103. };
  104. return (
  105. <>
  106. <FormProvider {...formProps}>
  107. <Stack
  108. spacing={2}
  109. component="form"
  110. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  111. >
  112. {/*<Grid>*/}
  113. {/* <Typography mb={2} variant="h4">*/}
  114. {/* {t(`${mode} ${title}`)}*/}
  115. {/* </Typography>*/}
  116. {/*</Grid>*/}
  117. <DetailInfoCard
  118. // recordDetails={{
  119. // id: 1,
  120. // scheduledPeriod: "2025-05-11 to 2025-05-17",
  121. // scheduledAt: "2025-05-07",
  122. // productCount: 13,
  123. // productionCount: 21000
  124. // }}
  125. isEditing={isEdit}
  126. />
  127. {/* <Stack
  128. direction="row"
  129. justifyContent="space-between"
  130. flexWrap="wrap"
  131. rowGap={2}
  132. >
  133. <Button
  134. variant="contained"
  135. onClick={onClickEdit}
  136. // startIcon={<Add />}
  137. //LinkComponent={Link}
  138. //href="qcCategory/create"
  139. >
  140. {isEdit ? t("Save") : t("Edit")}
  141. </Button>
  142. </Stack> */}
  143. <Tabs
  144. value={tabIndex}
  145. onChange={handleTabChange}
  146. variant="scrollable"
  147. >
  148. <Tab
  149. label={t("View By FG") + (tabIndex === 0 ? t(" (Selected)") : "")}
  150. iconPosition="end"
  151. />
  152. <Tab
  153. label={
  154. t("View By Material") + (tabIndex === 1 ? t(" (Selected)") : "")
  155. }
  156. iconPosition="end"
  157. />
  158. </Tabs>
  159. {serverError && (
  160. <Typography variant="body2" color="error" alignSelf="flex-end">
  161. {serverError}
  162. </Typography>
  163. )}
  164. {tabIndex === 0 && (
  165. <ViewByFGDetails
  166. type={type}
  167. isEdit={isEdit}
  168. apiRef={apiRef}
  169. dayPeriod={dayPeriod}
  170. />
  171. )}
  172. {tabIndex === 1 && (
  173. <ViewByBomDetails
  174. type={type}
  175. isEdit={isEdit}
  176. apiRef={apiRef}
  177. dayPeriod={dayPeriod}
  178. />
  179. )}
  180. {/* <Stack direction="row" justifyContent="flex-end" gap={1}>
  181. <Button
  182. name="submit"
  183. variant="contained"
  184. startIcon={<Check />}
  185. type="submit"
  186. // disabled={submitDisabled}
  187. >
  188. {isEditMode ? t("Save") : t("Confirm")}
  189. </Button>
  190. <Button
  191. variant="outlined"
  192. startIcon={<Close />}
  193. onClick={handleCancel}
  194. >
  195. {t("Cancel")}
  196. </Button>
  197. </Stack> */}
  198. </Stack>
  199. </FormProvider>
  200. </>
  201. );
  202. };
  203. export default RoughScheduleDetailView;