"use client"; import { PurchaseQcResult, PutawayInput } from "@/app/api/po/actions"; import { Autocomplete, Box, Button, Card, CardContent, FormControl, Grid, Modal, ModalProps, Stack, TextField, Tooltip, Typography, } 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 TwoLineCell from "./TwoLineCell"; import QcSelect from "./QcSelect"; import { QcItemWithChecks } from "@/app/api/qc"; import { GridEditInputCell } from "@mui/x-data-grid"; import { StockInLine } from "@/app/api/po"; import { WarehouseResult } from "@/app/api/warehouse"; import { stockInLineStatusMap } from "@/app/utils/formatUtil"; import { QRCodeSVG } from "qrcode.react"; import { QrCode } from "../QrCode"; import ReactQrCodeScanner, { ScannerConfig, } from "../ReactQrCodeScanner/ReactQrCodeScanner"; import { QrCodeInfo } from "@/app/api/qrcode"; interface Props { itemDetail: StockInLine; warehouse: WarehouseResult[]; disabled: boolean // qc: QcItemWithChecks[]; } type EntryError = | { [field in keyof PurchaseQcResult]?: string; } | undefined; // type PoQcRow = TableRow, EntryError>; const style = { position: "absolute", top: "50%", left: "50%", transform: "translate(-50%, -50%)", bgcolor: "background.paper", pt: 5, px: 5, pb: 10, width: "auto", }; const PutawayForm: React.FC = ({ itemDetail, warehouse, disabled }) => { const { t } = useTranslation(); const apiRef = useGridApiRef(); const { register, formState: { errors, defaultValues, touchedFields }, watch, control, setValue, getValues, reset, resetField, setError, clearErrors, } = useFormContext(); console.log(itemDetail); // const [recordQty, setRecordQty] = useState(0); const [warehouseId, setWarehouseId] = useState(itemDetail.defaultWarehouseId); const filteredWarehouse = useMemo(() => { // do filtering here if any return warehouse; }, []); const defaultOption = { value: 0, // think think sin label: t("Select warehouse"), group: "default", } const options = useMemo(() => { return [ // { // value: 0, // think think sin // label: t("Select warehouse"), // group: "default", // }, ...filteredWarehouse.map((w) => ({ value: w.id, label: `${w.code} - ${w.name}`, group: "existing", })), ]; }, [filteredWarehouse]); const currentValue = warehouseId > 0 ? options.find((o) => o.value === warehouseId) : options.find((o) => o.value === getValues("warehouseId")) || defaultOption; const onChange = useCallback( ( event: React.SyntheticEvent, newValue: { value: number; group: string } | { value: number }[] ) => { const singleNewVal = newValue as { value: number; group: string; }; console.log(singleNewVal); console.log("onChange"); // setValue("warehouseId", singleNewVal.value); setWarehouseId(singleNewVal.value); }, [] ); // const accQty = watch("acceptedQty"); // const validateForm = useCallback(() => { // console.log(accQty); // if (accQty > itemDetail.acceptedQty) { // setError("acceptedQty", { // message: `acceptedQty must not greater than ${itemDetail.acceptedQty}`, // type: "required", // }); // } // if (accQty < 1) { // setError("acceptedQty", { // message: `minimal value is 1`, // type: "required", // }); // } // if (isNaN(accQty)) { // setError("acceptedQty", { // message: `value must be a number`, // type: "required", // }); // } // }, [accQty]); // useEffect(() => { // clearErrors(); // validateForm(); // }, [validateForm]); const qrContent = useMemo( () => ({ stockInLineId: itemDetail.id, itemId: itemDetail.itemId, lotNo: itemDetail.lotNo, // warehouseId: 2 // for testing // expiryDate: itemDetail.expiryDate, // productionDate: itemDetail.productionDate, // supplier: itemDetail.supplier, // poCode: itemDetail.poCode, }), [itemDetail] ); const [isOpenScanner, setOpenScanner] = useState(false); const closeHandler = useCallback>( (...args) => { setOpenScanner(false); }, [] ); const onOpenScanner = useCallback(() => { setOpenScanner(true); }, []); const onCloseScanner = useCallback(() => { setOpenScanner(false); }, []); const scannerConfig = useMemo( () => ({ onUpdate: (err, result) => { console.log(result); console.log(Boolean(result)); if (result) { const data: QrCodeInfo = JSON.parse(result.getText()); console.log(data); if (data.warehouseId) { console.log(data.warehouseId); setWarehouseId(data.warehouseId); onCloseScanner(); } } else return; }, }), [onCloseScanner] ); useEffect(() => { setValue("status", "completed"); }, []); useEffect(() => { if (warehouseId > 0) { setValue("warehouseId", warehouseId); clearErrors("warehouseId") } }, [warehouseId]); return ( {t("Putaway Detail")} o.value === 1)} /// modify this later // onChange={onChange} getOptionLabel={(option) => option.label} options={options} renderInput={(params) => ( )} /> {/* { console.log(field); return ( o.value == field.value)} onChange={onChange} getOptionLabel={(option) => option.label} options={options} renderInput={(params) => ( )} /> ); }} /> */} 0 // ? options.find((o) => o.value === warehouseId) // : undefined} value={currentValue} onChange={onChange} getOptionLabel={(option) => option.label} options={options} renderInput={(params) => ( )} /> {/* */} ); }; export default PutawayForm;