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.

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