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.

404 lines
19 KiB

  1. // material-ui
  2. import {
  3. Grid, Button,
  4. // Checkbox, FormControlLabel,
  5. Typography,
  6. Dialog, DialogTitle, DialogContent, DialogActions,
  7. FormHelperText, CircularProgress,
  8. } from '@mui/material';
  9. // import { FormControlLabel } from '@material-ui/core';
  10. import MainCard from "components/MainCard";
  11. import * as React from "react";
  12. import { useFormik } from 'formik';
  13. import * as yup from 'yup';
  14. import { useEffect, useState } from "react";
  15. import * as HttpUtils from 'utils/HttpUtils';
  16. import * as UrlUtils from "utils/ApiPathConst";
  17. import * as FieldUtils from "utils/FieldUtils";
  18. import * as ComboData from "utils/ComboData";
  19. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  20. import Loadable from 'components/Loadable';
  21. import { lazy } from 'react';
  22. import { notifySaveSuccess } from 'utils/CommonFunction';
  23. import { FormattedMessage, useIntl } from "react-intl";
  24. import { PNSPS_BUTTON_THEME } from "themes/buttonConst";
  25. import { ThemeProvider } from "@emotion/react";
  26. import { useNavigate } from "react-router-dom";
  27. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  28. const OrganizationPubCard = ({ userData, loadDataFun, id, setEditModeFun }) => {
  29. const intl = useIntl();
  30. const navigate = useNavigate();
  31. const [creditorConfirmPopUp, setCreditorConfirmPopUp] = React.useState(false);
  32. const [nonCreditorConfirmPopUp, setNonCreditorConfirmPopUp] = React.useState(false);
  33. const [currentUserData, setCurrentUserData] = useState({});
  34. const [editMode, setEditMode] = useState(false);
  35. const [createMode, setCreateMode] = useState(false);
  36. const [onReady, setOnReady] = useState(false);
  37. const [errorMsg, setErrorMsg] = useState("");
  38. useEffect(() => {
  39. //if state data are ready and assign to different field
  40. // console.log(currentApplicationDetailData)
  41. if (Object.keys(currentUserData).length > 0) {
  42. setOnReady(true);
  43. }
  44. }, [currentUserData]);
  45. function displayErrorMsg(errorMsg) {
  46. return <Typography variant="errorMessage1">{errorMsg}</Typography>
  47. }
  48. const formik = useFormik({
  49. enableReinitialize: true,
  50. initialValues: currentUserData,
  51. validationSchema: yup.object().shape({
  52. addressLine1: yup.string().max(40).required(displayErrorMsg(intl.formatMessage({ id: 'validateAddressLine1' }))),
  53. addressLine2: yup.string().max(40, displayErrorMsg(intl.formatMessage({ id: 'noMoreThen40Words' }))),
  54. addressLine3: yup.string().max(40, displayErrorMsg(intl.formatMessage({ id: 'noMoreThen40Words' }))),
  55. tel_countryCode: yup.string().min(3, displayErrorMsg(intl.formatMessage({ id: 'requireDialingCode' }))),
  56. phoneNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({ id: 'requiredValidNumber' }))).required(displayErrorMsg(intl.formatMessage({ id: 'requireContactNumber' }))),
  57. faxNumber: yup.string().min(8, displayErrorMsg(intl.formatMessage({ id: 'require8Number' }))).nullable(),
  58. }),
  59. onSubmit: (values) => {
  60. if (values.country == null) {
  61. setErrorMsg(intl.formatMessage({ id: 'pleaseFillInCountry' }))
  62. return;
  63. }
  64. if (values.country.type == "hongKong" && values.district == null) {
  65. setErrorMsg(intl.formatMessage({ id: 'pleaseFillInDistrict' }))
  66. return;
  67. }
  68. return new Promise((resolve, reject) => {
  69. HttpUtils.post({
  70. url: UrlUtils.POST_PUB_ORG_SAVE_PATH,
  71. params: {
  72. contactPerson: values.contactPerson,
  73. contactTel: {
  74. countryCode: values.tel_countryCode,
  75. phoneNumber: values.phoneNumber
  76. },
  77. faxNo: {
  78. countryCode: values.fax_countryCode,
  79. faxNumber: values.faxNumber
  80. },
  81. addressTemp: {
  82. country: values.country.type,
  83. district: values.district?.type,
  84. addressLine1: values.addressLine1,
  85. addressLine2: values.addressLine2,
  86. addressLine3: values.addressLine3,
  87. },
  88. },
  89. onSuccess: function () {
  90. notifySaveSuccess()
  91. loadDataFun();
  92. setEditMode(false);
  93. resolve();
  94. },
  95. onFail: () => reject(),
  96. onError: () => reject(),
  97. });
  98. });
  99. }
  100. });
  101. useEffect(() => {
  102. setEditModeFun(editMode);
  103. }, [editMode]);
  104. useEffect(() => {
  105. if (Object.keys(userData).length > 0) {
  106. setCreateMode(id <= 0);
  107. setEditMode(id <= 0);
  108. setCurrentUserData(userData);
  109. }
  110. }, [userData]);
  111. // useEffect(() => {
  112. // if (Object.keys(userData).length > 0) {
  113. // if(userData.creditor === undefined||userData.creditor === null){
  114. // userData.creditor = false
  115. // }
  116. // setCurrentUserData(userData);
  117. // }
  118. // }, []);
  119. const onEditClick = () => {
  120. setEditMode(true);
  121. };
  122. return (
  123. <MainCard elevation={0}
  124. border={false}
  125. content={false}
  126. >
  127. <form onSubmit={formik.handleSubmit} style={{ padding: 24 }}>
  128. {/*top*/}
  129. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  130. <Grid container maxWidth justifyContent="flex-start">
  131. {editMode ?
  132. <>
  133. {createMode ?
  134. <>
  135. <Grid item sx={{ ml: 0, mr: 3 }}>
  136. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  137. <Button
  138. variant="contained"
  139. type="submit"
  140. color="success"
  141. disabled={formik.isSubmitting}
  142. startIcon={formik.isSubmitting ? <CircularProgress color="inherit" size={18} /> : null}
  143. >
  144. <FormattedMessage id="create" />
  145. </Button>
  146. </ThemeProvider>
  147. </Grid>
  148. </> :
  149. <>
  150. <Grid item sx={{ ml: 0, mr: 3 }}>
  151. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  152. <Button
  153. variant="contained"
  154. color="cancel"
  155. onClick={loadDataFun}
  156. >
  157. <FormattedMessage id="resetAndBack" />
  158. </Button>
  159. </ThemeProvider>
  160. </Grid>
  161. <Grid item sx={{ ml: 3, mr: 3 }}>
  162. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  163. <Button
  164. variant="contained"
  165. type="submit"
  166. color="success"
  167. disabled={formik.isSubmitting}
  168. startIcon={formik.isSubmitting ? <CircularProgress color="inherit" size={18} /> : null}
  169. >
  170. <FormattedMessage id="save" />
  171. </Button>
  172. </ThemeProvider>
  173. </Grid>
  174. </>
  175. }
  176. </>
  177. :
  178. <>
  179. <Grid item sx={{ ml: 0, mr: 3 }}>
  180. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  181. <Button
  182. variant="contained"
  183. onClick={onEditClick}
  184. color="success"
  185. >
  186. < FormattedMessage id="edit" />
  187. </Button>
  188. </ThemeProvider>
  189. </Grid>
  190. {currentUserData.canSubmitBr ?
  191. <Grid item sx={{ ml: 3, mr: 3 }}>
  192. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  193. <Button
  194. variant="contained"
  195. onClick={() => navigate("/org/submit-br")}
  196. color="success"
  197. >
  198. <FormattedMessage id="submitBrCertificate" />
  199. </Button>
  200. </ThemeProvider>
  201. </Grid>
  202. : null}
  203. </>
  204. }
  205. </Grid>
  206. </Grid>
  207. {/*top*/}
  208. {!onReady ?
  209. <LoadingComponent />
  210. :
  211. <Grid container spacing={1}>
  212. {/* <Grid item xs={12}>
  213. <Typography variant="h4" sx={{ mb: 2, mr: 3, borderBottom: "1px solid black" }}>
  214. <FormattedMessage id="organizationDetails" />
  215. </Typography>
  216. </Grid> */}
  217. <Grid item xs={12}>
  218. <FormHelperText error id="helper-text-address1-signup">
  219. <Typography variant="errorMessage1">
  220. {errorMsg}
  221. </Typography>
  222. </FormHelperText>
  223. </Grid>
  224. <Grid item xs={12} lg={4} >
  225. {FieldUtils.getTextField({
  226. label: intl.formatMessage({ id: 'brNo' }) + ":",
  227. valueName: "brNo",
  228. disabled: true,
  229. form: formik
  230. })}
  231. </Grid>
  232. <Grid item xs={12} lg={4} >
  233. {FieldUtils.getTextField({
  234. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'expiryDate' }) + ":"),
  235. valueName: "brExpiryDate",
  236. disabled: true,
  237. form: formik,
  238. displayValue: (!editMode && !createMode && currentUserData.brStatus)
  239. ? `${formik.values.brExpiryDate || ""} (${currentUserData.brStatus === "Invalid"
  240. ? intl.formatMessage({ id: "brStatusInvalid" })
  241. : intl.formatMessage({ id: "brStatusValid" })})`.trim()
  242. : undefined
  243. })}
  244. </Grid>
  245. <Grid item xs={12} lg={4} >
  246. {FieldUtils.getTextField({
  247. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'nameEng' }) + ":"),
  248. valueName: "enCompanyName",
  249. disabled: true,
  250. form: formik
  251. })}
  252. </Grid>
  253. <Grid item xs={12} lg={4} >
  254. {FieldUtils.getTextField({
  255. label: intl.formatMessage({ id: 'nameChi' }) + ":",
  256. valueName: "chCompanyName",
  257. disabled: true,
  258. form: formik
  259. })}
  260. </Grid>
  261. <Grid item xs={12} lg={4} >
  262. </Grid>
  263. <Grid item xs={12} lg={4} >
  264. {FieldUtils.getTextField({
  265. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'contactPerson' }) + ":"),
  266. valueName: "contactPerson",
  267. disabled: (!editMode && !createMode),
  268. form: formik
  269. })}
  270. </Grid>
  271. <Grid item xs={12} lg={4} >
  272. {FieldUtils.getPhoneField({
  273. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'userContactNumber' }) + ":"),
  274. valueName: {
  275. code: "tel_countryCode",
  276. num: "phoneNumber"
  277. },
  278. disabled: (!editMode && !createMode),
  279. form: formik
  280. })}
  281. </Grid>
  282. <Grid item xs={12} lg={4} >
  283. {FieldUtils.getPhoneField({
  284. label: intl.formatMessage({ id: 'contactFaxNumber' }) + ":",
  285. valueName: {
  286. code: "fax_countryCode",
  287. num: "faxNumber"
  288. },
  289. disabled: (!editMode && !createMode),
  290. form: formik
  291. })}
  292. </Grid>
  293. <Grid item xs={12} lg={12} >
  294. {FieldUtils.getAddressField({
  295. label: FieldUtils.notNullFieldLabel(intl.formatMessage({ id: 'formAddress' }) + ":"),
  296. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  297. disabled: (!editMode && !createMode),
  298. form: formik
  299. })}
  300. </Grid>
  301. <Grid item xs={12} lg={12} >
  302. {FieldUtils.getProfileComboField({
  303. // label: FieldUtils.notNullFieldLabel(""),
  304. label: "",
  305. valueName: "district",
  306. disabled: (!editMode && !createMode),
  307. dataList: ComboData.district,
  308. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  309. form: formik
  310. })}
  311. </Grid>
  312. <Grid item xs={12} lg={12} >
  313. {FieldUtils.getProfileComboField({
  314. // label: FieldUtils.notNullFieldLabel(""),
  315. label: "",
  316. valueName: "country",
  317. disabled: true,
  318. dataList: ComboData.country,
  319. getOptionLabel: (option) => option.type ? intl.formatMessage({ id: option.type }) : "",
  320. form: formik
  321. })}
  322. </Grid>
  323. <Grid item lg={12} ></Grid>
  324. </Grid>
  325. }
  326. </form>
  327. <div>
  328. <Dialog
  329. open={creditorConfirmPopUp}
  330. onClose={() => setCreditorConfirmPopUp(false)}
  331. PaperProps={{
  332. sx: {
  333. minWidth: '40vw',
  334. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  335. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  336. }
  337. }}
  338. >
  339. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  340. <DialogContent style={{ display: 'flex', }}>
  341. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Credit Client?</Typography>
  342. </DialogContent>
  343. <DialogActions>
  344. <Button onClick={() => setCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  345. <Button onClick={() => markAsCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  346. </DialogActions>
  347. </Dialog>
  348. </div>
  349. <div>
  350. <Dialog
  351. open={nonCreditorConfirmPopUp}
  352. onClose={() => setNonCreditorConfirmPopUp(false)}
  353. PaperProps={{
  354. sx: {
  355. minWidth: '40vw',
  356. maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
  357. maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
  358. }
  359. }}
  360. >
  361. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  362. <DialogContent style={{ display: 'flex', }}>
  363. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Non-Credit Client?</Typography>
  364. </DialogContent>
  365. <DialogActions>
  366. <Button onClick={() => setNonCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  367. <Button onClick={() => markAsNonCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  368. </DialogActions>
  369. </Dialog>
  370. </div>
  371. </MainCard>
  372. );
  373. };
  374. export default OrganizationPubCard;