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.
 
 

260 lines
12 KiB

  1. // material-ui
  2. import {
  3. Grid,
  4. Typography,
  5. Button,
  6. RadioGroup,
  7. Dialog, DialogTitle, DialogContent, DialogActions,
  8. Stack, Box
  9. } from '@mui/material';
  10. import { useFormik } from 'formik';
  11. import * as yup from 'yup';
  12. import * as React from "react";
  13. import * as HttpUtils from "utils/HttpUtils";
  14. import * as UrlUtils from "utils/ApiPathConst";
  15. import * as FieldUtils from "utils/FieldUtils";
  16. import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'
  17. import { useNavigate } from "react-router-dom";
  18. import { notifyActionSuccess } from 'utils/CommonFunction';
  19. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  20. const PublicNoticeApplyForm = ({ loadedData, selections }) => {
  21. const [isWarningPopUp, setIsWarningPopUp] = React.useState(false);
  22. const [warningText, setWarningText] = React.useState("");
  23. const [attachment, setAttachment] = React.useState({});
  24. const [issueId, setIssueId] = React.useState(loadedData.issueId);
  25. const navigate = useNavigate();
  26. const BackgroundHead = {
  27. backgroundImage: `url(${titleBackgroundImg})`,
  28. width: 'auto',
  29. height: 'auto',
  30. backgroundSize: 'contain',
  31. backgroundRepeat: 'no-repeat',
  32. backgroundColor: '#0C489E',
  33. backgroundPosition: 'right'
  34. }
  35. // React.useEffect(()=>{
  36. // setFormData(loadedData);
  37. // },[]);
  38. const formik = useFormik({
  39. enableReinitialize: true,
  40. initialValues: loadedData,
  41. validationSchema: yup.object().shape({
  42. contactPerson: yup.string().max(40, "不得超過 40 個字符").required('請輸入聯絡人'),
  43. tel_countryCode: yup.string().min(3, '請輸入3位數字').required('請輸入國際區號'),
  44. fax_countryCode: yup.string().min(3, '請輸入3位數字'),
  45. phoneNumber: yup.string().min(8, '請輸入8位數字').required('請輸入聯絡電話'),
  46. faxNumber: yup.string().min(8, '請輸入8位數字'),
  47. remarks: yup.string().max(255, "不得超過 255 個字符").nullable(),
  48. }),
  49. onSubmit: values => {
  50. if (!values.issueId) {
  51. setWarningText("請選擇目標期數");
  52. setIsWarningPopUp(true);
  53. return;
  54. }
  55. if (!attachment) {
  56. setWarningText("請選擇上傳檔案");
  57. setIsWarningPopUp(true);
  58. return;
  59. } else if (!attachment.size || attachment.size <= 0) {
  60. setWarningText("請上傳有效檔案");
  61. setIsWarningPopUp(true);
  62. return;
  63. } else if (attachment.size >= (10 * 1024 * 1034)) {
  64. setWarningText("上傳檔案大小應<10MB");
  65. setIsWarningPopUp(true);
  66. return;
  67. }
  68. // console.log(values);
  69. HttpUtils.postWithFiles({
  70. url: UrlUtils.POST_PUBLIC_NOTICE_APPLY,
  71. params: {
  72. id: 0,
  73. contactPerson: values.contactPerson,
  74. contactTelNo: {
  75. countryCode: values.tel_countryCode,
  76. phoneNumber: values.phoneNumber
  77. },
  78. contactFaxNo: {
  79. countryCode: values.fax_countryCode,
  80. faxNumber: values.faxNumber
  81. },
  82. issueId: issueId,
  83. remarks: values.remarks ? values.remarks : "",
  84. },
  85. files: [attachment],
  86. onSuccess: function () {
  87. notifyActionSuccess('申請成功!')
  88. navigate("/publicNotice");
  89. // location.reload();
  90. }
  91. });
  92. }
  93. });
  94. const readFile = (event) => {
  95. let file = event.target.files[0];
  96. if (file) {
  97. if (file.name.toLowerCase().substr(file.name.length - 4).includes(".doc")
  98. || file.name.toLowerCase().substr(file.name.length - 5).includes(".docx")
  99. || file.name.toLowerCase().substr(file.name.length - 4).includes(".xls")
  100. || file.name.toLowerCase().substr(file.name.length - 5).includes(".xlsx")
  101. ) {
  102. setAttachment(event.target.files[0]);
  103. } else {
  104. setWarningText("請上傳有效檔案 (檔案格式: .doc, .docx, .xls, .xlsx)");
  105. setIsWarningPopUp(true);
  106. setAttachment({});
  107. document.getElementById("uploadFileBtn").value = "";
  108. return;
  109. }
  110. }
  111. }
  112. return (
  113. <Grid container sx={{ minHeight: '95vh', backgroundColor: '#ffffff' }} direction="column" alignItems="center">
  114. <Grid item xs={12} md={12} width="100%" >
  115. <div style={BackgroundHead}>
  116. <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
  117. <Typography ml={15} color='#FFF' variant="h4">申請公共啟事</Typography>
  118. </Stack>
  119. </div>
  120. </Grid>
  121. {/* <Grid item xs={12}>
  122. <Typography variant="h5">申請公共啟事</Typography>
  123. </Grid> */}
  124. <Grid item xs={12} md={12} width={{ md: "60%", xs: "90%" }}>
  125. <Box xs={12} mt={1} sx={{ p: 2, border: '3px groove grey', borderRadius: '10px' }}>
  126. <form onSubmit={formik.handleSubmit}>
  127. <Grid container spacing={1} sx={{ minHeight: '80vh' }} direction="row" justifyContent="flex-start" alignItems="center">
  128. <Grid item xs={12} md={12} lg={12} sx={{ mb: 1 }}>
  129. {FieldUtils.getTextField({
  130. label: "聯絡人:",
  131. valueName: "contactPerson",
  132. form: formik,
  133. disabled: true
  134. })}
  135. </Grid>
  136. <Grid item xs={12} md={12}>
  137. {FieldUtils.getPhoneField({
  138. label: "聯繫電話:",
  139. disabled: true,
  140. valueName: {
  141. code: "tel_countryCode",
  142. num: "phoneNumber",
  143. },
  144. form: formik
  145. })}
  146. </Grid>
  147. <Grid item xs={12} md={12}>
  148. {FieldUtils.getPhoneField({
  149. label: "聯繫傳真:",
  150. disabled: true,
  151. valueName: {
  152. code: "fax_countryCode",
  153. num: "faxNumber",
  154. },
  155. form: formik
  156. })}
  157. </Grid>
  158. <Grid item xs={12} lg={12}>
  159. <Grid container alignItems={"center"}>
  160. <Grid item xs={12} md={3} lg={3}
  161. sx={{ display: 'flex', alignItems: 'center' }}>
  162. 目標期數:
  163. </Grid>
  164. <Grid item xs={12} md={6} lg={6}>
  165. <RadioGroup
  166. aria-labelledby="demo-radio-buttons-group-label"
  167. id="issueId"
  168. name="issueId"
  169. defaultValue={issueId}
  170. onChange={(event)=>{
  171. setIssueId(event.target.value);
  172. }}
  173. >
  174. {
  175. selections
  176. }
  177. </RadioGroup>
  178. </Grid>
  179. </Grid>
  180. </Grid>
  181. <Grid item xs={12} md={12} lg={12}>
  182. <Grid container direction="row" justifyContent="flex-start" alignItems="center">
  183. <Grid item xs={12} md={3} lg={3}
  184. sx={{ display: 'flex', alignItems: 'center' }}>
  185. 稿件檔案 ({"檔案大小應<10MB"}):
  186. </Grid>
  187. <Grid item xs={12} md={3} lg={3}>
  188. <input
  189. id="uploadFileBtn"
  190. name="file"
  191. type="file"
  192. accept=".doc,.docx,.xls,.xlsx"
  193. style={{ display: 'none' }}
  194. onChange={(event) => {
  195. readFile(event)
  196. }}
  197. />
  198. {attachment.name}
  199. </Grid>
  200. <Grid item xs={12} md={3} lg={3}>
  201. <label htmlFor="uploadFileBtn">
  202. <Button
  203. component="span"
  204. variant="outlined"
  205. size="large"
  206. >{attachment ? "上傳檔案" : "重新上傳"}</Button>
  207. </label>
  208. </Grid>
  209. </Grid>
  210. </Grid>
  211. <Grid item xs={12} md={12} lg={12}>
  212. {FieldUtils.getTextArea({
  213. label: "備註:",
  214. valueName: "remarks",
  215. form: formik,
  216. inputProps: { maxLength: 255 }
  217. })}
  218. </Grid>
  219. <Grid item xs={12}>
  220. <center>
  221. <Button
  222. variant="contained"
  223. type="submit"
  224. size="large"
  225. >申請公共啟事</Button>
  226. </center>
  227. </Grid>
  228. </Grid>
  229. </form>
  230. </Box>
  231. </Grid>
  232. <div>
  233. <Dialog open={isWarningPopUp} onClose={() => setIsWarningPopUp(false)} >
  234. <DialogTitle>注意</DialogTitle>
  235. <DialogContent style={{ display: 'flex', }}>
  236. <Typography variant="h3" style={{ padding: '16px' }}>{warningText}</Typography>
  237. </DialogContent>
  238. <DialogActions>
  239. <Button onClick={() => setIsWarningPopUp(false)}>OK</Button>
  240. </DialogActions>
  241. </Dialog>
  242. </div>
  243. </Grid>
  244. );
  245. };
  246. export default PublicNoticeApplyForm;