您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

385 行
16 KiB

  1. // material-ui
  2. import {
  3. Dialog, DialogTitle, DialogContent, DialogActions,
  4. Typography,
  5. Autocomplete,
  6. CardContent,
  7. Grid,
  8. Stack,
  9. TextField,
  10. FormLabel,
  11. Button
  12. } from '@mui/material';
  13. import * as UrlUtils from "utils/ApiPathConst";
  14. import * as HttpUtils from "utils/HttpUtils";
  15. import MainCard from "components/MainCard";
  16. import * as ComboData from "utils/ComboData";
  17. import * as React from "react";
  18. import { useFormik } from 'formik';
  19. import { useNavigate } from "react-router-dom";
  20. import Loadable from 'components/Loadable';
  21. const UploadFileTable = Loadable(React.lazy(() => import('./UploadFileTable')));
  22. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  23. const FormPanel = ({ formData }) => {
  24. const [data, setData] = React.useState({});
  25. const [columnPrice, setColumnPrice] = React.useState(ComboData.proofPrice[0]);
  26. const [attachments, setAttachments] = React.useState([]);
  27. const [wait, setWait] = React.useState(false);
  28. const [isWarningPopUp, setIsWarningPopUp] = React.useState(false);
  29. const [warningText, setWarningText] = React.useState("");
  30. const navigate = useNavigate()
  31. React.useEffect(() => {
  32. if (formData) {
  33. setData(formData);
  34. if (formData.groupType == "A") {
  35. setColumnPrice(ComboData.proofPrice[1])
  36. formData['length'] = 18;
  37. }
  38. }
  39. }, [formData]);
  40. React.useEffect(() => {
  41. if (!attachments || attachments.length <= 0) {
  42. formik.setFieldValue("length", 0);
  43. formik.setFieldValue("noOfPages", 0);
  44. formik.setFieldValue("fee", 0);
  45. return;
  46. }
  47. doCalculate();
  48. }, [attachments]);
  49. const doCalculate = () => {
  50. setWait(true);
  51. if (!attachments || attachments.length <= 0) {
  52. setWarningText("Unable to calculate, please upload a valid document.");
  53. setIsWarningPopUp(true);
  54. setWait(false);
  55. return;
  56. }
  57. HttpUtils.postWithFiles({
  58. url: UrlUtils.PROOF_CHECK_PRICE,
  59. params: {
  60. appId: data.id,
  61. },
  62. files: attachments,
  63. onSuccess: function (responseData) {
  64. if (responseData.data.detail) {
  65. setWarningText("Unable to calculate, please upload a valid document or input manually.");
  66. setIsWarningPopUp(true);
  67. return;
  68. }
  69. formik.setFieldValue("length", responseData.data.length);
  70. let colValue = 0;
  71. setColumnPrice(ComboData.proofPrice.find(obj => {
  72. colValue = obj.value;
  73. return obj.colCount === responseData.data.column
  74. }));
  75. formik.setFieldValue("noOfPages", responseData.data.no_of_page);
  76. formik.setFieldValue("fee", (data.groupType == "A" ? 6552 * responseData.data.no_of_page : responseData.data.length * colValue));
  77. setWait(false);
  78. },
  79. onError: function () {
  80. setWarningText("Unable to calculate, please input manually.");
  81. setIsWarningPopUp(true);
  82. setWait(false);
  83. }
  84. });
  85. }
  86. const formik = useFormik({
  87. enableReinitialize: true,
  88. initialValues: data,
  89. onSubmit: values => {
  90. if (!attachments || attachments.length <= 0) {
  91. setWarningText("Please upload file.");
  92. setIsWarningPopUp(true);
  93. return;
  94. }
  95. // console.log(values);
  96. HttpUtils.postWithFiles({
  97. url: UrlUtils.CREATE_PROOF,
  98. params: {
  99. appId: values.id,
  100. fee: values.fee,
  101. length: values.length,
  102. colCount: columnPrice.colCount,
  103. noOfPages: values.noOfPages,
  104. },
  105. files: attachments,
  106. onSuccess: function () {
  107. navigate("/proof/search");
  108. }
  109. });
  110. }
  111. });
  112. const readFile = (event) => {
  113. let file = event.target.files[0];
  114. if (file) {
  115. if (!file.name.toLowerCase().substr(file.name.length - 4).includes(".pdf")) {
  116. setWarningText("Please upload a valid file (File format: .pdf).");
  117. setIsWarningPopUp(true);
  118. document.getElementById("uploadFileBtn").value = "";
  119. return;
  120. }
  121. if (file.size >= (10 * 1024 * 1034)) {
  122. setWarningText("The file size for uploading should be less than 10MB");
  123. setIsWarningPopUp(true);
  124. return;
  125. }
  126. file['id'] = attachments.length;
  127. setAttachments([
  128. ...attachments,
  129. file
  130. ]);
  131. document.getElementById("uploadFileBtn").value = "";
  132. }
  133. }
  134. return (
  135. <MainCard xs={12} md={12} lg={12}
  136. border={false}
  137. content={false}>
  138. <form onSubmit={formik.handleSubmit}>
  139. {/*row 1*/}
  140. <CardContent sx={{ px: 2.5, pt: 3 }}>
  141. <Grid item justifyContent="space-between" alignItems="center">
  142. Proof
  143. </Grid>
  144. </CardContent>
  145. {/*row 2*/}
  146. <Grid container direction="column" sx={{ paddingLeft: 4, paddingRight: 4 }} spacing={1}>
  147. <Grid item xs={12} md={12}>
  148. <input
  149. id="uploadFileBtn"
  150. name="file"
  151. type="file"
  152. accept=".pdf"
  153. style={{ display: 'none' }}
  154. disabled={attachments.length >= (formik.values.groupType == "A" ? 2 : 1)}
  155. onChange={(event) => {
  156. readFile(event)
  157. }}
  158. />
  159. <label htmlFor="uploadFileBtn">
  160. <Button
  161. component="span"
  162. variant="contained"
  163. size="large"
  164. disabled={attachments.length >= (formik.values.groupType == "A" ? 2 : 1)}
  165. > Upload Files</Button>
  166. </label>
  167. </Grid>
  168. <Grid item xs={12} md={12}>
  169. <UploadFileTable key="uploadTable" recordList={attachments} setRecordList={setAttachments} />
  170. </Grid>
  171. {
  172. wait ?
  173. <Grid item xs={12} md={12}>
  174. Doing calculate, please wait ...
  175. </Grid>
  176. :
  177. <Grid item xs={12} md={12}>
  178. <Button
  179. size="large"
  180. variant="contained"
  181. onClick={doCalculate}
  182. sx={{
  183. textTransform: 'capitalize',
  184. alignItems: 'end'
  185. }}>
  186. Calculate
  187. </Button>
  188. </Grid>
  189. }
  190. {
  191. formik.values.groupType == "A" ?
  192. <Grid item xs={12} md={12}>
  193. <Stack direction="row" sx={{ display: 'flex', alignItems: 'center' }}>
  194. <TextField
  195. fullWidth
  196. size="small"
  197. type="text"
  198. onChange={(event) => {
  199. const value = event.target.value;
  200. formik.setFieldValue("length", value);
  201. formik.setFieldValue("fee", 6552 * value);
  202. }}
  203. name="noOfPages"
  204. value={formik.values["noOfPages"]}
  205. variant="outlined"
  206. sx={
  207. {
  208. "& .MuiInputBase-input.Mui-disabled": {
  209. WebkitTextFillColor: "#000000",
  210. background: "#f8f8f8",
  211. },
  212. width: '15%'
  213. }
  214. }
  215. />
  216. <FormLabel sx={{ paddingLeft: 2, paddingRight: 2, textAlign: "center" }}>
  217. pages
  218. </FormLabel>
  219. <FormLabel sx={{ paddingLeft: 2, paddingRight: 2, textAlign: "center" }}>
  220. x
  221. </FormLabel>
  222. <FormLabel sx={{ paddingLeft: 2, paddingRight: 2, textAlign: "center" }}>
  223. ${formik.values.price ? formik.values.price : "6,552"}
  224. </FormLabel>
  225. </Stack>
  226. </Grid>
  227. :
  228. <Grid item xs={12} md={12}>
  229. <Stack direction="row" sx={{ display: 'flex', alignItems: 'center' }}>
  230. <TextField
  231. fullWidth
  232. size="small"
  233. type="text"
  234. onChange={(event) => {
  235. const value = event.target.value;
  236. formik.setFieldValue("length", value);
  237. formik.setFieldValue("fee", columnPrice.value * value);
  238. }}
  239. name="length"
  240. value={formik.values["length"]}
  241. variant="outlined"
  242. sx={
  243. {
  244. "& .MuiInputBase-input.Mui-disabled": {
  245. WebkitTextFillColor: "#000000",
  246. background: "#f8f8f8",
  247. },
  248. width: '15%'
  249. }
  250. }
  251. />
  252. <FormLabel sx={{ paddingLeft: 2, paddingRight: 2, textAlign: "center" }}>
  253. cm
  254. </FormLabel>
  255. <FormLabel sx={{ paddingLeft: 2, paddingRight: 4, textAlign: "center" }}>
  256. x
  257. </FormLabel>
  258. <Autocomplete
  259. sx={{
  260. width: "15%"
  261. }}
  262. disablePortal
  263. id="price"
  264. options={ComboData.proofPrice}
  265. value={columnPrice}
  266. inputValue={(columnPrice?.label) ? columnPrice?.label : ""}
  267. getOptionLabel={(option) => option.label ? option.label : ""}
  268. onChange={(event, newValue) => {
  269. setColumnPrice(newValue)
  270. formik.values["fee"] = newValue.value * formik.values.length;
  271. }}
  272. renderInput={(params) => (
  273. <TextField {...params}
  274. InputLabelProps={{
  275. shrink: true
  276. }}
  277. sx={{
  278. width: "100%"
  279. }}
  280. />
  281. )}
  282. />
  283. </Stack>
  284. </Grid>
  285. }
  286. <Grid item xs={12} md={12}>
  287. <Stack direction="row" sx={{ display: 'flex', alignItems: 'center' }}>
  288. <FormLabel sx={{ paddingRight: 2, textAlign: "center" }}>
  289. Necessary fee:
  290. </FormLabel>
  291. <FormLabel sx={{ paddingLeft: 2, paddingRight: 2, textAlign: "center" }}>
  292. $
  293. </FormLabel>
  294. <TextField
  295. fullWidth
  296. size="small"
  297. type="text"
  298. onChange={formik.handleChange}
  299. name="fee"
  300. value={formik.values["fee"]}
  301. variant="outlined"
  302. sx={
  303. {
  304. "& .MuiInputBase-input.Mui-disabled": {
  305. WebkitTextFillColor: "#000000",
  306. background: "#f8f8f8",
  307. },
  308. width: '15%'
  309. }
  310. }
  311. />
  312. </Stack>
  313. </Grid>
  314. <Grid item xs={12} md={12}>
  315. <Button
  316. size="large"
  317. variant="contained"
  318. color="success"
  319. type="submit"
  320. sx={{
  321. textTransform: 'capitalize',
  322. alignItems: 'end'
  323. }}>
  324. Save
  325. </Button>
  326. </Grid>
  327. </Grid>
  328. </form>
  329. <div>
  330. <Dialog open={isWarningPopUp} onClose={() => setIsWarningPopUp(false)} >
  331. <DialogTitle>Warning</DialogTitle>
  332. <DialogContent style={{ display: 'flex', }}>
  333. <Typography variant="h3" style={{ padding: '16px' }}>{warningText}</Typography>
  334. </DialogContent>
  335. <DialogActions>
  336. <Button onClick={() => setIsWarningPopUp(false)}>OK</Button>
  337. </DialogActions>
  338. </Dialog>
  339. </div>
  340. </MainCard>
  341. );
  342. };
  343. export default FormPanel;