"use client"; import { StockInInput, } from "@/app/api/stockIn/actions"; import { Box, Card, CardContent, Grid, Stack, TextField, Tooltip, Typography, Button, } from "@mui/material"; import { Controller, useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import StyledDataGrid from "../StyledDataGrid"; import { useCallback, useEffect, useMemo, useState } from "react"; import { GridColDef, GridRowIdGetter, GridRowModel, useGridApiContext, GridRenderCellParams, GridRenderEditCellParams, useGridApiRef, } from "@mui/x-data-grid"; import InputDataGrid from "../InputDataGrid"; import { TableRow } from "../InputDataGrid/InputDataGrid"; import { QcItemWithChecks } from "@/app/api/qc"; import { GridEditInputCell } from "@mui/x-data-grid"; import { StockInLine } from "@/app/api/stockIn"; import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers"; import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { INPUT_DATE_FORMAT, OUTPUT_DATE_FORMAT } from "@/app/utils/formatUtil"; import dayjs from "dayjs"; import CalculateExpiryDateModal from "./CalculateExpiryDateModal"; import { InputAdornment } from "@mui/material"; import { dayjsToDateString } from "@/app/utils/formatUtil"; // change PurchaseQcResult to stock in entry props interface Props { itemDetail: StockInLine; // qc: QcItemWithChecks[]; disabled: boolean; putawayMode?: boolean; } type EntryError = | { [field in keyof StockInInput]?: string; } | undefined; // type PoQcRow = TableRow, EntryError>; const textfieldSx = { width: "100%", "& .MuiInputBase-root": { // height: "120", // Scales with root font size height: "5rem", // Scales with root font size }, "& .MuiInputBase-input": { height: "100%", boxSizing: "border-box", padding: "0.75rem", fontSize: 30, }, "& .MuiInputLabel-root": { fontSize: 30, transform: "translate(14px, 1.2rem) scale(1)", "&.MuiInputLabel-shrink": { fontSize: 24, transform: "translate(14px, -0.5rem) scale(1)", }, // [theme.breakpoints.down("sm")]: { // fontSize: "1rem", // transform: "translate(14px, 1.5rem) scale(1)", // "&.MuiInputLabel-shrink": { // fontSize: "0.875rem", // }, // }, }, }; const FgStockInForm: React.FC = ({ // qc, itemDetail, disabled, putawayMode = false, }) => { const { t, i18n: { language }, } = useTranslation("purchaseOrder"); const apiRef = useGridApiRef(); const { register, formState: { errors, defaultValues, touchedFields }, watch, control, setValue, getValues, reset, resetField, setError, clearErrors, } = useFormContext(); // console.log(itemDetail); useEffect(() => { // console.log("triggered"); // // receiptDate default tdy // setValue("receiptDate", dayjs().add(0, "month").format(INPUT_DATE_FORMAT)); // setValue("status", "received"); }, [setValue]); useEffect(() => { console.log(errors); }, [errors]); const [openModal, setOpenModal] = useState(false); const [openExpDatePicker, setOpenExpDatePicker] = useState(false); const productionDate = watch("productionDate"); const expiryDate = watch("expiryDate"); const uom = watch("uom"); //// TODO : Add Checking //// // Check if dates are input // if (data.productionDate === undefined || data.productionDate == null) { // validationErrors.push("請輸入生產日期!"); // } // if (data.expiryDate === undefined || data.expiryDate == null) { // validationErrors.push("請輸入到期日!"); // } useEffect(() => { // console.log(uom); // console.log(productionDate); // console.log(expiryDate); if (expiryDate) clearErrors(); if (productionDate) clearErrors(); }, [productionDate, expiryDate, clearErrors]); useEffect(() => { console.log("%c StockInForm itemDetail update: ", "color: brown", itemDetail); }, [itemDetail]); const handleOpenModal = useCallback(() => { setOpenModal(true); }, []); const handleOnModalClose = useCallback(() => { setOpenExpDatePicker(false); setOpenModal(false); }, []); const handleReturnExpiryDate = useCallback((result: dayjs.Dayjs) => { if (result) { setValue("expiryDate", dayjsToDateString(result)); } }, [setValue]); return ( <> {/* {t("Stock In Detail")} */} {putawayMode && ( ) } {putawayMode || ( <> { return ( { if (!date) return; // setValue("receiptDate", date.format(INPUT_DATE_FORMAT)); field.onChange(date); }} inputRef={field.ref} slotProps={{ textField: { // required: true, error: Boolean(errors.receiptDate?.message), helperText: errors.receiptDate?.message, }, }} /> ); }} /> )} {putawayMode ? ( ) : ( ) } { return ( { if (!date) return; setValue( "productionDate", date.format(INPUT_DATE_FORMAT), ); }} inputRef={field.ref} slotProps={{ textField: { error: Boolean(errors.productionDate?.message), helperText: errors.productionDate?.message, }, }} /> ); }} /> {/* {putawayMode || (<> {/* {putawayMode || (<> { return ( { if (!date) return; setValue( "productionDate", date.format(INPUT_DATE_FORMAT), ); // field.onChange(date); }} inputRef={field.ref} slotProps={{ textField: { // required: true, error: Boolean(errors.productionDate?.message), helperText: errors.productionDate?.message, }, }} /> ); }} /> )} */} { return ( { if (!date) return; console.log(date.format(INPUT_DATE_FORMAT)); setValue("expiryDate", date.format(INPUT_DATE_FORMAT)); }} inputRef={field.ref} open={openExpDatePicker && !openModal} onOpen={() => setOpenExpDatePicker(true)} onClose={() => setOpenExpDatePicker(false)} slotProps={{ textField: { InputProps: { ...(!disabled && { endAdornment: ( ), }) }, error: Boolean(errors.expiryDate?.message), helperText: errors.expiryDate?.message, onClick: () => setOpenExpDatePicker(true), }, }} /> ); }} /> {/* {putawayMode ? ( ) : ( )} */} {putawayMode ? (<> sum + p.qty, 0) ?? 0} /> ) : ( )} {/* */} {/* apiRef={apiRef} checkboxSelection={false} _formKey={"qcCheck"} columns={columns} validateRow={validationTest} /> */} ); }; export default FgStockInForm;