Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

221 строка
8.9 KiB

  1. // material-ui
  2. import {
  3. Button,
  4. CardContent,
  5. Grid, TextField,
  6. Autocomplete,
  7. Typography
  8. } from '@mui/material';
  9. import MainCard from "components/MainCard";
  10. import { useForm } from "react-hook-form";
  11. import * as React from "react";
  12. import * as ComboData from "utils/ComboData";
  13. import * as DateUtils from "utils/DateUtils";
  14. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  15. const SearchPublicNoticeForm = ({ applySearch, searchCriteria }) => {
  16. const [type, setType] = React.useState([]);
  17. const [status, setStatus] = React.useState([{ key: 0, label: 'All', labelCht: "全部", type: 'all' }]);
  18. const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
  19. const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
  20. const [selectedLabelsString, setSelectedLabelsString] = React.useState('');
  21. const { reset, register, handleSubmit } = useForm()
  22. const onSubmit = (data) => {
  23. data.status = selectedLabelsString
  24. let typeArray = [];
  25. for (let i = 0; i < type.length; i++) {
  26. typeArray.push(type[i].label);
  27. }
  28. const temp = {
  29. appNo: data.appNo,
  30. dateFrom: data.dateFrom,
  31. dateTo: data.dateTo,
  32. contact: data.contact,
  33. status: (data.status === "" || data.status.includes('all')) ? "" : data.status,
  34. };
  35. applySearch(temp);
  36. };
  37. function resetForm() {
  38. setType([]);
  39. setStatus([{ key: 0, label: 'All', labelCht: "全部", type: 'all' }]);
  40. reset();
  41. }
  42. return (
  43. <MainCard xs={12} md={12} lg={12}
  44. border={false}
  45. content={false}>
  46. <form onSubmit={handleSubmit(onSubmit)}>
  47. {/*row 1*/}
  48. <CardContent sx={{ px: 2.5, pt: 3 }}>
  49. <Grid item justifyContent="space-between" alignItems="center">
  50. <Typography variant="h4">搜尋</Typography>
  51. </Grid>
  52. </CardContent>
  53. {/*row 2*/}
  54. <Grid container alignItems={"center"}>
  55. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  56. <TextField
  57. fullWidth
  58. {...register("appNo")}
  59. id='appNo'
  60. label="申請編號"
  61. defaultValue={searchCriteria.appNo}
  62. InputLabelProps={{
  63. shrink: true
  64. }}
  65. />
  66. </Grid>
  67. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  68. <TextField
  69. fullWidth
  70. {...register("dateFrom")}
  71. id="dateFrom"
  72. type="date"
  73. label="提交日期(從)"
  74. defaultValue={searchCriteria.dateFrom}
  75. InputProps={{ inputProps: { max: maxDate } }}
  76. onChange={(newValue) => {
  77. setMinDate(DateUtils.dateStr(newValue));
  78. }}
  79. InputLabelProps={{
  80. shrink: true
  81. }}
  82. />
  83. </Grid>
  84. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  85. <TextField
  86. fullWidth
  87. InputLabelProps={{
  88. shrink: true
  89. }}
  90. {...register("dateTo")}
  91. id="dateTo"
  92. type="date"
  93. label="提交日期(到)"
  94. defaultValue={searchCriteria.dateTo}
  95. InputProps={{ inputProps: { min: minDate } }}
  96. onChange={(newValue) => {
  97. setMaxDate(DateUtils.dateStr(newValue));
  98. }}
  99. />
  100. </Grid>
  101. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  102. <TextField
  103. fullWidth
  104. {...register("contact")}
  105. id="contact"
  106. label="聯絡人"
  107. defaultValue={searchCriteria.contact}
  108. InputLabelProps={{
  109. shrink: true
  110. }}
  111. />
  112. </Grid>
  113. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  114. <Autocomplete
  115. multiple
  116. {...register("status")}
  117. id="status"
  118. // filterOptions={(options)=>options}
  119. options={
  120. localStorage.getItem('userData').creditor ?
  121. ComboData.publicNoticeStatic_Creditor :
  122. ComboData.publicNoticeStatic
  123. }
  124. value={status}
  125. // inputValue={status?.labelCht}
  126. getOptionLabel={(option) => option.labelCht}
  127. onChange={(event, newValue) => {
  128. console.log(newValue)
  129. const findAllIndex = newValue.findIndex((ele) => {
  130. return ele.type === "all"
  131. })
  132. if (findAllIndex > -1) {
  133. setStatus([newValue[findAllIndex]]);
  134. setSelectedLabelsString('all')
  135. } else {
  136. const selectedLabels = newValue.map(option => option.type);
  137. const selectedLabelsString = `${selectedLabels.join(',')}`;
  138. setStatus(newValue);
  139. console.log(newValue)
  140. setSelectedLabelsString(selectedLabelsString);
  141. }
  142. console.log(selectedLabelsString)
  143. console.log(status)
  144. }}
  145. renderInput={(params) => (
  146. <TextField {...params}
  147. label="狀態"
  148. InputLabelProps={{
  149. shrink: true
  150. }}
  151. />
  152. )}
  153. // InputLabelProps={{
  154. // shrink: true
  155. // }}
  156. />
  157. </Grid>
  158. {/*<Grid item xs={9} s={6} md={5} lg={3} sx={{ml:3, mr:3, mb:3}}>*/}
  159. {/* <TextField*/}
  160. {/* fullWidth*/}
  161. {/* {...register("subDivisionId")}*/}
  162. {/* id="subDivision"*/}
  163. {/* label="Sub-Division"*/}
  164. {/* />*/}
  165. {/*</Grid>*/}
  166. </Grid>
  167. {/*last row*/}
  168. <Grid container maxWidth justifyContent="flex-end">
  169. <Grid item sx={{ ml: 3, mr: 3, mb: 3, mt: 3 }}>
  170. <Button
  171. size="large"
  172. variant="contained"
  173. onClick={resetForm}
  174. sx={{
  175. textTransform: 'capitalize',
  176. alignItems: 'end'
  177. }}>
  178. <Typography variant="h5">重置</Typography>
  179. </Button>
  180. </Grid>
  181. <Grid item sx={{ ml: 3, mr: 3, mb: 3, mt: 3 }}>
  182. <Button
  183. size="large"
  184. variant="contained"
  185. type="submit"
  186. sx={{
  187. textTransform: 'capitalize',
  188. alignItems: 'end'
  189. }}>
  190. <Typography variant="h5">搜尋</Typography>
  191. </Button>
  192. </Grid>
  193. </Grid>
  194. </form>
  195. </MainCard>
  196. );
  197. };
  198. export default SearchPublicNoticeForm;