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.
 
 

217 lines
8.6 KiB

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