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.

244 lines
9.1 KiB

  1. // material-ui
  2. import {
  3. Button,
  4. Grid, TextField,
  5. Typography
  6. } from '@mui/material';
  7. import MainCard from "components/MainCard";
  8. import { useForm } from "react-hook-form";
  9. import * as React from "react";
  10. import * as DateUtils from "utils/DateUtils";
  11. import {ThemeProvider} from "@emotion/react";
  12. import { useNavigate } from "react-router-dom";
  13. import {PNSPS_BUTTON_THEME} from "../../../themes/buttonConst";
  14. import { makeStyles } from '@mui/styles';
  15. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  16. const useStyles = makeStyles(() => ({
  17. root: {
  18. position: "relative"
  19. },
  20. display: {
  21. position: "absolute",
  22. top: 2,
  23. left: 12,
  24. bottom: 2,
  25. background: "white",
  26. pointerEvents: "none",
  27. right: 50,
  28. display: "flex",
  29. alignItems: "center"
  30. },
  31. }));
  32. const SearchPublicNoticeForm = ({ applySearch, searchCriteria}) => {
  33. const navigate = useNavigate()
  34. const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
  35. const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
  36. const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy");
  37. const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy");
  38. React.useEffect(() => {
  39. // console.log(minDate)
  40. setFromDateValue(minDate);
  41. }, [minDate]);
  42. React.useEffect(() => {
  43. setToDateValue(maxDate);
  44. }, [maxDate]);
  45. function FormDateInputComponent({inputRef, ...props }) {
  46. const classes = useStyles();
  47. return (
  48. <>
  49. <div className={classes.display}>
  50. {DateUtils.dateStr(fromDateValue)=="Invalid Date"?
  51. fromDateValue
  52. :
  53. DateUtils.dateStr(fromDateValue)}
  54. </div>
  55. <input
  56. // className={classes.input}
  57. ref={inputRef}
  58. {...props}
  59. // onChange={handleChange}
  60. value={fromDateValue}
  61. max= {maxDate}
  62. />
  63. </>
  64. );
  65. }
  66. function ToDateInputComponent({inputRef, ...props }) {
  67. const classes = useStyles();
  68. return (
  69. <>
  70. <div className={classes.display}>
  71. {DateUtils.dateStr(toDateValue)=="Invalid Date"?
  72. toDateValue
  73. :
  74. DateUtils.dateStr(toDateValue)}
  75. </div>
  76. <input
  77. // className={classes.input}
  78. ref={inputRef}
  79. {...props}
  80. // onChange={handleChange}
  81. value={toDateValue}
  82. min = {minDate}
  83. />
  84. </>
  85. );
  86. }
  87. const marginBottom = 2.5;
  88. const { reset, register, handleSubmit } = useForm()
  89. const onSubmit = (data) => {
  90. let sentDateFrom = "";
  91. let sentDateTo = "";
  92. if( fromDateValue!="dd / mm / yyyy"&&toDateValue!="dd / mm / yyyy"){
  93. sentDateFrom = DateUtils.dateValue(fromDateValue)
  94. sentDateTo = DateUtils.dateValue(toDateValue)
  95. }
  96. const temp = {
  97. key: data.key,
  98. dateFrom: sentDateFrom,
  99. dateTo: sentDateTo,
  100. };
  101. applySearch(temp);
  102. };
  103. function resetForm() {
  104. setMinDate(DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)))
  105. setMaxDate(DateUtils.dateValue(new Date()))
  106. reset();
  107. }
  108. return (
  109. <MainCard xs={12} md={12} lg={12}
  110. border={false}
  111. content={false}
  112. sx={{ backgroundColor: '#fff' }}
  113. >
  114. <form onSubmit={handleSubmit(onSubmit)}>
  115. <Grid container sx={{ backgroundColor: '#ffffff', ml: 2, mt: 1, mb: marginBottom}} width="98%">
  116. {/*row 1*/}
  117. <Grid item justifyContent="space-between" alignItems="center" sx={{mt:1,ml:3,mb:marginBottom}}>
  118. <Typography variant="pnspsFormHeader" >
  119. Search
  120. </Typography>
  121. </Grid>
  122. {/*row 2*/}
  123. <Grid container display="flex" alignItems={"center"}>
  124. <Grid item xs={12} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  125. <TextField
  126. fullWidth
  127. {...register("key")}
  128. id='key'
  129. label={"Key"}
  130. defaultValue={searchCriteria.key}
  131. InputLabelProps={{
  132. shrink: true
  133. }}
  134. />
  135. </Grid>
  136. <Grid item xs={12} s={6} md={5} lg={3} sx={{ml:3, mr:3, mb:marginBottom}}>
  137. <Grid container spacing={1}>
  138. <Grid item xs={6}>
  139. <TextField
  140. fullWidth
  141. {...register("dateFrom")}
  142. id="dateFrom"
  143. type="date"
  144. label={"Submit Date (From)"}
  145. defaultValue={searchCriteria.dateFrom}
  146. InputProps={{
  147. inputComponent: FormDateInputComponent,
  148. }}
  149. onChange={(newValue) => {
  150. if(newValue.target.value!=""){
  151. setMinDate(newValue.target.value);
  152. }
  153. }}
  154. InputLabelProps={{
  155. shrink: true
  156. }}
  157. sx={{ "& .MuiInputBase-input": {display:"block", textIndent: "-9999px"} }}
  158. />
  159. </Grid>
  160. <Grid item xs={6}>
  161. <TextField
  162. fullWidth
  163. InputLabelProps={{
  164. shrink: true
  165. }}
  166. {...register("dateTo")}
  167. InputProps={{
  168. inputComponent: ToDateInputComponent,
  169. }}
  170. onChange={(newValue) => {
  171. if(newValue.target.value!=""){
  172. setMaxDate(newValue.target.value);
  173. }
  174. }}
  175. id="dateTo"
  176. type="date"
  177. label={"Submit Date (To)"}
  178. defaultValue={searchCriteria.dateTo}
  179. sx={{ "& .MuiInputBase-input": {display:"block", textIndent: "-9999px"} }}
  180. />
  181. </Grid>
  182. </Grid>
  183. </Grid>
  184. </Grid>
  185. {/*last row*/}
  186. <Grid container maxWidth justifyContent="flex-end">
  187. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  188. <Grid item sx={{ ml: 3 }}>
  189. <Button
  190. variant="contained"
  191. color="green"
  192. onClick={()=>{
  193. navigate('/setting/announcement/details/' + 0);
  194. }}
  195. >
  196. Create
  197. </Button>
  198. </Grid>
  199. <Grid item sx={{ ml: 3 }}>
  200. <Button
  201. variant="contained"
  202. color="cancel"
  203. onClick={resetForm}
  204. >
  205. Reset
  206. </Button>
  207. </Grid>
  208. <Grid item sx={{ ml: 3 }}>
  209. <Button
  210. variant="contained"
  211. type="submit"
  212. >
  213. Submit
  214. </Button>
  215. </Grid>
  216. </ThemeProvider>
  217. </Grid>
  218. </Grid>
  219. </form>
  220. </MainCard>
  221. );
  222. };
  223. export default SearchPublicNoticeForm;