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.
 
 
 

210 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. 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, {FGRecord} from "@/components/RoughScheduleDetail/ViewByFGDetails";
  23. import ViewByBomDetails from "@/components/RoughScheduleDetail/ViewByBomDetails";
  24. import EditableSearchResults, {Column} from "@/components/SearchResults/EditableSearchResults";
  25. type Props = {
  26. isEditMode: boolean;
  27. // type: TypeEnum;
  28. defaultValues: Partial<CreateItemInputs> | undefined;
  29. qcChecks: ItemQc[]
  30. };
  31. const RoughScheduleDetailView: React.FC<Props> = ({
  32. isEditMode,
  33. // type,
  34. defaultValues,
  35. qcChecks
  36. }) => {
  37. // console.log(type)
  38. const apiRef = useGridApiRef();
  39. const params = useSearchParams()
  40. console.log(params.get("id"))
  41. const [serverError, setServerError] = useState("");
  42. const [tabIndex, setTabIndex] = useState(0);
  43. const { t } = useTranslation();
  44. const router = useRouter();
  45. const [isEdit, setIsEdit] = useState(false);
  46. //const title = "Rough Schedule Detail"
  47. const [mode, redirPath] = useMemo(() => {
  48. // var typeId = TypeEnum.CONSUMABLE_ID
  49. var title = "";
  50. var mode = "";
  51. var redirPath = "";
  52. // if (type === TypeEnum.MATERIAL) {
  53. // typeId = TypeEnum.MATERIAL_ID
  54. // title = "Material";
  55. // redirPath = "/settings/material";
  56. // }
  57. // if (type === TypeEnum.PRODUCT) {
  58. // typeId = TypeEnum.PRODUCT_ID
  59. title = "Product";
  60. redirPath = "scheduling/rough/edit";
  61. // }
  62. // if (type === TypeEnum.BYPRODUCT) {
  63. // typeId = TypeEnum.BYPRODUCT_ID
  64. // title = "By-Product";
  65. // redirPath = "/settings/byProduct";
  66. // }
  67. if (isEditMode) {
  68. mode = "Edit";
  69. } else {
  70. mode = "Create";
  71. }
  72. return [mode, redirPath];
  73. }, [isEditMode]);
  74. // console.log(typeId)
  75. const formProps = useForm<CreateItemInputs>({
  76. defaultValues: defaultValues ? defaultValues : {
  77. },
  78. });
  79. const errors = formProps.formState.errors;
  80. const handleTabChange = useCallback<NonNullable<TabsProps["onChange"]>>(
  81. (_e, newValue) => {
  82. setTabIndex(newValue);
  83. },
  84. [],
  85. );
  86. const [pagingController, setPagingController] = useState({
  87. pageNum: 1,
  88. pageSize: 10,
  89. totalCount: 0,
  90. })
  91. const handleCancel = () => {
  92. router.replace(`/scheduling/rough`);
  93. };
  94. const onSubmit = useCallback<SubmitHandler<CreateItemInputs & {}>>(
  95. async (data, event) => {
  96. let hasErrors = false;
  97. console.log(errors)
  98. // console.log(apiRef.current.getCellValue(2, "lowerLimit"))
  99. // apiRef.current.
  100. try {
  101. if (hasErrors) {
  102. setServerError(t("An error has occurred. Please try again later."));
  103. return false;
  104. }
  105. } catch (e) {
  106. // backend error
  107. setServerError(t("An error has occurred. Please try again later."));
  108. console.log(e);
  109. }
  110. },
  111. [apiRef, router, t]
  112. );
  113. // multiple tabs
  114. const onSubmitError = useCallback<SubmitErrorHandler<CreateItemInputs>>(
  115. (errors) => {},
  116. []
  117. );
  118. const onClickEdit = () =>{
  119. setIsEdit(!isEdit)
  120. }
  121. return (
  122. <>
  123. <FormProvider {...formProps}>
  124. <Stack
  125. spacing={2}
  126. component="form"
  127. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  128. >
  129. {/*<Grid>*/}
  130. {/* <Typography mb={2} variant="h4">*/}
  131. {/* {t(`${mode} ${title}`)}*/}
  132. {/* </Typography>*/}
  133. {/*</Grid>*/}
  134. <DetailInfoCard
  135. recordDetails={{
  136. id: 1,
  137. scheduledPeriod: "2025-05-11 to 2025-05-17",
  138. scheduledAt: "2025-05-07",
  139. productCount: 13,
  140. productionCount: 21000
  141. }}
  142. isEditing={isEdit}
  143. />
  144. <Stack
  145. direction="row"
  146. justifyContent="space-between"
  147. flexWrap="wrap"
  148. rowGap={2}
  149. >
  150. <Button
  151. variant="contained"
  152. onClick={onClickEdit}
  153. // startIcon={<Add />}
  154. //LinkComponent={Link}
  155. //href="qcCategory/create"
  156. >
  157. {isEdit ? t("Save") : t("Edit")}
  158. </Button>
  159. </Stack>
  160. <Tabs value={tabIndex} onChange={handleTabChange} variant="scrollable">
  161. <Tab label={t("View By Schedule")} iconPosition="end"/>
  162. <Tab label={t("View By Material")} iconPosition="end" />
  163. </Tabs>
  164. {serverError && (
  165. <Typography variant="body2" color="error" alignSelf="flex-end">
  166. {serverError}
  167. </Typography>
  168. )}
  169. {tabIndex === 0 && <ViewByFGDetails isEdit={isEdit} apiRef={apiRef}/>}
  170. {tabIndex === 1 && <ViewByBomDetails isEdit={isEdit} apiRef={apiRef} isHideButton={true} />}
  171. <Stack direction="row" justifyContent="flex-end" gap={1}>
  172. <Button
  173. name="submit"
  174. variant="contained"
  175. startIcon={<Check />}
  176. type="submit"
  177. // disabled={submitDisabled}
  178. >
  179. {isEditMode ? t("Save") : t("Confirm")}
  180. </Button>
  181. <Button
  182. variant="outlined"
  183. startIcon={<Close />}
  184. onClick={handleCancel}
  185. >
  186. {t("Cancel")}
  187. </Button>
  188. </Stack>
  189. </Stack>
  190. </FormProvider>
  191. </>
  192. );
  193. };
  194. export default RoughScheduleDetailView;