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.
 
 
 

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