// 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 DateUtils from "utils/DateUtils"; import * as ComboData from "utils/ComboData"; import {PNSPS_BUTTON_THEME} from "../../../themes/buttonConst"; import {ThemeProvider} from "@emotion/react"; import {DatePicker} from "@mui/x-date-pickers/DatePicker"; import dayjs from "dayjs"; import {DemoItem} from "@mui/x-date-pickers/internals/demo"; import {LocalizationProvider} from "@mui/x-date-pickers/LocalizationProvider"; import {AdapterDayjs} from "@mui/x-date-pickers/AdapterDayjs"; const getDefaultDateFrom = () => DateUtils.dateValue(new Date().setDate(new Date().getDate() - 14)); const getDefaultDateTo = () => DateUtils.dateValue(new Date()); const isSubmitDateEmpty = (value) => value == null || value === "" || value === "dd / mm / yyyy"; const isBlank = (value) => value == null || String(value).trim() === ""; const toDayjsOrNull = (value) => { if (isSubmitDateEmpty(value)) return null; const d = dayjs(value); return d.isValid() ? d : null; }; // ==============================|| DASHBOARD - DEFAULT ||============================== // const SearchPublicNoticeForm = ({ applySearch, searchCriteria, onGridReady }) => { const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom); const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo); const [status, setStatus] = React.useState(ComboData.paymentStatus[0]); const [payMethod, setPayMethod] = React.useState(ComboData.payMethod[0]); const marginBottom = 2.5; const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy"); const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy"); const prevHasOtherRef = React.useRef(null); React.useEffect(() => { if(searchCriteria.status!=undefined){ if(searchCriteria.status === ""){ ComboData.paymentStatus[0] }else{ setStatus(ComboData.paymentStatus.find(item => item.type === searchCriteria.status)) } }else{ setStatus(ComboData.paymentStatus[0]) } }, [searchCriteria]); React.useEffect(() => { const defaultPayMethod = ComboData.payMethod[0]; const value = searchCriteria?.payMethod; // may be [], null, undefined, or array of strings if (!value || value.length === 0) { setPayMethod(defaultPayMethod); return; } // Find the matching entry whose type array matches value contents const found = ComboData.payMethod.find(item => Array.isArray(item.type) && item.type.length === value.length && item.type.every((v, i) => v === value[i]) // strict positional match ); setPayMethod(found ?? defaultPayMethod); }, [searchCriteria?.payMethod]); React.useEffect(() => { setFromDateValue(minDate); }, [minDate]); React.useEffect(() => { setToDateValue(maxDate); }, [maxDate]); const { reset, register, handleSubmit, watch } = useForm({ defaultValues: { code: searchCriteria.code || "", transNo: searchCriteria.transNo || "" } }); const code = watch("code"); const transNo = watch("transNo"); // add near the top inside the component (after useState for payMethod) const toPayMethodArray = (opt) => { if (!opt || opt.type === 'all') return []; return Array.isArray(opt.type) ? opt.type : [opt.type]; }; const clearSubmitDates = () => { setMinDate(null); setMaxDate(null); }; const restoreDefaultSubmitDates = () => { setMinDate(getDefaultDateFrom()); setMaxDate(getDefaultDateTo()); }; const hasOtherCriteria = (textFields = {}) => { if (!isBlank(textFields.code)) return true; if (!isBlank(textFields.transNo)) return true; if (status?.type && status.type !== "all" && status.type !== "") return true; if (payMethod?.type && payMethod.type !== "all") return true; return false; }; React.useEffect(() => { const hasOther = hasOtherCriteria({ code, transNo }); if (prevHasOtherRef.current === null) { prevHasOtherRef.current = hasOther; return; } if (hasOther && !prevHasOtherRef.current) { clearSubmitDates(); } else if (!hasOther && prevHasOtherRef.current) { // Only refill defaults when From or To is empty; keep user-entered dates otherwise if (isSubmitDateEmpty(minDate) || isSubmitDateEmpty(maxDate)) { restoreDefaultSubmitDates(); } } prevHasOtherRef.current = hasOther; }, [code, transNo, status, payMethod]); const onSubmit = (data) => { let sentDateFrom = ""; let sentDateTo = ""; const hasOther = hasOtherCriteria({ code: data.code, transNo: data.transNo }); const datesEmpty = isSubmitDateEmpty(fromDateValue) || isSubmitDateEmpty(toDateValue) || minDate == null || maxDate == null; if (!hasOther && datesEmpty) { const dateFrom = getDefaultDateFrom(); const dateTo = getDefaultDateTo(); setMinDate(dateFrom); setMaxDate(dateTo); sentDateFrom = dateFrom; sentDateTo = dateTo; } else if (!datesEmpty) { sentDateFrom = DateUtils.dateValue(fromDateValue); sentDateTo = DateUtils.dateValue(toDateValue); } const temp = { code: data.code, transNo: data.transNo, dateFrom: sentDateFrom, dateTo: sentDateTo, status : (status?.type && status?.type != 'all') ? status?.type : "", payMethod : toPayMethodArray(payMethod), start:0, limit:10 }; if (searchCriteria?.sort && searchCriteria?.direction) { temp.sort = searchCriteria.sort; temp.direction = searchCriteria.direction; } applySearch(temp); }; function resetForm() { setStatus(ComboData.paymentStatus[0]); setPayMethod(ComboData.payMethod[0]); const dateFrom = getDefaultDateFrom(); const dateTo = getDefaultDateTo(); setMinDate(dateFrom); setMaxDate(dateTo); reset({ code:"", transNo:"" }); prevHasOtherRef.current = false; localStorage.setItem('searchCriteria',""); applySearch({ code: "", transNo: "", dateFrom, dateTo, status: "", payMethod: [], start: 0, limit: 10 }); } return (
{/*row 1*/} Search {/*row 2*/} {}} slotProps={{ field: { readOnly: true, clearable: true }, textField: { InputLabelProps: { shrink: true }, error: false, helperText: null }, }} format="DD/MM/YYYY" label="Payment Date (From)" value={toDayjsOrNull(minDate)} maxDate={toDayjsOrNull(maxDate)} onChange={(newValue) => { setMinDate(newValue && newValue.isValid?.() ? newValue : null); }} /> {}} slotProps={{ field: { readOnly: true, clearable: true }, textField: { InputLabelProps: { shrink: true }, error: false, helperText: null }, }} format="DD/MM/YYYY" label="Payment Date (To)" value={toDayjsOrNull(maxDate)} minDate={toDayjsOrNull(minDate)} onChange={(newValue) => { setMaxDate(newValue && newValue.isValid?.() ? newValue : null); }} /> options} options={ComboData.paymentStatus} value={status} getOptionLabel={(option) => (option?.label != null ? String(option.label) : "")} inputValue={status?.label ? status?.label : ""} onChange={(event, newValue) => { if(newValue==null){ setStatus(ComboData.paymentStatus[0]); }else{ setStatus(newValue); } }} sx={{ '& .MuiInputBase-root': { alignItems: 'center' }, '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' }, '& .MuiOutlinedInput-root': { height: 40 } }} renderInput={(params) => ( )} /> options} options={ComboData.payMethod} value={payMethod} getOptionLabel={(option) => (option?.label != null ? String(option.label) : "")} inputValue={payMethod?.label ? payMethod?.label : ""} onChange={(event, newValue) => { if(newValue==null){ setPayMethod(ComboData.payMethod[0]); }else{ setPayMethod(newValue); } }} sx={{ '& .MuiInputBase-root': { alignItems: 'center' }, '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' }, '& .MuiOutlinedInput-root': { height: 40 } }} renderInput={(params) => ( )} /> {/*last row*/}
); }; export default SearchPublicNoticeForm;