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.
 
 

324 lines
14 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. import * as FormatUtils from "utils/FormatUtils";
  15. import { FormattedMessage, useIntl } from "react-intl";
  16. import { PNSPS_BUTTON_THEME } from "../../../themes/buttonConst";
  17. import { ThemeProvider } from "@emotion/react";
  18. import {DatePicker} from "@mui/x-date-pickers/DatePicker";
  19. import dayjs from "dayjs";
  20. import {DemoItem} from "@mui/x-date-pickers/internals/demo";
  21. import {LocalizationProvider} from "@mui/x-date-pickers/LocalizationProvider";
  22. import {AdapterDayjs} from "@mui/x-date-pickers/AdapterDayjs";
  23. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  24. const SearchDemandNoteForm = ({ applySearch, searchCriteria, issueComboData, onGridReady
  25. }) => {
  26. const intl = useIntl();
  27. const { locale } = intl;
  28. const [type, setType] = React.useState([]);
  29. const [issueSelected, setIssueSelected] = React.useState({ key: 0, i18nLabel: 'all', labelCht: '全部', label: 'All', type: 'all' });
  30. const [issueCombo, setIssueCombo] = React.useState([]);
  31. const [selectedStatus, setSelectedStatus] = React.useState(ComboData.denmandNoteStatus_Public[0]);
  32. const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
  33. const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
  34. const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy");
  35. const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy");
  36. React.useEffect(() => {
  37. if(searchCriteria.status!=undefined){
  38. if(searchCriteria.status === ""){
  39. ComboData.denmandNoteStatus_Public[0]
  40. }else{
  41. setSelectedStatus(ComboData.denmandNoteStatus_Public.find(item => item.type === searchCriteria.status))
  42. }
  43. }else{
  44. setSelectedStatus(ComboData.denmandNoteStatus_Public[0])
  45. }
  46. }, [searchCriteria]);
  47. React.useEffect(() => {
  48. setFromDateValue(minDate);
  49. }, [minDate]);
  50. React.useEffect(() => {
  51. setToDateValue(maxDate);
  52. }, [maxDate]);
  53. const { reset, register, handleSubmit } = useForm()
  54. const onSubmit = (data) => {
  55. let typeArray = [];
  56. let sentDateFrom = "";
  57. let sentDateTo = "";
  58. for (let i = 0; i < type.length; i++) {
  59. typeArray.push(type[i].label);
  60. }
  61. if (fromDateValue != "dd / mm / yyyy" && toDateValue != "dd / mm / yyyy") {
  62. sentDateFrom = DateUtils.dateValue(fromDateValue)
  63. sentDateTo = DateUtils.dateValue(toDateValue)
  64. }
  65. const temp = {
  66. appNo: data.appNo,
  67. issueId: issueSelected?.id,
  68. dnNo: data.dnNo,
  69. sentDateFrom: sentDateFrom,
  70. sentDateTo: sentDateTo,
  71. status: (selectedStatus?.type && selectedStatus?.type != 'all') ? selectedStatus?.type : "",
  72. };
  73. // console.log(temp)
  74. applySearch(temp);
  75. };
  76. React.useEffect(() => {
  77. if (issueComboData && issueComboData.length > 0) {
  78. setIssueCombo(issueComboData);
  79. if(searchCriteria.issueId!=undefined){
  80. setIssueSelected(issueComboData.find(item => item.id === searchCriteria.issueId))
  81. }
  82. }
  83. }, [issueComboData]);
  84. function resetForm() {
  85. setType([]);
  86. // setStatus({ key: 0, label: 'All', type: 'all' });
  87. // setOrgSelected({});
  88. setMinDate(DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)))
  89. setMaxDate(DateUtils.dateValue(new Date()))
  90. setIssueSelected({});
  91. reset({
  92. appNo:"",
  93. dnNo:"",
  94. });
  95. }
  96. function getIssueLabel(data) {
  97. let issueYear = data.issueYear
  98. let volume = data.volume;
  99. let issueNo = data.issueNo;
  100. let issueDate = data.issueDate;
  101. if (locale === 'zh-HK') {
  102. return issueYear
  103. + " 第" + volume + "卷,"
  104. + " 第" + issueNo + "期,"
  105. + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
  106. + " (" + DateUtils.getWeekdayStr_ZH(issueDate) + ")";
  107. } else if (locale === 'zh-CN') {
  108. return issueYear
  109. + " 第" + volume + "卷,"
  110. + " 第" + issueNo + "期,"
  111. + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
  112. + " (" + DateUtils.getWeekdayStr_CN(issueDate) + ")";
  113. }
  114. return issueYear
  115. + " Vol. " + FormatUtils.zeroPad(volume, 3)
  116. + ", No. " + FormatUtils.zeroPad(issueNo, 2)
  117. + ", " + DateUtils.dateFormat(issueDate, "D MMM YYYY (ddd)");
  118. }
  119. return (
  120. <MainCard xs={12} md={12} lg={12}
  121. border={false}
  122. content={false}
  123. sx={{ backgroundColor: '#fff' }}
  124. >
  125. <form onSubmit={handleSubmit(onSubmit)}>
  126. <Grid container sx={{ backgroundColor: '#ffffff' }} width="98%">
  127. {/*row 1*/}
  128. <Grid item xs={12}>
  129. <CardContent sx={{ px: 2.5, pt: 3 }}>
  130. <Grid item justifyContent="space-between" alignItems="center" >
  131. <Typography variant="pnspsFormHeader">
  132. <FormattedMessage id="searchForm" />
  133. </Typography>
  134. </Grid>
  135. </CardContent>
  136. </Grid>
  137. {/*row 2*/}
  138. <Grid container display="flex" alignItems={"center"}>
  139. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  140. <Autocomplete
  141. {...register("issueId")}
  142. disablePortal
  143. id="issueId"
  144. options={issueCombo}
  145. value={issueSelected}
  146. inputValue={(issueSelected?.id) ? getIssueLabel(issueSelected) : ""}
  147. getOptionLabel={(option) => getIssueLabel(option)}
  148. onChange={(event, newValue) => {
  149. if (newValue !== null) {
  150. setIssueSelected(newValue);
  151. }
  152. }}
  153. renderInput={(params) => (
  154. <TextField {...params}
  155. label={intl.formatMessage({ id: 'gazetteCount' })}
  156. InputLabelProps={{
  157. shrink: true
  158. }}
  159. />
  160. )}
  161. />
  162. </Grid>
  163. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  164. <TextField
  165. fullWidth
  166. {...register("appNo")}
  167. id='appNo'
  168. label={intl.formatMessage({ id: 'applicationId' })}
  169. defaultValue={searchCriteria.appNo}
  170. InputLabelProps={{
  171. shrink: true
  172. }}
  173. />
  174. </Grid>
  175. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  176. <TextField
  177. fullWidth
  178. {...register("dnNo")}
  179. id='dnNo'
  180. label={intl.formatMessage({ id: 'paymentRecordId' })}
  181. defaultValue={searchCriteria.dnNo}
  182. InputLabelProps={{
  183. shrink: true
  184. }}
  185. />
  186. </Grid>
  187. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  188. <LocalizationProvider dateAdapter={AdapterDayjs}>
  189. <DemoItem components={['DatePicker']}>
  190. <DatePicker
  191. id="dateFrom"
  192. // onError={(newError) => setReceiptFromError(newError)}
  193. slotProps={{
  194. field: { readOnly: true, },
  195. // textField: {
  196. // helperText: receiptFromErrorMessage,
  197. // },
  198. }}
  199. format="DD/MM/YYYY"
  200. label={intl.formatMessage({ id: 'sendDateFrom' })}
  201. value={minDate === null ? null : dayjs(minDate)}
  202. maxDate={maxDate === null ? null : dayjs(maxDate)}
  203. onChange={(newValue) => {
  204. // console.log(newValue)
  205. if(newValue!=null){
  206. setMinDate(newValue);
  207. }
  208. }}
  209. />
  210. </DemoItem >
  211. </LocalizationProvider>
  212. </Grid>
  213. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  214. <LocalizationProvider dateAdapter={AdapterDayjs}>
  215. <DemoItem components={['DatePicker']}>
  216. <DatePicker
  217. id="dateTo"
  218. // onError={(newError) => setReceiptFromError(newError)}
  219. slotProps={{
  220. field: { readOnly: true, },
  221. // textField: {
  222. // helperText: receiptFromErrorMessage,
  223. // },
  224. }}
  225. format="DD/MM/YYYY"
  226. label={intl.formatMessage({ id: 'sendDateTo' })}
  227. value={maxDate === null ? null : dayjs(maxDate)}
  228. minDate={minDate === null ? null : dayjs(minDate)}
  229. onChange={(newValue) => {
  230. // console.log(newValue)
  231. if(newValue!=null){
  232. setMaxDate(newValue);
  233. }
  234. }}
  235. />
  236. </DemoItem >
  237. </LocalizationProvider>
  238. </Grid>
  239. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: 3 }}>
  240. <Autocomplete
  241. {...register("status")}
  242. id="status"
  243. size="small"
  244. disableClearable
  245. options={ComboData.denmandNoteStatus_Public}
  246. getOptionLabel={(option) => option?.i18nLabel ? intl.formatMessage({ id: option.i18nLabel }) : ""}
  247. inputValue={selectedStatus?.i18nLabel ? intl.formatMessage({ id: selectedStatus.i18nLabel }) : ""}
  248. value={selectedStatus}
  249. onChange={(event, newValue) => {
  250. if (newValue !== null) {
  251. setSelectedStatus(newValue);
  252. }
  253. }}
  254. renderInput={(params) => (
  255. <TextField
  256. {...params}
  257. label={intl.formatMessage({ id: 'status' })}
  258. />
  259. )}
  260. InputLabelProps={{
  261. shrink: true
  262. }}
  263. />
  264. </Grid>
  265. </Grid>
  266. {/*last row*/}
  267. <Grid container maxWidth justifyContent="flex-end">
  268. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  269. <Grid item sx={{ mr: 3, mb: 3 }}>
  270. <Button
  271. color="cancel"
  272. variant="contained"
  273. onClick={resetForm}
  274. >
  275. <FormattedMessage id="reset" />
  276. </Button>
  277. </Grid>
  278. <Grid item sx={{ mb: 3 }}>
  279. <Button
  280. variant="contained"
  281. type="submit"
  282. disabled={onGridReady}
  283. >
  284. <FormattedMessage id="submit" />
  285. </Button>
  286. </Grid>
  287. </ThemeProvider>
  288. </Grid>
  289. </Grid>
  290. </form>
  291. </MainCard>
  292. );
  293. };
  294. export default SearchDemandNoteForm;