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.
 
 

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