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.

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