Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

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