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.
 
 

385 lines
18 KiB

  1. // material-uistatus
  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 ComboData from "utils/ComboData";
  12. import * as DateUtils from "utils/DateUtils";
  13. import {
  14. isORGLoggedIn,
  15. } from "utils/Utils";
  16. import {PNSPS_BUTTON_THEME} from "../../../themes/buttonConst";
  17. import {ThemeProvider} from "@emotion/react";
  18. import {FormattedMessage, useIntl} from "react-intl";
  19. import {DatePicker} from "@mui/x-date-pickers/DatePicker";
  20. import dayjs from "dayjs";
  21. import {DemoItem} from "@mui/x-date-pickers/internals/demo";
  22. import {LocalizationProvider} from "@mui/x-date-pickers/LocalizationProvider";
  23. import {AdapterDayjs} from "@mui/x-date-pickers/AdapterDayjs";
  24. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  25. const SearchPublicNoticeForm = ({ applySearch, searchCriteria, onGridReady }) => {
  26. const intl = useIntl();
  27. const [type, setType] = React.useState([]);
  28. const [status, setStatus] = React.useState(localStorage.getItem('userData').creditor?ComboData.publicNoticeStatic_Creditor[0]:ComboData.publicNoticeStatic[0]);
  29. const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
  30. const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
  31. const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy");
  32. const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy");
  33. // const [selectedLabelsString, setSelectedLabelsString] = React.useState('');
  34. const { reset, register, handleSubmit } = useForm()
  35. const marginBottom = 2.5;
  36. React.useEffect(() => {
  37. if(searchCriteria.status!=undefined){
  38. if(localStorage.getItem('userData').creditor){
  39. if(searchCriteria.status === ""){
  40. ComboData.publicNoticeStatic_Creditor[0]
  41. }else{
  42. setStatus(ComboData.publicNoticeStatic_Creditor.find(item => item.type === searchCriteria.status))
  43. }
  44. }else{
  45. if(searchCriteria.status === ""){
  46. ComboData.publicNoticeStatic[0]
  47. }else{
  48. setStatus(ComboData.publicNoticeStatic.find(item => item.type === searchCriteria.status))
  49. }
  50. }
  51. }else{
  52. if(localStorage.getItem('userData').creditor){
  53. setStatus(ComboData.publicNoticeStatic_Creditor[0])
  54. }else{
  55. setStatus(ComboData.publicNoticeStatic[0])
  56. }
  57. }
  58. }, [searchCriteria]);
  59. React.useEffect(() => {
  60. setFromDateValue(minDate);
  61. }, [minDate]);
  62. React.useEffect(() => {
  63. setToDateValue(maxDate);
  64. }, [maxDate]);
  65. const onSubmit = (data) => {
  66. data.status = status.type;
  67. let typeArray = [];
  68. let sentDateFrom = "";
  69. let sentDateTo = "";
  70. for (let i = 0; i < type.length; i++) {
  71. typeArray.push(type[i].label);
  72. }
  73. if (fromDateValue != "dd / mm / yyyy" && toDateValue != "dd / mm / yyyy") {
  74. sentDateFrom = DateUtils.dateValue(fromDateValue)
  75. sentDateTo = DateUtils.dateValue(toDateValue)
  76. }
  77. const temp = {
  78. appNo: data.appNo,
  79. dateFrom: sentDateFrom,
  80. dateTo: sentDateTo,
  81. contact: data.contact,
  82. careOf: data.careOf?data.careOf:"",
  83. status: (data.status === "" || data.status.includes('all')) ? "" : data.status,
  84. start:0,
  85. limit:10
  86. };
  87. if (searchCriteria?.sort && searchCriteria?.direction) {
  88. temp.sort = searchCriteria.sort;
  89. temp.direction = searchCriteria.direction;
  90. }
  91. applySearch(temp);
  92. };
  93. function resetForm() {
  94. setType([]);
  95. setStatus(localStorage.getItem('userData').creditor?ComboData.publicNoticeStatic_Creditor[0]:ComboData.publicNoticeStatic[0]);
  96. const dateFrom = DateUtils.dateValue(new Date().setDate(new Date().getDate() - 14));
  97. const dateTo = DateUtils.dateValue(new Date());
  98. setMinDate(dateFrom);
  99. setMaxDate(dateTo);
  100. reset({
  101. appNo: "",
  102. careOf: "",
  103. contact: ""
  104. });
  105. localStorage.setItem('searchCriteria', "");
  106. // Reset form criteria and sorting first, then reload with backend default sort
  107. applySearch({
  108. appNo: "",
  109. dateFrom,
  110. dateTo,
  111. contact: "",
  112. careOf: "",
  113. status: "",
  114. start: 0,
  115. limit: 10
  116. });
  117. }
  118. return (
  119. <MainCard xs={12} md={12} lg={12}
  120. border={false}
  121. content={false}>
  122. <form onSubmit={handleSubmit(onSubmit)}>
  123. {/*row 1*/}
  124. <Grid item justifyContent="space-between" alignItems="center" sx={{mt:1,ml:3,mb:marginBottom}}>
  125. <Typography variant="pnspsFormHeader" >
  126. <FormattedMessage id="searchForm"/>
  127. </Typography>
  128. </Grid>
  129. {/*row 2*/}
  130. <Grid container alignItems={"center"}>
  131. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  132. <TextField
  133. fullWidth
  134. {...register("appNo")}
  135. id='appNo'
  136. aria-label={intl.formatMessage({id: 'applicationId'})}
  137. label={intl.formatMessage({id: 'applicationId'})}
  138. defaultValue={searchCriteria.appNo}
  139. InputLabelProps={{
  140. shrink: true
  141. }}
  142. />
  143. </Grid>
  144. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  145. <LocalizationProvider dateAdapter={AdapterDayjs} localeText={DateUtils.getPickerLocaleText(intl.locale)}>
  146. <DemoItem components={['DatePicker']}>
  147. <DatePicker
  148. id="dateFrom"
  149. // onError={(newError) => setReceiptFromError(newError)}
  150. slotProps={{
  151. field: { readOnly: true, },
  152. // textField: {
  153. // helperText: receiptFromErrorMessage,
  154. // },
  155. }}
  156. format="DD/MM/YYYY"
  157. aria-label={intl.formatMessage({id: 'submitDateFrom'})}
  158. label={intl.formatMessage({id: 'submitDateFrom'})}
  159. value={minDate === null ? null : dayjs(minDate)}
  160. maxDate={maxDate === null ? null : dayjs(maxDate)}
  161. onChange={(newValue) => {
  162. // console.log(newValue)
  163. if(newValue!=null){
  164. setMinDate(newValue);
  165. }
  166. }}
  167. />
  168. </DemoItem >
  169. </LocalizationProvider>
  170. </Grid>
  171. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  172. <LocalizationProvider dateAdapter={AdapterDayjs} localeText={DateUtils.getPickerLocaleText(intl.locale)}>
  173. <DemoItem components={['DatePicker']}>
  174. <DatePicker
  175. id="dateTo"
  176. // onError={(newError) => setReceiptFromError(newError)}
  177. slotProps={{
  178. field: { readOnly: true, },
  179. // textField: {
  180. // helperText: receiptFromErrorMessage,
  181. // },
  182. }}
  183. format="DD/MM/YYYY"
  184. label={intl.formatMessage({id: 'submitDateTo'})}
  185. value={maxDate === null ? null : dayjs(maxDate)}
  186. minDate={minDate === null ? null : dayjs(minDate)}
  187. onChange={(newValue) => {
  188. // console.log(newValue)
  189. if(newValue!=null){
  190. setMaxDate(newValue);
  191. }
  192. }}
  193. />
  194. </DemoItem >
  195. </LocalizationProvider>
  196. </Grid>
  197. {isORGLoggedIn()?
  198. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  199. <TextField
  200. fullWidth
  201. {...register("careOf")}
  202. id="careOf"
  203. label={intl.formatMessage({id: 'careOf'})}
  204. aria-label={intl.formatMessage({id: 'careOf'})}
  205. defaultValue={searchCriteria.careOf}
  206. InputLabelProps={{
  207. shrink: true
  208. }}
  209. />
  210. </Grid>:null
  211. }
  212. {/* <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  213. <TextField
  214. fullWidth
  215. {...register("contact")}
  216. id="contact"
  217. label={intl.formatMessage({id: 'contactPerson'})}
  218. aria-label={intl.formatMessage({id: 'contactPerson'})}
  219. defaultValue={searchCriteria.contact}
  220. InputLabelProps={{
  221. shrink: true
  222. }}
  223. />
  224. </Grid> */}
  225. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  226. <Autocomplete
  227. {...register("status")}
  228. id="status"
  229. size="small"
  230. disableClearable
  231. // filterOptions={(options)=>options}
  232. options={
  233. localStorage.getItem('userData').creditor ?
  234. ComboData.publicNoticeStatic_Creditor :
  235. ComboData.publicNoticeStatic
  236. }
  237. value={status}
  238. // inputValue={status?.labelCht}
  239. getOptionLabel={(option) => {
  240. if (option == null || option.label == null) return "";
  241. const s = intl.formatMessage({ id: option.label });
  242. return typeof s === "string" ? s : String(s ?? "");
  243. }}
  244. onChange={(event, newValue) => {
  245. if(newValue ==null){
  246. setStatus(localStorage.getItem('userData').creditor?ComboData.publicNoticeStatic_Creditor[0]:ComboData.publicNoticeStatic[0]);
  247. }else{
  248. setStatus(newValue);
  249. }
  250. }}
  251. sx={{
  252. '& .MuiInputBase-root': { alignItems: 'center' },
  253. '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' },
  254. '& .MuiOutlinedInput-root': { height: 40 }
  255. }}
  256. renderInput={(params) => (
  257. <TextField {...params}
  258. label={intl.formatMessage({id: 'status'})}
  259. aria-label={intl.formatMessage({id: 'status'})}
  260. InputLabelProps={{
  261. shrink: true
  262. }}
  263. />
  264. )}
  265. clearText={intl.formatMessage({ id: "muiClear" })}
  266. closeText={intl.formatMessage({ id: "muiClose" })}
  267. openText={intl.formatMessage({ id: "muiOpen" })}
  268. noOptionsText={intl.formatMessage({ id: "muiNoOptions" })}
  269. // InputLabelProps={{
  270. // shrink: true
  271. // }}
  272. />
  273. </Grid>
  274. {/* <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  275. <Autocomplete
  276. multiple
  277. {...register("status")}
  278. id="status"
  279. size="small"
  280. // filterOptions={(options)=>options}
  281. options={
  282. localStorage.getItem('userData').creditor ?
  283. ComboData.publicNoticeStatic_Creditor :
  284. ComboData.publicNoticeStatic
  285. }
  286. value={status}
  287. // inputValue={status?.labelCht}
  288. getOptionLabel={(option) => {
  289. if (option == null || option.label == null) return "";
  290. const s = intl.formatMessage({ id: option.label });
  291. return typeof s === "string" ? s : String(s ?? "");
  292. }}
  293. onChange={(event, newValue) => {
  294. console.log(newValue)
  295. const findAllIndex = newValue.findIndex((ele) => {
  296. return ele.type === "all"
  297. })
  298. if (findAllIndex > -1) {
  299. setStatus([newValue[findAllIndex]]);
  300. setSelectedLabelsString('all')
  301. } else {
  302. const selectedLabels = newValue.map(option => option.type);
  303. const selectedLabelsString = `${selectedLabels.join(',')}`;
  304. setStatus(newValue);
  305. console.log(newValue)
  306. setSelectedLabelsString(selectedLabelsString);
  307. }
  308. console.log(selectedLabelsString)
  309. console.log(status)
  310. }}
  311. renderInput={(params) => (
  312. <TextField {...params}
  313. label={intl.formatMessage({id: 'status'})}
  314. aria-label={intl.formatMessage({id: 'status'})}
  315. InputLabelProps={{
  316. shrink: true
  317. }}
  318. />
  319. )}
  320. // InputLabelProps={{
  321. // shrink: true
  322. // }}
  323. />
  324. </Grid> */}
  325. </Grid>
  326. {/*last row*/}
  327. <Grid container maxWidth justifyContent="flex-end">
  328. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  329. <Grid item sx={{ ml: 3, mr: 3 }}>
  330. <Button
  331. variant="contained"
  332. color="cancel"
  333. onClick={resetForm}
  334. aria-label={intl.formatMessage({id: 'reset'})}
  335. >
  336. <Typography variant="pnspsButtonText">
  337. <FormattedMessage id="reset"/>
  338. </Typography>
  339. </Button>
  340. </Grid>
  341. <Grid item>
  342. <Button
  343. variant="contained"
  344. type="submit"
  345. disabled={onGridReady}
  346. aria-label={intl.formatMessage({id: 'search'})}
  347. >
  348. <Typography variant="pnspsButtonText">
  349. <FormattedMessage id="search"/>
  350. </Typography>
  351. </Button>
  352. </Grid>
  353. </ThemeProvider>
  354. </Grid>
  355. </form>
  356. </MainCard>
  357. );
  358. };
  359. export default SearchPublicNoticeForm;