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.

455 rivejä
22 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 ComboData from "utils/ComboData";
  12. import * as DateUtils from "utils/DateUtils";
  13. import * as FormatUtils from "utils/FormatUtils";
  14. import { ThemeProvider } from "@emotion/react";
  15. import { PNSPS_BUTTON_THEME } from "../../../themes/buttonConst";
  16. import { useIntl } from "react-intl";
  17. import {DatePicker} from "@mui/x-date-pickers/DatePicker";
  18. import dayjs from "dayjs";
  19. import {DemoItem} from "@mui/x-date-pickers/internals/demo";
  20. import {LocalizationProvider} from "@mui/x-date-pickers/LocalizationProvider";
  21. import {AdapterDayjs} from "@mui/x-date-pickers/AdapterDayjs";
  22. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  23. const SearchPublicNoticeForm = ({ applySearch, orgComboData, searchCriteria, issueComboData, onGridReady
  24. }) => {
  25. const [type, setType] = React.useState([]);
  26. // const [status, setStatus] = React.useState({ key: 0, label: 'All', type: 'all' });
  27. const [orgSelected, setOrgSelected] = React.useState({});
  28. const [orgCombo, setOrgCombo] = React.useState();
  29. const [issueSelected, setIssueSelected] = React.useState({});
  30. const [issueCombo, setIssueCombo] = React.useState([]);
  31. const [selectedStatus, setSelectedStatus] = React.useState({key: 0, label: 'All', type: 'all'});
  32. const [groupSelected, setGroupSelected] = React.useState(searchCriteria.gazettGroup!=undefined?ComboData.groupTitle.find(item => item.code === searchCriteria.gazettGroup):{});
  33. const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
  34. const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
  35. const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy");
  36. const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy");
  37. React.useEffect(() => {
  38. if(searchCriteria.status!=undefined){
  39. if(searchCriteria.status === ""){
  40. ComboData.publicNoticeStatic_GLD[0]
  41. }else{
  42. setSelectedStatus(ComboData.publicNoticeStatic_GLD.find(item => item.type === searchCriteria.status))
  43. }
  44. }else{
  45. setSelectedStatus(ComboData.publicNoticeStatic_GLD[0])
  46. }
  47. }, [searchCriteria]);
  48. React.useEffect(() => {
  49. setFromDateValue(minDate);
  50. }, [minDate]);
  51. React.useEffect(() => {
  52. setToDateValue(maxDate);
  53. }, [maxDate]);
  54. const intl = useIntl();
  55. const { locale } = intl;
  56. const marginBottom = 2.5;
  57. const { reset, register, handleSubmit } = useForm()
  58. const onSubmit = (data) => {
  59. // localStorage.setItem('searchCriteria',"")
  60. data.status = selectedStatus?.type
  61. let typeArray = [];
  62. let sentDateFrom = "";
  63. let sentDateTo = "";
  64. for (let i = 0; i < type.length; i++) {
  65. typeArray.push(type[i].label);
  66. }
  67. if (fromDateValue != "dd / mm / yyyy" && toDateValue != "dd / mm / yyyy") {
  68. sentDateFrom = DateUtils.dateValue(fromDateValue)
  69. sentDateTo = DateUtils.dateValue(toDateValue)
  70. }
  71. const temp = {
  72. appNo: data.appNo,
  73. dateFrom: sentDateFrom,
  74. dateTo: sentDateTo,
  75. contact: data.contact,
  76. status: (data.status === '' || data.status?.includes("all")) ? "" : data.status,
  77. orgId: (orgSelected?.key && orgSelected?.key > 0) ? orgSelected?.key : "",
  78. issueId: issueSelected?.id,
  79. groupNo: data.groupNo,
  80. gazettGroup: groupSelected?.code,
  81. start:0,
  82. limit:10
  83. };
  84. applySearch(temp);
  85. // setSearchReady(true)
  86. };
  87. React.useEffect(() => {
  88. if (orgComboData && orgComboData.length > 0) {
  89. setOrgCombo(orgComboData);
  90. if(searchCriteria.orgId!=undefined){
  91. setOrgSelected(orgComboData.find(item => item.key === searchCriteria.orgId))
  92. }
  93. }
  94. }, [orgComboData]);
  95. React.useEffect(() => {
  96. if (issueComboData && issueComboData.length > 0) {
  97. setIssueCombo(issueComboData);
  98. if(searchCriteria.issueId!=undefined){
  99. setIssueSelected(issueComboData.find(item => item.id === searchCriteria.issueId))
  100. }
  101. }
  102. }, [issueComboData]);
  103. function resetForm() {
  104. setType([]);
  105. // setStatus({ key: 0, label: 'All', type: 'all' });
  106. setOrgSelected({});
  107. setIssueSelected({});
  108. setGroupSelected({});
  109. setSelectedStatus({key: 0, label: 'All', type: 'all'});
  110. setMinDate(DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)))
  111. setMaxDate(DateUtils.dateValue(new Date()))
  112. reset({
  113. appNo:"",
  114. contact:"",
  115. groupNo:""
  116. });
  117. localStorage.setItem('searchCriteria',"")
  118. }
  119. const getIssueLabel=(data)=> {
  120. let issueYear = data.issueYear
  121. let volume = data.volume;
  122. let issueNo = data.issueNo;
  123. let issueDate = data.issueDate;
  124. if (locale === 'zh-HK') {
  125. return issueYear
  126. + " 第" + volume + "卷,"
  127. + " 第" + issueNo + "期,"
  128. + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
  129. + " (" + DateUtils.getWeekdayStr_ZH(issueDate) + ")";
  130. } else if (locale === 'zh-CN') {
  131. return issueYear
  132. + " 第" + volume + "卷,"
  133. + " 第" + issueNo + "期,"
  134. + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
  135. + " (" + DateUtils.getWeekdayStr_CN(issueDate) + ")";
  136. }
  137. return issueYear
  138. + " Vol. " + FormatUtils.zeroPad(volume, 3)
  139. + ", No. " + FormatUtils.zeroPad(issueNo, 2)
  140. + ", " + DateUtils.dateFormat(issueDate, "D MMM YYYY (ddd)");
  141. }
  142. return (
  143. <MainCard xs={12} md={12} lg={12}
  144. border={false}
  145. content={false}
  146. sx={{ backgroundColor: '#fff' }}
  147. >
  148. <form onSubmit={handleSubmit(onSubmit)}>
  149. <Grid container sx={{ backgroundColor: '#ffffff', ml: 2, mt: 1, mb: marginBottom }} width="98%">
  150. {/*row 1*/}
  151. <Grid item justifyContent="space-between" alignItems="center" sx={{ mt: 1, ml: 3, mb: marginBottom }}>
  152. <Typography variant="pnspsFormHeader" >
  153. Search
  154. </Typography>
  155. </Grid>
  156. {/*row 2*/}
  157. <Grid container display="flex" alignItems={"center"}>
  158. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  159. <TextField
  160. fullWidth
  161. {...register("appNo")}
  162. id='appNo'
  163. label={"Application No."}
  164. defaultValue={searchCriteria.appNo}
  165. InputLabelProps={{
  166. shrink: true
  167. }}
  168. />
  169. </Grid>
  170. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  171. <Grid container spacing={1}>
  172. <Grid item xs={6}>
  173. <LocalizationProvider dateAdapter={AdapterDayjs}>
  174. <DemoItem components={['DatePicker']}>
  175. <DatePicker
  176. id="dateFrom"
  177. // onError={(newError) => setReceiptFromError(newError)}
  178. slotProps={{
  179. field: { readOnly: true, },
  180. // textField: {
  181. // helperText: receiptFromErrorMessage,
  182. // },
  183. }}
  184. format="DD/MM/YYYY"
  185. label={"Submit Date (From)"}
  186. value={minDate === null ? null : dayjs(minDate)}
  187. maxDate={maxDate === null ? null : dayjs(maxDate)}
  188. onChange={(newValue) => {
  189. // console.log(newValue)
  190. if(newValue!=null){
  191. setMinDate(newValue);
  192. }
  193. }}
  194. />
  195. </DemoItem >
  196. </LocalizationProvider>
  197. </Grid>
  198. <Grid item xs={6}>
  199. <LocalizationProvider dateAdapter={AdapterDayjs}>
  200. <DemoItem components={['DatePicker']}>
  201. <DatePicker
  202. id="dateTo"
  203. // onError={(newError) => setReceiptFromError(newError)}
  204. slotProps={{
  205. field: { readOnly: true, },
  206. // textField: {
  207. // helperText: receiptFromErrorMessage,
  208. // },
  209. }}
  210. format="DD/MM/YYYY"
  211. label={"Submit Date (To)"}
  212. value={maxDate === null ? null : dayjs(maxDate)}
  213. minDate={minDate === null ? null : dayjs(minDate)}
  214. onChange={(newValue) => {
  215. // console.log(newValue)
  216. if(newValue!=null){
  217. setMaxDate(newValue);
  218. }
  219. }}
  220. />
  221. </DemoItem >
  222. </LocalizationProvider>
  223. </Grid>
  224. </Grid>
  225. </Grid>
  226. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  227. <TextField
  228. fullWidth
  229. {...register("contact")}
  230. id="contact"
  231. label={"Client"}
  232. defaultValue={searchCriteria.contact}
  233. InputLabelProps={{
  234. shrink: true
  235. }}
  236. />
  237. </Grid>
  238. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  239. <Autocomplete
  240. {...register("status")}
  241. id="status"
  242. size="small"
  243. options={ComboData.publicNoticeStatic_GLD}
  244. value={selectedStatus}
  245. onChange={(event, newValue) => {
  246. if(newValue==null){
  247. setSelectedStatus(ComboData.publicNoticeStatic_GLD[0]);
  248. }else{
  249. setSelectedStatus(newValue);
  250. }
  251. }}
  252. getOptionLabel={(option) => option.label}
  253. renderInput={(params) => (
  254. <TextField
  255. {...params}
  256. label="Status"
  257. InputLabelProps={{
  258. shrink: true
  259. }}
  260. />
  261. )}
  262. />
  263. {/* <Autocomplete
  264. multiple
  265. {...register("status")}
  266. id="status"
  267. size="small"
  268. options={ComboData.publicNoticeStatic_GLD}
  269. value={selectedStatus}
  270. onChange={(event, newValue) => {
  271. const findAllIndex = newValue.findIndex((ele) => {
  272. return ele.type === "all"
  273. })
  274. if (findAllIndex > -1) {
  275. setSelectedStatus([newValue[findAllIndex]]);
  276. setSelectedLabelsString('all')
  277. } else {
  278. const selectedLabels = newValue.map(option => option.type);
  279. const selectedLabelsString = `${selectedLabels.join(',')}`;
  280. setSelectedStatus(newValue);
  281. setSelectedLabelsString(selectedLabelsString);
  282. }
  283. }}
  284. getOptionLabel={(option) => option.label}
  285. renderInput={(params) => (
  286. <TextField
  287. {...params}
  288. label="Status"
  289. InputLabelProps={{
  290. shrink: true
  291. }}
  292. />
  293. )}
  294. /> */}
  295. </Grid>
  296. {
  297. orgCombo ?
  298. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  299. <Autocomplete
  300. {...register("orgId")}
  301. disablePortal
  302. id="orgId"
  303. options={orgCombo}
  304. groupBy={(option) => option.groupType}
  305. size="small"
  306. value={orgSelected}
  307. getOptionLabel={(option) => option.name? option.name : ""}
  308. inputValue={orgSelected ? orgSelected.name!=undefined?orgSelected.name:"" : ""}
  309. onChange={(event, newValue) => {
  310. if (newValue !== null) {
  311. setOrgSelected(newValue);
  312. }else{
  313. setOrgSelected({});
  314. }
  315. }}
  316. renderInput={(params) => (
  317. <TextField {...params}
  318. label="Organisation"
  319. InputLabelProps={{
  320. shrink: true
  321. }}
  322. />
  323. )}
  324. renderGroup={(params) => (
  325. <Grid item key={params.key}>
  326. <Typography fontSize={20} fontStyle="italic" p={1}>
  327. {params.group}
  328. </Typography>
  329. {params.children}
  330. </Grid>
  331. )}
  332. />
  333. </Grid>
  334. : <></>
  335. }
  336. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  337. <Autocomplete
  338. {...register("issueId")}
  339. disablePortal
  340. size="small"
  341. id="issueId"
  342. options={issueCombo}
  343. value={issueSelected}
  344. inputValue={(issueSelected?.id) ? getIssueLabel(issueSelected) : ""}
  345. getOptionLabel={(option) => getIssueLabel(option)}
  346. onChange={(event, newValue) => {
  347. setIssueSelected(newValue);
  348. }}
  349. renderInput={(params) => (
  350. <TextField {...params}
  351. label="Gazette Issue No."
  352. InputLabelProps={{
  353. shrink: true
  354. }}
  355. />
  356. )}
  357. />
  358. </Grid>
  359. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  360. <Autocomplete
  361. {...register("gazettGroup")}
  362. disablePortal
  363. size="small"
  364. id="gazettGroup"
  365. options={ComboData.groupTitle}
  366. value={groupSelected}
  367. inputValue={(groupSelected?.label) ? groupSelected?.label : ""}
  368. getOptionLabel={(option) => option.label}
  369. onChange={(event, newValue) => {
  370. setGroupSelected(newValue);
  371. }}
  372. renderInput={(params) => (
  373. <TextField {...params}
  374. label="Gazette Group"
  375. InputLabelProps={{
  376. shrink: true
  377. }}
  378. />
  379. )}
  380. />
  381. </Grid>
  382. <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
  383. <TextField
  384. fullWidth
  385. {...register("groupNo")}
  386. id='groupNo'
  387. label="Gazette Code"
  388. defaultValue={searchCriteria.groupNo}
  389. InputLabelProps={{
  390. shrink: true
  391. }}
  392. />
  393. </Grid>
  394. </Grid>
  395. {/*last row*/}
  396. <Grid container maxWidth justifyContent="flex-end">
  397. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  398. <Grid item sx={{ ml: 3 }}>
  399. <Button
  400. variant="contained"
  401. color="cancel"
  402. onClick={resetForm}
  403. >
  404. Reset
  405. </Button>
  406. </Grid>
  407. <Grid item sx={{ ml: 3 }}>
  408. <Button
  409. variant="contained"
  410. type="submit"
  411. disabled={onGridReady}
  412. >
  413. Submit
  414. </Button>
  415. </Grid>
  416. </ThemeProvider>
  417. </Grid>
  418. </Grid>
  419. </form>
  420. </MainCard>
  421. );
  422. };
  423. export default SearchPublicNoticeForm;