|
- // material-ui
- import {
- Button,
- Grid, TextField,
- Autocomplete,
- Typography
- } from '@mui/material';
- import MainCard from "components/MainCard";
- import { useForm } from "react-hook-form";
- import * as React from "react";
- import * as ComboData from "utils/ComboData";
- import * as DateUtils from "utils/DateUtils";
- import * as FormatUtils from "utils/FormatUtils";
- import { ThemeProvider } from "@emotion/react";
- import { PNSPS_BUTTON_THEME } from "../../../themes/buttonConst";
- import { useIntl } from "react-intl";
- import { makeStyles } from '@mui/styles';
-
- // ==============================|| DASHBOARD - DEFAULT ||============================== //
- const useStyles = makeStyles(() => ({
- root: {
- position: "relative"
- },
- display: {
- position: "absolute",
- top: 2,
- left: 12,
- bottom: 2,
- background: "white",
- pointerEvents: "none",
- right: 50,
- display: "flex",
- alignItems: "center",
- width:"70%"
- },
- }));
-
- const SearchPublicNoticeForm = ({ applySearch, orgComboData, searchCriteria, issueComboData
- }) => {
-
- const [type, setType] = React.useState([]);
- // const [status, setStatus] = React.useState({ key: 0, label: 'All', type: 'all' });
- const [orgSelected, setOrgSelected] = React.useState({});
- const [orgCombo, setOrgCombo] = React.useState();
- const [issueSelected, setIssueSelected] = React.useState({});
- const [issueCombo, setIssueCombo] = React.useState([]);
- const [selectedStatus, setSelectedStatus] = React.useState({key: 0, label: 'All', type: 'all'});
-
- const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
- const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
- const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy");
- const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy");
-
- React.useEffect(() => {
- setFromDateValue(minDate);
- }, [minDate]);
-
- React.useEffect(() => {
- setToDateValue(maxDate);
- }, [maxDate]);
-
- function FormDateInputComponent({ inputRef, ...props }) {
- const classes = useStyles();
- return (
- <>
- <div className={classes.display}>
- {DateUtils.dateStr(fromDateValue) == "Invalid Date" ?
- fromDateValue
- :
- DateUtils.dateStr(fromDateValue)}
- </div>
- <input
- className={classes.input}
- ref={inputRef}
- {...props}
- // onChange={handleChange}
- value={fromDateValue}
- max={maxDate}
- />
- </>
- );
- }
-
- function ToDateInputComponent({ inputRef, ...props }) {
- const classes = useStyles();
- return (
- <>
- <div className={classes.display}>
- {DateUtils.dateStr(toDateValue) == "Invalid Date" ?
- toDateValue
- :
- DateUtils.dateStr(toDateValue)}
- </div>
- <input
- // className={classes.input}
- ref={inputRef}
- {...props}
- // onChange={handleChange}
- value={toDateValue}
- min={minDate}
- />
- </>
- );
- }
-
- const intl = useIntl();
- const { locale } = intl;
-
- const marginBottom = 2.5;
- const { reset, register, handleSubmit } = useForm()
- const onSubmit = (data) => {
- data.status = selectedStatus?.type
- let typeArray = [];
- let sentDateFrom = "";
- let sentDateTo = "";
-
- for (let i = 0; i < type.length; i++) {
- typeArray.push(type[i].label);
- }
-
- if (fromDateValue != "dd / mm / yyyy" && toDateValue != "dd / mm / yyyy") {
- sentDateFrom = DateUtils.dateValue(fromDateValue)
- sentDateTo = DateUtils.dateValue(toDateValue)
- }
-
- const temp = {
- appNo: data.appNo,
- dateFrom: sentDateFrom,
- dateTo: sentDateTo,
- contact: data.contact,
- status: (data.status === '' || data.status?.includes("all")) ? "" : data.status,
- orgId: (orgSelected?.key && orgSelected?.key > 0) ? orgSelected?.key : "",
- issueId: issueSelected?.id,
- groupNo: data.groupNo,
- };
- applySearch(temp);
- };
-
- React.useEffect(() => {
- if (orgComboData && orgComboData.length > 0) {
- setOrgCombo(orgComboData);
- }
- }, [orgComboData]);
-
- React.useEffect(() => {
- if (issueComboData && issueComboData.length > 0) {
- setIssueCombo(issueComboData);
- }
- }, [issueComboData]);
-
- function resetForm() {
- setType([]);
- // setStatus({ key: 0, label: 'All', type: 'all' });
- setOrgSelected({});
- setIssueSelected({});
- setSelectedStatus({key: 0, label: 'All', type: 'all'});
- setMinDate(DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)))
- setMaxDate(DateUtils.dateValue(new Date()))
- reset();
- }
-
- const getIssueLabel=(data)=> {
- let issueYear = data.issueYear
- let volume = data.volume;
- let issueNo = data.issueNo;
- let issueDate = data.issueDate;
- if (locale === 'zh-HK') {
- return issueYear
- + " 第" + volume + "卷,"
- + " 第" + issueNo + "期,"
- + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
- + " (" + DateUtils.getWeekdayStr_ZH(issueDate) + ")";
- } else if (locale === 'zh-CN') {
- return issueYear
- + " 第" + volume + "卷,"
- + " 第" + issueNo + "期,"
- + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
- + " (" + DateUtils.getWeekdayStr_CN(issueDate) + ")";
- }
- return issueYear
- + " Vol. " + FormatUtils.zeroPad(volume, 3)
- + ", No. " + FormatUtils.zeroPad(issueNo, 2)
- + ", " + DateUtils.dateFormat(issueDate, "D MMM YYYY (ddd)");
- }
-
- return (
- <MainCard xs={12} md={12} lg={12}
- border={false}
- content={false}
- sx={{ backgroundColor: '#fff' }}
- >
-
- <form onSubmit={handleSubmit(onSubmit)}>
- <Grid container sx={{ backgroundColor: '#ffffff', ml: 2, mt: 1, mb: marginBottom }} width="98%">
- {/*row 1*/}
- <Grid item justifyContent="space-between" alignItems="center" sx={{ mt: 1, ml: 3, mb: marginBottom }}>
- <Typography variant="pnspsFormHeader" >
- Search
- </Typography>
- </Grid>
- {/*row 2*/}
- <Grid container display="flex" alignItems={"center"}>
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <TextField
- fullWidth
- {...register("appNo")}
- id='appNo'
- label={"Application No."}
- defaultValue={searchCriteria.appNo}
- InputLabelProps={{
- shrink: true
- }}
- />
- </Grid>
-
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <Grid container spacing={1}>
- <Grid item xs={6}>
- <TextField
- fullWidth
- {...register("dateFrom")}
- id="dateFrom"
- type="date"
- label={"Submit Date (From)"}
- defaultValue={searchCriteria.dateFrom}
- InputProps={{
- inputComponent: FormDateInputComponent,
- }}
- onChange={(newValue) => {
- if(newValue.target.value!=""){
- setMinDate(newValue.target.value);
- }
- }}
- InputLabelProps={{
- shrink: true
- }}
- sx={{ "& .MuiInputBase-input": {display:"block", textIndent: "-9999px"} }}
- />
- </Grid>
-
- <Grid item xs={6}>
- <TextField
- fullWidth
- InputLabelProps={{
- shrink: true
- }}
- {...register("dateTo")}
- InputProps={{
- inputComponent: ToDateInputComponent,
- }}
- onChange={(newValue) => {
- if(newValue.target.value!=""){
- setMaxDate(newValue.target.value);
- }
- }}
- id="dateTo"
- type="date"
- label={"Submit Date (To)"}
- defaultValue={searchCriteria.dateTo}
- sx={{ "& .MuiInputBase-input": {display:"block", textIndent: "-9999px"} }}
- />
- </Grid>
- </Grid>
- </Grid>
-
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <TextField
- fullWidth
- {...register("contact")}
- id="contact"
- label={"Client"}
- defaultValue={searchCriteria.contact}
- InputLabelProps={{
- shrink: true
- }}
- />
-
- </Grid>
-
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <Autocomplete
- {...register("status")}
- id="status"
- size="small"
- options={ComboData.publicNoticeStatic_GLD}
- value={selectedStatus}
- onChange={(event, newValue) => {
- if(newValue==null){
- setSelectedStatus(ComboData.publicNoticeStatic_GLD[0]);
- }else{
- setSelectedStatus(newValue);
- }
- }}
- getOptionLabel={(option) => option.label}
- renderInput={(params) => (
- <TextField
- {...params}
- label="Status"
- InputLabelProps={{
- shrink: true
- }}
- />
- )}
- />
- {/* <Autocomplete
- multiple
- {...register("status")}
- id="status"
- size="small"
- options={ComboData.publicNoticeStatic_GLD}
- value={selectedStatus}
- onChange={(event, newValue) => {
- const findAllIndex = newValue.findIndex((ele) => {
- return ele.type === "all"
- })
-
- if (findAllIndex > -1) {
- setSelectedStatus([newValue[findAllIndex]]);
- setSelectedLabelsString('all')
- } else {
- const selectedLabels = newValue.map(option => option.type);
- const selectedLabelsString = `${selectedLabels.join(',')}`;
- setSelectedStatus(newValue);
- setSelectedLabelsString(selectedLabelsString);
- }
- }}
- getOptionLabel={(option) => option.label}
- renderInput={(params) => (
- <TextField
- {...params}
- label="Status"
- InputLabelProps={{
- shrink: true
- }}
- />
- )}
- /> */}
- </Grid>
-
- {
- orgCombo ?
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <Autocomplete
- {...register("orgId")}
- disablePortal
- id="orgId"
- options={orgCombo}
- size="small"
- value={orgSelected}
- getOptionLabel={(option) => option.name? option.name : ""}
- inputValue={orgSelected ? orgSelected.name : ""}
- onChange={(event, newValue) => {
- if (newValue !== null) {
- setOrgSelected(newValue);
- }else{
- setOrgSelected({});
- }
- }}
- renderInput={(params) => (
- <TextField {...params}
- label="Organisation"
- InputLabelProps={{
- shrink: true
- }}
- />
- )}
- />
- </Grid>
- : <></>
- }
-
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <Autocomplete
- {...register("issueId")}
- disablePortal
- size="small"
- id="issueId"
- options={issueCombo}
- value={issueSelected}
- inputValue={(issueSelected?.id) ? getIssueLabel(issueSelected) : ""}
- getOptionLabel={(option) => getIssueLabel(option)}
- onChange={(event, newValue) => {
- setIssueSelected(newValue);
- }}
- renderInput={(params) => (
- <TextField {...params}
- label="Gazette Issue No."
- InputLabelProps={{
- shrink: true
- }}
- />
- )}
- />
- </Grid>
-
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <TextField
- fullWidth
- {...register("groupNo")}
- id='groupNo'
- label="Gazette Code"
- defaultValue={searchCriteria.groupNo}
- InputLabelProps={{
- shrink: true
- }}
- />
- </Grid>
- </Grid>
-
- {/*last row*/}
- <Grid container maxWidth justifyContent="flex-end">
- <ThemeProvider theme={PNSPS_BUTTON_THEME}>
- <Grid item sx={{ ml: 3 }}>
- <Button
- variant="contained"
- color="cancel"
- onClick={resetForm}
- >
- Reset
- </Button>
- </Grid>
-
- <Grid item sx={{ ml: 3 }}>
- <Button
- variant="contained"
- type="submit"
- >
- Submit
- </Button>
- </Grid>
- </ThemeProvider>
- </Grid>
- </Grid>
- </form>
- </MainCard>
- );
- };
-
- export default SearchPublicNoticeForm;
|