Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

317 righe
14 KiB

  1. // material-ui
  2. import {
  3. Button,
  4. Grid, TextField,
  5. Autocomplete,
  6. Typography
  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 DateUtils from "utils/DateUtils";
  12. import * as ComboData from "utils/ComboData";
  13. import {PNSPS_BUTTON_THEME} from "../../../themes/buttonConst";
  14. import {ThemeProvider} from "@emotion/react";
  15. import {DatePicker} from "@mui/x-date-pickers/DatePicker";
  16. import dayjs from "dayjs";
  17. import {DemoItem} from "@mui/x-date-pickers/internals/demo";
  18. import {LocalizationProvider} from "@mui/x-date-pickers/LocalizationProvider";
  19. import {AdapterDayjs} from "@mui/x-date-pickers/AdapterDayjs";
  20. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  21. const SearchPublicNoticeForm = ({ applySearch, searchCriteria, onGridReady }) => {
  22. const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
  23. const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
  24. const [status, setStatus] = React.useState(ComboData.paymentStatus[0]);
  25. const [payMethod, setPayMethod] = React.useState(ComboData.payMethod[0]);
  26. const { reset, register, handleSubmit } = useForm()
  27. const marginBottom = 2.5;
  28. const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy");
  29. const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy");
  30. React.useEffect(() => {
  31. if(searchCriteria.status!=undefined){
  32. if(searchCriteria.status === ""){
  33. ComboData.paymentStatus[0]
  34. }else{
  35. setStatus(ComboData.paymentStatus.find(item => item.type === searchCriteria.status))
  36. }
  37. }else{
  38. setStatus(ComboData.paymentStatus[0])
  39. }
  40. }, [searchCriteria]);
  41. React.useEffect(() => {
  42. const defaultPayMethod = ComboData.payMethod[0];
  43. const value = searchCriteria?.payMethod; // may be [], null, undefined, or array of strings
  44. if (!value || value.length === 0) {
  45. setPayMethod(defaultPayMethod);
  46. return;
  47. }
  48. // Find the matching entry whose type array matches value contents
  49. const found = ComboData.payMethod.find(item =>
  50. Array.isArray(item.type) &&
  51. item.type.length === value.length &&
  52. item.type.every((v, i) => v === value[i]) // strict positional match
  53. );
  54. setPayMethod(found ?? defaultPayMethod);
  55. }, [searchCriteria?.payMethod]);
  56. React.useEffect(() => {
  57. setFromDateValue(minDate);
  58. }, [minDate]);
  59. React.useEffect(() => {
  60. setToDateValue(maxDate);
  61. }, [maxDate]);
  62. // add near the top inside the component (after useState for payMethod)
  63. const toPayMethodArray = (opt) => {
  64. if (!opt || opt.type === 'all') return [];
  65. return Array.isArray(opt.type) ? opt.type : [opt.type];
  66. };
  67. const onSubmit = (data) => {
  68. let sentDateFrom = "";
  69. let sentDateTo = "";
  70. if (fromDateValue != "dd / mm / yyyy" && toDateValue != "dd / mm / yyyy") {
  71. sentDateFrom = DateUtils.dateValue(fromDateValue)
  72. sentDateTo = DateUtils.dateValue(toDateValue)
  73. }
  74. const temp = {
  75. code: data.code,
  76. transNo: data.transNo,
  77. dateFrom: sentDateFrom,
  78. dateTo: sentDateTo,
  79. status : (status?.type && status?.type != 'all') ? status?.type : "",
  80. payMethod : toPayMethodArray(payMethod),
  81. start:0,
  82. limit:10
  83. };
  84. applySearch(temp);
  85. };
  86. function resetForm() {
  87. setStatus(ComboData.paymentStatus[0]);
  88. setMinDate(DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)))
  89. setMaxDate(DateUtils.dateValue(new Date()))
  90. reset({
  91. code:"",
  92. transNo:""
  93. });
  94. localStorage.setItem('searchCriteria',"")
  95. }
  96. return (
  97. <MainCard xs={12} md={12} lg={12}
  98. border={false}
  99. content={false}
  100. >
  101. <form onSubmit={handleSubmit(onSubmit)} >
  102. <Grid container sx={{ backgroundColor: '#ffffff', ml: 2, mt: 1, mb: marginBottom}} width="98%">
  103. {/*row 1*/}
  104. <Grid item justifyContent="space-between" alignItems="center" sx={{mt:1,ml:3,mb:2.5}}>
  105. <Typography variant="pnspsFormHeader" >
  106. Search
  107. </Typography>
  108. </Grid>
  109. {/*row 2*/}
  110. <Grid container display="flex" alignItems={"center"}>
  111. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3,mb: marginBottom}}>
  112. <TextField
  113. fullWidth
  114. {...register("code")}
  115. id='code'
  116. label="Application No."
  117. defaultValue={searchCriteria.code}
  118. InputLabelProps={{
  119. shrink: true
  120. }}
  121. />
  122. </Grid>
  123. <Grid item xs={9} s={6} md={5} lg={3} sx={{ml:3, mr:3, mb:marginBottom}}>
  124. <Grid container spacing={1}>
  125. <Grid item xs={6}>
  126. <LocalizationProvider dateAdapter={AdapterDayjs}>
  127. <DemoItem components={['DatePicker']}>
  128. <DatePicker
  129. id="dateFrom"
  130. // onError={(newError) => setReceiptFromError(newError)}
  131. slotProps={{
  132. field: { readOnly: true, },
  133. // textField: {
  134. // helperText: receiptFromErrorMessage,
  135. // },
  136. }}
  137. format="DD/MM/YYYY"
  138. label="Transaction Date (From)"
  139. value={minDate === null ? null : dayjs(minDate)}
  140. maxDate={maxDate === null ? null : dayjs(maxDate)}
  141. onChange={(newValue) => {
  142. // console.log(newValue)
  143. if(newValue!=null){
  144. setMinDate(newValue);
  145. }
  146. }}
  147. />
  148. </DemoItem >
  149. </LocalizationProvider>
  150. </Grid>
  151. <Grid item xs={6}>
  152. <LocalizationProvider dateAdapter={AdapterDayjs}>
  153. <DemoItem components={['DatePicker']}>
  154. <DatePicker
  155. id="dateTo"
  156. // onError={(newError) => setReceiptFromError(newError)}
  157. slotProps={{
  158. field: { readOnly: true, },
  159. // textField: {
  160. // helperText: receiptFromErrorMessage,
  161. // },
  162. }}
  163. format="DD/MM/YYYY"
  164. label="Transaction Date (To)"
  165. value={maxDate === null ? null : dayjs(maxDate)}
  166. minDate={minDate === null ? null : dayjs(minDate)}
  167. onChange={(newValue) => {
  168. // console.log(newValue)
  169. if(newValue!=null){
  170. setMaxDate(newValue);
  171. }
  172. }}
  173. />
  174. </DemoItem >
  175. </LocalizationProvider>
  176. </Grid>
  177. </Grid>
  178. </Grid>
  179. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom}}>
  180. <TextField
  181. fullWidth
  182. {...register("transNo")}
  183. id='transNo'
  184. label="Transaction No."
  185. defaultValue={searchCriteria.transNo}
  186. InputLabelProps={{
  187. shrink: true
  188. }}
  189. />
  190. </Grid>
  191. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  192. <Autocomplete
  193. {...register("status")}
  194. disablePortal={false}
  195. size="small"
  196. id="status"
  197. filterOptions={(options) => options}
  198. options={ComboData.paymentStatus}
  199. value={status}
  200. getOptionLabel={(option) => option.label}
  201. inputValue={status?.label ? status?.label : ""}
  202. onChange={(event, newValue) => {
  203. if(newValue==null){
  204. setStatus(ComboData.paymentStatus[0]);
  205. }else{
  206. setStatus(newValue);
  207. }
  208. }}
  209. sx={{
  210. '& .MuiInputBase-root': { alignItems: 'center' },
  211. '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' },
  212. '& .MuiOutlinedInput-root': { height: 40 }
  213. }}
  214. renderInput={(params) => (
  215. <TextField {...params}
  216. label="Status"
  217. />
  218. )}
  219. InputLabelProps={{
  220. shrink: true
  221. }}
  222. />
  223. </Grid>
  224. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  225. <Autocomplete
  226. {...register("payMethod")}
  227. disablePortal={false}
  228. size="small"
  229. id="payMethod"
  230. filterOptions={(options) => options}
  231. options={ComboData.payMethod}
  232. value={payMethod}
  233. getOptionLabel={(option) => option.label}
  234. inputValue={payMethod?.label ? payMethod?.label : ""}
  235. onChange={(event, newValue) => {
  236. if(newValue==null){
  237. setPayMethod(ComboData.payMethod[0]);
  238. }else{
  239. setPayMethod(newValue);
  240. }
  241. }}
  242. sx={{
  243. '& .MuiInputBase-root': { alignItems: 'center' },
  244. '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' },
  245. '& .MuiOutlinedInput-root': { height: 40 }
  246. }}
  247. renderInput={(params) => (
  248. <TextField {...params}
  249. label="Payment Method"
  250. />
  251. )}
  252. InputLabelProps={{
  253. shrink: true
  254. }}
  255. />
  256. </Grid>
  257. </Grid>
  258. </Grid>
  259. {/*last row*/}
  260. <Grid container maxWidth justifyContent="flex-end">
  261. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  262. <Grid item sx={{ ml: 3, mb: 3}}>
  263. <Button
  264. variant="contained"
  265. color="cancel"
  266. onClick={resetForm}
  267. >
  268. Reset
  269. </Button>
  270. </Grid>
  271. <Grid item sx={{ ml: 3, mr: 3, mb: 3 }}>
  272. <Button
  273. variant="contained"
  274. type="submit"
  275. disabled={onGridReady}
  276. >
  277. Submit
  278. </Button>
  279. </Grid>
  280. </ThemeProvider>
  281. </Grid>
  282. </form>
  283. </MainCard>
  284. );
  285. };
  286. export default SearchPublicNoticeForm;