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.
 
 

423 lines
19 KiB

  1. // material-ui
  2. import {
  3. Grid, Button, Checkbox, FormControlLabel, Typography,
  4. Dialog, DialogTitle, DialogContent, DialogActions,
  5. } from '@mui/material';
  6. // import { FormControlLabel } from '@material-ui/core';
  7. import MainCard from "../../../components/MainCard";
  8. import * as React from "react";
  9. import { useFormik } from 'formik';
  10. import * as yup from 'yup';
  11. import { useEffect, useState } from "react";
  12. import * as HttpUtils from '../../../utils/HttpUtils';
  13. import * as UrlUtils from "../../../utils/ApiPathConst";
  14. import * as FieldUtils from "../../../utils/FieldUtils";
  15. import * as ComboData from "../../../utils/ComboData";
  16. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  17. import Loadable from 'components/Loadable';
  18. import { lazy } from 'react';
  19. import { notifySaveSuccess } from 'utils/CommonFunction';
  20. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  21. const OrganizationCard = ({ userData, loadDataFun, id, setEditModeFun }) => {
  22. const [creditorConfirmPopUp, setCreditorConfirmPopUp] = React.useState(false);
  23. const [nonCreditorConfirmPopUp, setNonCreditorConfirmPopUp] = React.useState(false);
  24. const [currentUserData, setCurrentUserData] = useState({});
  25. const [editMode, setEditMode] = useState(false);
  26. const [createMode, setCreateMode] = useState(false);
  27. const [onReady, setOnReady] = useState(false);
  28. useEffect(() => {
  29. //if state data are ready and assign to different field
  30. // console.log(currentApplicationDetailData)
  31. if (Object.keys(currentUserData).length > 0) {
  32. setOnReady(true);
  33. }
  34. }, [currentUserData]);
  35. function displayErrorMsg(errorMsg) {
  36. return <Typography variant="errorMessage1">{errorMsg}</Typography>
  37. }
  38. const formik = useFormik({
  39. enableReinitialize: true,
  40. initialValues: currentUserData,
  41. validationSchema: yup.object().shape({
  42. enCompanyName: yup.string().max(255).required(displayErrorMsg('請輸入英文名稱')),
  43. chCompanyName: yup.string().max(255, displayErrorMsg('請輸入中文名稱')).nullable(),
  44. addressLine1: yup.string().max(255).required(displayErrorMsg('請輸入第一行地址')),
  45. addressLine2: yup.string().max(255, displayErrorMsg("length must <= 255")),
  46. addressLine3: yup.string().max(255, displayErrorMsg("length must <= 255")),
  47. fax_countryCode: yup.string().min(3, displayErrorMsg('請輸入國際區號')).nullable(),
  48. tel_countryCode: yup.string().min(3, displayErrorMsg('請輸入國際區號')),
  49. phoneNumber: yup.string().min(8, displayErrorMsg('請輸入有效聯絡電話')).required(displayErrorMsg('請輸入聯絡電話')),
  50. faxNumber: yup.string().min(8, displayErrorMsg('請輸入8位數字')).nullable(),
  51. brExpiryDate: yup.string().min(8).required(displayErrorMsg('請輸入商業登記證有效日期')),
  52. brNo: yup.string().required(displayErrorMsg('請輸入商業登記證號碼')).test('checkBrNoFormat', displayErrorMsg(displayErrorMsg(`請輸入有效商業登記證號碼`)), function (value) {
  53. var brNo_pattern = /[0-9]{8}/
  54. if (value !== undefined) {
  55. if (value.match(brNo_pattern)) {
  56. return true
  57. } else {
  58. return false
  59. }
  60. }
  61. }),
  62. }),
  63. onSubmit: vaule => {
  64. console.log(vaule)
  65. HttpUtils.post({
  66. url: UrlUtils.POST_ORG_SAVE_PATH,
  67. params: {
  68. id: id > 0 ? id : null,
  69. enCompanyName: vaule.enCompanyName,
  70. chCompanyName: vaule.chCompanyName,
  71. brNo: vaule.brNo,
  72. brExpiryDate: vaule.brExpiryDate,
  73. enCompanyNameTemp: vaule.enCompanyNameTemp,
  74. chCompanyNameTemp: vaule.chCompanyNameTemp,
  75. brExpiryDateTemp: vaule.brExpiryDateTemp,
  76. contactPerson: vaule.contactPerson,
  77. contactTel: {
  78. countryCode: vaule.tel_countryCode,
  79. phoneNumber: vaule.phoneNumber
  80. },
  81. faxNo: {
  82. countryCode: vaule.fax_countryCode,
  83. faxNumber: vaule.faxNumber
  84. },
  85. addressTemp: {
  86. country: vaule.country,
  87. district: vaule.district,
  88. addressLine1: vaule.addressLine1,
  89. addressLine2: vaule.addressLine2,
  90. addressLine3: vaule.addressLine3,
  91. },
  92. //creditor: vaule.creditor,
  93. },
  94. onSuccess: function () {
  95. notifySaveSuccess()
  96. loadDataFun();
  97. setEditMode(false);
  98. }
  99. });
  100. }
  101. });
  102. useEffect(()=>{
  103. setEditModeFun(editMode);
  104. },[editMode]);
  105. useEffect(() => {
  106. if (Object.keys(userData).length > 0) {
  107. setCreateMode(id <= 0);
  108. setEditMode(id <= 0);
  109. setCurrentUserData(userData);
  110. }
  111. }, [userData]);
  112. // useEffect(() => {
  113. // if (Object.keys(userData).length > 0) {
  114. // if(userData.creditor === undefined||userData.creditor === null){
  115. // userData.creditor = false
  116. // }
  117. // setCurrentUserData(userData);
  118. // }
  119. // }, []);
  120. const onEditClick = () => {
  121. setEditMode(true);
  122. };
  123. const markAsCreditor = () => {
  124. setCreditorConfirmPopUp(false);
  125. HttpUtils.get({
  126. url: UrlUtils.GET_ORG_MARK_AS_CREDITOR + "/" + id,
  127. onSuccess: () => {
  128. loadDataFun();
  129. }
  130. });
  131. }
  132. const markAsNonCreditor = () => {
  133. setNonCreditorConfirmPopUp(false);
  134. HttpUtils.get({
  135. url: UrlUtils.GET_ORG_MARK_AS_NON_CREDITOR + "/" + id,
  136. onSuccess: () => {
  137. loadDataFun();
  138. }
  139. });
  140. }
  141. return (
  142. <MainCard elevation={0}
  143. border={false}
  144. content={false}
  145. >
  146. <form onSubmit={formik.handleSubmit} style={{ padding: 24 }}>
  147. {/*top*/}
  148. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"start"} justifyContent="center">
  149. <Grid container maxWidth justifyContent="flex-start">
  150. {editMode ?
  151. <>
  152. {createMode ?
  153. <>
  154. <Grid item sx={{ ml: 0, mr: 3 }}>
  155. <Button
  156. size="large"
  157. variant="contained"
  158. type="submit"
  159. sx={{
  160. textTransform: 'capitalize',
  161. alignItems: 'end'
  162. }}
  163. >
  164. <Typography variant="h5">Create</Typography>
  165. </Button>
  166. </Grid>
  167. </> :
  168. <>
  169. <Grid item sx={{ ml: 0, mr: 3 }}>
  170. <Button
  171. size="large"
  172. variant="contained"
  173. onClick={loadDataFun}
  174. sx={{
  175. textTransform: 'capitalize',
  176. alignItems: 'end'
  177. }}
  178. >
  179. <Typography variant="h5">Reset & Back</Typography>
  180. </Button>
  181. </Grid>
  182. <Grid item sx={{ ml: 3, mr: 3 }}>
  183. <Button
  184. size="large"
  185. variant="contained"
  186. type="submit"
  187. color="success"
  188. sx={{
  189. textTransform: 'capitalize',
  190. alignItems: 'end'
  191. }}
  192. >
  193. <Typography variant="h5">Save</Typography>
  194. </Button>
  195. </Grid>
  196. </>
  197. }
  198. </>
  199. :
  200. <>
  201. <Grid item sx={{ ml: 0, mr: 3 }}>
  202. <Button
  203. size="large"
  204. variant="contained"
  205. sx={{
  206. textTransform: 'capitalize',
  207. alignItems: 'end'
  208. }}
  209. onClick={onEditClick}
  210. >
  211. <Typography variant="h5">Edit</Typography>
  212. </Button>
  213. </Grid>
  214. <Grid item sx={{ ml: 3, mr: 3 }}>
  215. <Button
  216. size="large"
  217. variant="contained"
  218. color="orange"
  219. onClick={()=>setCreditorConfirmPopUp(true)}
  220. sx={{
  221. textTransform: 'capitalize',
  222. alignItems: 'end'
  223. }}
  224. >
  225. <Typography variant="h5">Mark as Creditor</Typography>
  226. </Button>
  227. </Grid>
  228. <Grid item sx={{ ml: 3, mr: 3 }}>
  229. <Button
  230. size="large"
  231. variant="contained"
  232. color="error"
  233. onClick={()=>setNonCreditorConfirmPopUp(true)}
  234. sx={{
  235. textTransform: 'capitalize',
  236. alignItems: 'end'
  237. }}
  238. >
  239. <Typography variant="h5">Mark as Non-Creditor</Typography>
  240. </Button>
  241. </Grid>
  242. </>
  243. }
  244. </Grid>
  245. </Grid>
  246. {/*top*/}
  247. {!onReady ?
  248. <LoadingComponent />
  249. :
  250. <Grid container spacing={1}>
  251. <Grid item xs={12}>
  252. <Typography variant="h4" sx={{ mb: 2, mr: 3, borderBottom: "1px solid black" }}>
  253. Organisation Details
  254. </Typography>
  255. </Grid>
  256. <Grid item lg={4} >
  257. {FieldUtils.getTextField({
  258. label: FieldUtils.notNullFieldLabel("BR No.:"),
  259. valueName: "brNo",
  260. disabled: (!editMode && !createMode),
  261. form: formik
  262. })}
  263. </Grid>
  264. <Grid item lg={4} >
  265. <FormControlLabel
  266. control={<Checkbox checked={formik.values.creditor} />}
  267. label="is Creditor"
  268. name="creditor"
  269. onChange={() => {
  270. formik.setFieldValue("creditor", !formik.values.creditor);
  271. }}
  272. disabled={true}
  273. //disabled={!editMode && !createMode}
  274. />
  275. </Grid>
  276. <Grid item lg={4} ></Grid>
  277. <Grid item lg={4} >
  278. {FieldUtils.getTextField({
  279. label: FieldUtils.notNullFieldLabel("Name (Eng):"),
  280. valueName: "enCompanyName",
  281. disabled: (!editMode && !createMode),
  282. form: formik
  283. })}
  284. </Grid>
  285. <Grid item lg={4} >
  286. {FieldUtils.getTextField({
  287. label: "Name (Ch):",
  288. valueName: "chCompanyName",
  289. disabled: (!editMode && !createMode),
  290. form: formik
  291. })}
  292. </Grid>
  293. <Grid item lg={4} >
  294. {FieldUtils.getDateField({
  295. label: FieldUtils.notNullFieldLabel("Expiry Date:"),
  296. valueName: "brExpiryDate",
  297. disabled: (!editMode && !createMode),
  298. form: formik
  299. })}
  300. </Grid>
  301. <Grid item lg={4} >
  302. {FieldUtils.getTextField({
  303. label: FieldUtils.notNullFieldLabel("Contact Person:"),
  304. valueName: "contactPerson",
  305. disabled: (!editMode && !createMode),
  306. form: formik
  307. })}
  308. </Grid>
  309. <Grid item lg={4} >
  310. {FieldUtils.getPhoneField({
  311. label: FieldUtils.notNullFieldLabel("Contact Tel:"),
  312. valueName: {
  313. code: "tel_countryCode",
  314. num: "phoneNumber"
  315. },
  316. disabled: (!editMode && !createMode),
  317. form: formik
  318. })}
  319. </Grid>
  320. <Grid item lg={4} >
  321. {FieldUtils.getPhoneField({
  322. label: "Fax No:",
  323. valueName: {
  324. code: "fax_countryCode",
  325. num: "faxNumber"
  326. },
  327. disabled: (!editMode && !createMode),
  328. form: formik
  329. })}
  330. </Grid>
  331. <Grid item lg={4} >
  332. {FieldUtils.getComboField({
  333. label: FieldUtils.notNullFieldLabel("Country:"),
  334. valueName: "country",
  335. disabled: (!editMode && !createMode),
  336. dataList: ComboData.country,
  337. form: formik
  338. })}
  339. </Grid>
  340. <Grid item lg={4} >
  341. {FieldUtils.getComboField({
  342. label: FieldUtils.notNullFieldLabel("District:"),
  343. valueName: "district",
  344. disabled: (!editMode && !createMode),
  345. dataList: ComboData.district,
  346. form: formik
  347. })}
  348. </Grid>
  349. <Grid item lg={4} >
  350. {FieldUtils.getAddressField({
  351. label: FieldUtils.notNullFieldLabel("Address:"),
  352. valueName: ["addressLine1", "addressLine2", "addressLine3"],
  353. disabled: (!editMode && !createMode),
  354. form: formik
  355. })}
  356. </Grid>
  357. <Grid item lg={12} ></Grid>
  358. </Grid>
  359. }
  360. </form>
  361. <div>
  362. <Dialog open={creditorConfirmPopUp} onClose={() => setCreditorConfirmPopUp(false)} >
  363. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  364. <DialogContent style={{ display: 'flex', }}>
  365. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Creditor?</Typography>
  366. </DialogContent>
  367. <DialogActions>
  368. <Button onClick={() => setCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  369. <Button onClick={() => markAsCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  370. </DialogActions>
  371. </Dialog>
  372. </div>
  373. <div>
  374. <Dialog open={nonCreditorConfirmPopUp} onClose={() => setNonCreditorConfirmPopUp(false)} >
  375. <DialogTitle><Typography variant="h3">Confirm</Typography></DialogTitle>
  376. <DialogContent style={{ display: 'flex', }}>
  377. <Typography variant="h4" style={{ padding: '16px' }}>Are you sure mark as Non-Creditor?</Typography>
  378. </DialogContent>
  379. <DialogActions>
  380. <Button onClick={() => setNonCreditorConfirmPopUp(false)}><Typography variant="h5">Cancel</Typography></Button>
  381. <Button onClick={() => markAsNonCreditor()}><Typography variant="h5">Confirm</Typography></Button>
  382. </DialogActions>
  383. </Dialog>
  384. </div>
  385. </MainCard>
  386. );
  387. };
  388. export default OrganizationCard;