Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 

303 rader
10 KiB

  1. //src\components\ReportSearchBox\SearchBox2.tsx
  2. "use client";
  3. import Grid from "@mui/material/Grid";
  4. import Card from "@mui/material/Card";
  5. import CardContent from "@mui/material/CardContent";
  6. import Typography from "@mui/material/Typography";
  7. import React, { useCallback, useMemo, useState } from "react";
  8. import { useTranslation } from "react-i18next";
  9. import TextField from "@mui/material/TextField";
  10. import FormControl from "@mui/material/FormControl";
  11. import InputLabel from "@mui/material/InputLabel";
  12. import Select, { SelectChangeEvent } from "@mui/material/Select";
  13. import MenuItem from "@mui/material/MenuItem";
  14. import CardActions from "@mui/material/CardActions";
  15. import Button from "@mui/material/Button";
  16. import RestartAlt from "@mui/icons-material/RestartAlt";
  17. import Search from "@mui/icons-material/Search";
  18. import dayjs from "dayjs";
  19. import "dayjs/locale/zh-hk";
  20. import { DatePicker } from "@mui/x-date-pickers/DatePicker";
  21. import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
  22. import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
  23. import { Box } from "@mui/material";
  24. import * as XLSX from 'xlsx-js-style';
  25. //import { DownloadReportButton } from '../LateStartReportGen/DownloadReportButton';
  26. interface BaseCriterion<T extends string> {
  27. label: string;
  28. label2?: string;
  29. paramName: T;
  30. paramName2?: T;
  31. }
  32. interface TextCriterion<T extends string> extends BaseCriterion<T> {
  33. type: "text";
  34. }
  35. interface SelectCriterion<T extends string> extends BaseCriterion<T> {
  36. type: "select";
  37. options: string[];
  38. }
  39. interface DateRangeCriterion<T extends string> extends BaseCriterion<T> {
  40. type: "dateRange";
  41. }
  42. export type Criterion<T extends string> =
  43. | TextCriterion<T>
  44. | SelectCriterion<T>
  45. | DateRangeCriterion<T>;
  46. interface Props<T extends string> {
  47. criteria: Criterion<T>[];
  48. onSearch: (inputs: Record<T, string>) => void;
  49. onReset?: () => void;
  50. }
  51. function SearchBox<T extends string>({
  52. criteria,
  53. onSearch,
  54. onReset,
  55. }: Props<T>) {
  56. const { t } = useTranslation("common");
  57. const defaultInputs = useMemo(
  58. () =>
  59. criteria.reduce<Record<T, string>>(
  60. (acc, c) => {
  61. return { ...acc, [c.paramName]: c.type === "select" ? "All" : "" };
  62. },
  63. {} as Record<T, string>,
  64. ),
  65. [criteria],
  66. );
  67. const [inputs, setInputs] = useState(defaultInputs);
  68. const makeInputChangeHandler = useCallback(
  69. (paramName: T): React.ChangeEventHandler<HTMLInputElement> => {
  70. return (e) => {
  71. setInputs((i) => ({ ...i, [paramName]: e.target.value }));
  72. };
  73. },
  74. [],
  75. );
  76. const makeSelectChangeHandler = useCallback((paramName: T) => {
  77. return (e: SelectChangeEvent) => {
  78. setInputs((i) => ({ ...i, [paramName]: e.target.value }));
  79. };
  80. }, []);
  81. const makeDateChangeHandler = useCallback((paramName: T) => {
  82. return (e: any) => {
  83. setInputs((i) => ({ ...i, [paramName]: dayjs(e).format("YYYY-MM-DD") }));
  84. };
  85. }, []);
  86. const makeDateToChangeHandler = useCallback((paramName: T) => {
  87. return (e: any) => {
  88. setInputs((i) => ({
  89. ...i,
  90. [paramName + "To"]: dayjs(e).format("YYYY-MM-DD"),
  91. }));
  92. };
  93. }, []);
  94. const handleReset = () => {
  95. setInputs(defaultInputs);
  96. onReset?.();
  97. };
  98. const handleSearch = () => {
  99. onSearch(inputs);
  100. };
  101. const handleDownload = async () => {
  102. //setIsLoading(true);
  103. try {
  104. const response = await fetch('/temp/AR02_Delay Report.xlsx', {
  105. headers: {
  106. 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  107. },
  108. });
  109. if (!response.ok) throw new Error('Network response was not ok.');
  110. const data = await response.blob();
  111. const reader = new FileReader();
  112. reader.onload = (e) => {
  113. if (e.target && e.target.result) {
  114. const ab = e.target.result as ArrayBuffer;
  115. const workbook = XLSX.read(ab, { type: 'array' });
  116. const firstSheetName = workbook.SheetNames[0];
  117. const worksheet = workbook.Sheets[firstSheetName];
  118. // Add the current date to cell C2
  119. const cellAddress = 'C2';
  120. const date = new Date().toISOString().split('T')[0]; // Format YYYY-MM-DD
  121. const formattedDate = date.replace(/-/g, '/'); // Change format to YYYY/MM/DD
  122. XLSX.utils.sheet_add_aoa(worksheet, [[formattedDate]], { origin: cellAddress });
  123. // Calculate the maximum length of content in each column and set column width
  124. const colWidths: number[] = [];
  125. const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1, defval: "", blankrows: true }) as (string | number)[][];
  126. jsonData.forEach((row: (string | number)[]) => {
  127. row.forEach((cell: string | number, index: number) => {
  128. const valueLength = cell.toString().length;
  129. colWidths[index] = Math.max(colWidths[index] || 0, valueLength);
  130. });
  131. });
  132. // Apply calculated widths to each column, skipping column A
  133. worksheet['!cols'] = colWidths.map((width, index) => {
  134. if (index === 0) {
  135. return { wch: 8 }; // Set default or specific width for column A if needed
  136. }
  137. return { wch: width + 2 }; // Add padding to width
  138. });
  139. // Style for cell A1: Font size 16 and bold
  140. if (worksheet['A1']) {
  141. worksheet['A1'].s = {
  142. font: {
  143. bold: true,
  144. sz: 16, // Font size 16
  145. //name: 'Times New Roman' // Specify font
  146. }
  147. };
  148. }
  149. // Apply styles from A2 to A4 (bold)
  150. ['A2', 'A3', 'A4'].forEach(cell => {
  151. if (worksheet[cell]) {
  152. worksheet[cell].s = { font: { bold: true } };
  153. }
  154. });
  155. // Formatting from A6 to K6
  156. // Apply styles from A6 to K6 (bold, bottom border, center alignment)
  157. for (let col = 0; col < 11; col++) { // Columns A to K
  158. const cellRef = XLSX.utils.encode_col(col) + '6';
  159. if (worksheet[cellRef]) {
  160. worksheet[cellRef].s = {
  161. font: { bold: true },
  162. alignment: { horizontal: 'center' },
  163. border: {
  164. bottom: { style: 'thin', color: { auto: 1 } }
  165. }
  166. };
  167. }
  168. }
  169. // Format filename with date
  170. const today = new Date().toISOString().split('T')[0].replace(/-/g, '_'); // Get current date and format as YYYY_MM_DD
  171. const filename = `AR02_Delay_Report_${today}.xlsx`; // Append formatted date to the filename
  172. // Convert workbook back to XLSX file
  173. XLSX.writeFile(workbook, filename);
  174. } else {
  175. throw new Error('Failed to load file');
  176. }
  177. };
  178. reader.readAsArrayBuffer(data);
  179. } catch (error) {
  180. console.error('Error downloading the file: ', error);
  181. }
  182. //setIsLoading(false);
  183. };
  184. return (
  185. <Card>
  186. <CardContent sx={{ display: "flex", flexDirection: "column", gap: 1 }}>
  187. <Typography variant="overline">{t("Search Criteria")}</Typography>
  188. <Grid container spacing={2} columns={{ xs: 6, sm: 12 }}>
  189. {criteria.map((c) => {
  190. return (
  191. <Grid key={c.paramName} item xs={6}>
  192. {c.type === "text" && (
  193. <TextField
  194. label={c.label}
  195. fullWidth
  196. onChange={makeInputChangeHandler(c.paramName)}
  197. value={inputs[c.paramName]}
  198. />
  199. )}
  200. {c.type === "select" && (
  201. <FormControl fullWidth>
  202. <InputLabel>{c.label}</InputLabel>
  203. <Select
  204. label={c.label}
  205. onChange={makeSelectChangeHandler(c.paramName)}
  206. value={inputs[c.paramName]}
  207. >
  208. <MenuItem value={"All"}>{t("All")}</MenuItem>
  209. {c.options.map((option, index) => (
  210. <MenuItem key={`${option}-${index}`} value={option}>
  211. {option}
  212. </MenuItem>
  213. ))}
  214. </Select>
  215. </FormControl>
  216. )}
  217. {c.type === "dateRange" && (
  218. <LocalizationProvider
  219. dateAdapter={AdapterDayjs}
  220. // TODO: Should maybe use a custom adapterLocale here to support YYYY-MM-DD
  221. adapterLocale="zh-hk"
  222. >
  223. <Box display="flex">
  224. <FormControl fullWidth>
  225. <DatePicker
  226. label={c.label}
  227. onChange={makeDateChangeHandler(c.paramName)}
  228. value={inputs[c.paramName] ? dayjs(inputs[c.paramName]) : null}
  229. />
  230. </FormControl>
  231. <Box
  232. display="flex"
  233. alignItems="center"
  234. justifyContent="center"
  235. marginInline={2}
  236. >
  237. {"-"}
  238. </Box>
  239. <FormControl fullWidth>
  240. <DatePicker
  241. label={c.label2}
  242. onChange={makeDateToChangeHandler(c.paramName)}
  243. value={inputs[c.paramName.concat("To") as T] ? dayjs(inputs[c.paramName.concat("To") as T]) : null}
  244. />
  245. </FormControl>
  246. </Box>
  247. </LocalizationProvider>
  248. )}
  249. </Grid>
  250. );
  251. })}
  252. </Grid>
  253. <CardActions sx={{ justifyContent: "flex-end" }}>
  254. <Button
  255. variant="text"
  256. startIcon={<RestartAlt />}
  257. onClick={handleReset}
  258. >
  259. {t("Reset")}
  260. </Button>
  261. <Button
  262. variant="outlined"
  263. startIcon={<Search />}
  264. onClick={handleDownload}
  265. >
  266. {t("Download")}
  267. </Button>
  268. </CardActions>
  269. </CardContent>
  270. </Card>
  271. );
  272. }
  273. export default SearchBox;