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.
 
 

213 lines
8.3 KiB

  1. // material-ui
  2. import {
  3. Grid,
  4. Typography,
  5. Button,
  6. RadioGroup,
  7. Dialog, DialogTitle, DialogContent, DialogActions
  8. } from '@mui/material';
  9. import { useFormik } from 'formik';
  10. import * as yup from 'yup';
  11. import * as React 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 {useNavigate} from "react-router-dom";
  16. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  17. const PublicNoticeApplyForm = ({loadedData, selections}) => {
  18. const [isWarningPopUp, setIsWarningPopUp] = React.useState(false);
  19. const [warningText, setWarningText] = React.useState("");
  20. const [attachment, setAttachment] = React.useState({});
  21. const navigate=useNavigate();
  22. // React.useEffect(()=>{
  23. // setFormData(loadedData);
  24. // },[]);
  25. const formik = useFormik({
  26. enableReinitialize:true,
  27. initialValues:loadedData,
  28. validationSchema:yup.object().shape({
  29. contactPerson: yup.string().max(40).required('請輸入聯絡人'),
  30. tel_countryCode: yup.string().min(3,'請輸入3位數字').required('請輸入國際區號'),
  31. fax_countryCode: yup.string().min(3,'請輸入3位數字'),
  32. phoneNumber: yup.string().min(8,'請輸入8位數字').required('請輸入聯絡電話'),
  33. faxNumber: yup.string().min(8,'請輸入8位數字'),
  34. remarks: yup.string().max(255).nullable(),
  35. }),
  36. onSubmit:values=>{
  37. if(!values.issueId){
  38. setWarningText("請選擇目標期數");
  39. setIsWarningPopUp(true);
  40. return;
  41. }
  42. if(!attachment){
  43. setWarningText("請選擇上傳文件");
  44. setIsWarningPopUp(true);
  45. return;
  46. }else if(attachment.size >= (10*1024*1034)){
  47. setWarningText("上傳文件大小應<10MB");
  48. setIsWarningPopUp(true);
  49. return;
  50. }
  51. console.log(values);
  52. HttpUtils.postWithFiles({
  53. url: UrlUtils.POST_PUBLIC_NOTICE_APPLY,
  54. params: {
  55. id: 0,
  56. contactPerson: values.contactPerson,
  57. contactTelNo: {
  58. countryCode: values.tel_countryCode,
  59. phoneNumber: values.phoneNumber
  60. },
  61. contactFaxNo:{
  62. countryCode: values.fax_countryCode,
  63. faxNumber: values.faxNumber
  64. },
  65. issueId:values.issueId,
  66. remarks:values.remarks,
  67. },
  68. files: [attachment],
  69. onSuccess: function(){
  70. navigate("/publicNotice");
  71. }
  72. });
  73. }
  74. });
  75. const readFile=(event)=>{
  76. setAttachment(event.target.files[0]);
  77. }
  78. return (
  79. <Grid container style={{ padding: 24}} rowSpacing={16} columnSpacing={2.75}>
  80. <Grid item xs={12}>
  81. <Typography variant="h5">申請公共啟事</Typography>
  82. </Grid>
  83. <form style={{ width: "100%"}} onSubmit={formik.handleSubmit} >
  84. <Grid container spacing={1}>
  85. <Grid item lg={4}></Grid>
  86. {FieldUtils.getTextField({
  87. label:"聯絡人:",
  88. valueName:"contactPerson",
  89. form: formik
  90. })}
  91. <Grid item lg={4}></Grid>
  92. <Grid item lg={4}></Grid>
  93. {FieldUtils.getPhoneField({
  94. label:"聯繫電話:",
  95. valueName:{
  96. code: "tel_countryCode",
  97. num:"phoneNumber"
  98. },
  99. form: formik
  100. })}
  101. <Grid item lg={4}></Grid>
  102. <Grid item lg={4}></Grid>
  103. {FieldUtils.getPhoneField({
  104. label:"聯繫傳真:",
  105. valueName:{
  106. code: "fax_countryCode",
  107. num:"faxNumber"
  108. },
  109. form: formik
  110. })}
  111. <Grid item lg={4}></Grid>
  112. <Grid item lg={4}></Grid>
  113. <Grid item lg={4}>
  114. <Grid container alignItems={"center"}>
  115. <Grid item lg={4}
  116. sx={{ml: 3, mr: 3, display: 'flex', alignItems: 'center'}}>
  117. 目標期數:
  118. </Grid>
  119. <Grid item lg={6}>
  120. <RadioGroup
  121. aria-labelledby="demo-radio-buttons-group-label"
  122. id="issueId"
  123. name="issueId"
  124. defaultValue={formik.values.issueId}
  125. >
  126. {
  127. selections
  128. }
  129. </RadioGroup>
  130. </Grid>
  131. </Grid>
  132. </Grid>
  133. <Grid item lg={4}></Grid>
  134. <Grid item lg={4}></Grid>
  135. <Grid item lg={4}>
  136. <Grid container alignItems={"center"}>
  137. <Grid item lg={4}
  138. sx={{ml: 3, mr: 3, display: 'flex', alignItems: 'center'}}>
  139. 上傳文件 ({"檔案大小應<10MB"}):
  140. </Grid>
  141. <Grid item lg={4}>
  142. {attachment.name}
  143. </Grid>
  144. <Grid item lg={3}>
  145. <input
  146. id="uploadFileBtn"
  147. name="file"
  148. type="file"
  149. accept="image/png, image/jpeg.doc,.pdf,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  150. style={{ display: 'none' }}
  151. onChange={(event)=> {
  152. readFile(event)
  153. }}
  154. />
  155. <label htmlFor="uploadFileBtn">
  156. <Button
  157. component="span"
  158. variant="outlined"
  159. size="large"
  160. >{attachment?"上傳文件":"重新上傳"}</Button>
  161. </label>
  162. </Grid>
  163. </Grid>
  164. </Grid>
  165. <Grid item lg={4}></Grid>
  166. <Grid item lg={4}></Grid>
  167. {FieldUtils.getTextArea({
  168. label:"備註:",
  169. valueName:"remarks",
  170. form: formik
  171. })}
  172. <Grid item lg={4}></Grid>
  173. <Grid item xs={12}>
  174. <center>
  175. <Button
  176. variant="contained"
  177. type="submit"
  178. size="large"
  179. >申請公共啟事</Button>
  180. </center>
  181. </Grid>
  182. </Grid>
  183. </form>
  184. <div>
  185. <Dialog open={isWarningPopUp} onClose={() => setIsWarningPopUp(false)} >
  186. <DialogTitle>Warning</DialogTitle>
  187. <DialogContent style={{ display: 'flex', }}>
  188. <Typography variant="h3" style={{ padding: '16px' }}>{warningText}</Typography>
  189. </DialogContent>
  190. <DialogActions>
  191. <Button onClick={() => setIsWarningPopUp(false)}>OK</Button>
  192. </DialogActions>
  193. </Dialog>
  194. </div>
  195. </Grid>
  196. );
  197. };
  198. export default PublicNoticeApplyForm;