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.
 
 

142 line
6.2 KiB

  1. "use client";
  2. import Stack from "@mui/material/Stack";
  3. import Box from "@mui/material/Box";
  4. import Card from "@mui/material/Card";
  5. import CardContent from "@mui/material/CardContent";
  6. import Grid from "@mui/material/Grid";
  7. import TextField from "@mui/material/TextField";
  8. import Typography from "@mui/material/Typography";
  9. import { useTranslation } from "react-i18next";
  10. import CardActions from "@mui/material/CardActions";
  11. import RestartAlt from "@mui/icons-material/RestartAlt";
  12. import Button from "@mui/material/Button";
  13. import { Controller, useFormContext } from "react-hook-form";
  14. import { CustomerFormInputs } from "@/app/api/customer/actions";
  15. import { FormControl, InputLabel, MenuItem, Select } from "@mui/material";
  16. import ContactInfo from "./ContactInfo";
  17. import { useCallback } from "react";
  18. import { SubsidiaryType } from "@/app/api/subsidiary";
  19. interface Props {
  20. subsidiaryTypes: SubsidiaryType[],
  21. }
  22. const SubsidiaryInfo: React.FC<Props> = ({
  23. subsidiaryTypes,
  24. }) => {
  25. const { t } = useTranslation();
  26. const {
  27. register,
  28. formState: { errors, defaultValues },
  29. control,
  30. reset,
  31. resetField,
  32. setValue
  33. } = useFormContext<CustomerFormInputs>();
  34. const resetSubsidiary = useCallback(() => {
  35. if (defaultValues !== undefined) {
  36. resetField("code")
  37. resetField("name")
  38. resetField("address")
  39. resetField("district")
  40. resetField("typeId")
  41. resetField("brNo")
  42. }
  43. }, [defaultValues])
  44. return (
  45. <>
  46. <Card sx={{ display: "block" }}>
  47. <CardContent component={Stack} spacing={4}>
  48. <Box>
  49. <Typography variant="overline" display="block" marginBlockEnd={1}>
  50. {t("Subsidiary Info")}
  51. </Typography>
  52. <Grid container spacing={2} columns={{ xs: 6, sm: 12 }}>
  53. <Grid item xs={6}>
  54. <TextField
  55. label={`${t("Subsidiary Code")}*`}
  56. fullWidth
  57. {...register("code", {
  58. required: true,
  59. })}
  60. error={Boolean(errors.code)}
  61. helperText={Boolean(errors.code) && (errors.code?.message ? t(errors.code.message) : t("Please input correct Subsidiary code"))}
  62. />
  63. </Grid>
  64. <Grid item xs={6}>
  65. <TextField
  66. label={`${t("Subsidiary Name")}*`}
  67. fullWidth
  68. {...register("name", {
  69. required: true,
  70. })}
  71. error={Boolean(errors.name)}
  72. helperText={Boolean(errors.name) && t("Please input correct Subsidiary name")}
  73. />
  74. </Grid>
  75. <Grid item xs={6}>
  76. <TextField
  77. label={t("Subsidiary Address")}
  78. fullWidth
  79. {...register("address")}
  80. />
  81. </Grid>
  82. <Grid item xs={6}>
  83. <TextField
  84. label={t("Subsidiary District")}
  85. fullWidth
  86. {...register("district")}
  87. />
  88. </Grid>
  89. <Grid item xs={6}>
  90. <FormControl fullWidth>
  91. <InputLabel>{t("Subsidiary Type")}</InputLabel>
  92. <Controller
  93. defaultValue={subsidiaryTypes[0].id}
  94. control={control}
  95. name="typeId"
  96. render={({ field }) => (
  97. <Select label={t("Project Category")} {...field}>
  98. {subsidiaryTypes.map((type, index) => (
  99. <MenuItem
  100. key={`${type.id}-${index}`}
  101. value={type.id}
  102. >
  103. {t(type.name)}
  104. </MenuItem>
  105. ))}
  106. </Select>
  107. )}
  108. />
  109. </FormControl>
  110. </Grid>
  111. <Grid item xs={6}>
  112. <TextField
  113. label={t("Subsidiary Br No.")}
  114. fullWidth
  115. {...register("brNo", {
  116. pattern: /[0-9]{8}/,
  117. })}
  118. error={Boolean(errors.brNo)}
  119. helperText={Boolean(errors.brNo) && t("Please input correct subsidiary br no.")}
  120. />
  121. </Grid>
  122. </Grid>
  123. </Box>
  124. <CardActions sx={{ justifyContent: "flex-end" }}>
  125. <Button onClick={resetSubsidiary} variant="text" startIcon={<RestartAlt />}>
  126. {t("Reset")}
  127. </Button>
  128. </CardActions>
  129. </CardContent>
  130. </Card>
  131. <ContactInfo />
  132. </>
  133. );
  134. };
  135. export default SubsidiaryInfo;