"use client"; import { Autocomplete, Box, Button, Card, CardContent, FormControl, FormControlLabel, FormLabel, Grid, InputLabel, MenuItem, Radio, RadioGroup, Select, Stack, TextField, Typography, } from "@mui/material"; import { Check, EditNote } from "@mui/icons-material"; import { Controller, useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import InputDataGrid from "../InputDataGrid"; import { SyntheticEvent, useCallback, useEffect, useMemo, useState } from "react"; import { GridColDef, GridRowModel } from "@mui/x-data-grid"; import { InputDataGridProps, TableRow } from "../InputDataGrid/InputDataGrid"; import { TypeEnum } from "@/app/utils/typeEnum"; import { CreateItemInputs } from "@/app/api/settings/item/actions"; import { ItemQc } from "@/app/api/settings/item"; import { QcCategoryCombo } from "@/app/api/settings/qcCategory"; import { WarehouseResult } from "@/app/api/warehouse"; type Props = { // isEditMode: boolean; // type: TypeEnum; isEditMode: boolean; // type: TypeEnum; defaultValues?: Partial | undefined; qcChecks?: ItemQc[]; qcCategoryCombo: QcCategoryCombo[]; warehouses: WarehouseResult[]; }; const ProductDetails: React.FC = ({ isEditMode, qcCategoryCombo, warehouses, defaultValues: initialDefaultValues }) => { const { t, i18n: { language }, } = useTranslation(); const { register, formState: { errors, touchedFields }, watch, control, setValue, getValues, setError, clearErrors, } = useFormContext(); // const typeColumns = useMemo( // () => [ // { // field: "type", // headerName: "type", // flex: 1, // editable: true, // }, // ], // [] // ); // const weightUnitColumns = useMemo( // () => [ // { // field: "weightUnit", // headerName: "Weight Unit", // flex: 1, // editable: true, // }, // { // field: "conversion", // headerName: "conversion", // show base unit // flex: 1, // type: "number", // editable: true, // }, // ], // [] // ); // const uomColumns = useMemo( // () => [ // { // field: "uom", // headerName: "uom", // flex: 1, // editable: true, // }, // ], // [] // ); // const validationTest = useCallback( // ( // newRow: GridRowModel, EntryError>> // ): EntryError => { // const error: EntryError = {}; // console.log(newRow); // return Object.keys(error).length > 0 ? error : undefined; // }, // [] // ); const handleAutoCompleteChange = useCallback((event: SyntheticEvent, value: QcCategoryCombo, onChange: (...event: any[]) => void) => { onChange(value.id) }, []) // Ensure LocationCode is set from defaultValues when component mounts useEffect(() => { if (initialDefaultValues?.LocationCode && !getValues("LocationCode")) { setValue("LocationCode", initialDefaultValues.LocationCode); } }, [initialDefaultValues, setValue, getValues]); return ( {t("Product Details")} ( {t("Type")} {errors.type && ( {errors.type.message} )} )} /> ( qc.id === field.value)} onChange={(event, value) => { handleAutoCompleteChange(event, value, field.onChange) }} onBlur={field.onBlur} renderInput={(params) => ( )} /> )} /> ( ({ label: `${w.code}`, code: w.code, }))} getOptionLabel={(option) => typeof option === "string" ? option : option.label ?? option.code ?? "" } value={ warehouses .map((w) => ({ label: `${w.code}`, code: w.code, })) .find((opt) => opt.code === field.value) || (field.value ? { label: field.value as string, code: field.value as string } : null) } onChange={(_e, value) => { if (typeof value === "string") { field.onChange(value.trim() === "" ? undefined : value); } else { field.onChange(value?.code ? (value.code.trim() === "" ? undefined : value.code) : undefined); } }} onInputChange={(_e, value) => { // keep manual input synced - convert empty string to undefined field.onChange(value.trim() === "" ? undefined : value); }} renderInput={(params) => ( )} /> )} /> {t("Special Type")} { const value = e.target.value; setValue("isEgg", value === "isEgg", { shouldValidate: true }); setValue("isFee", value === "isFee", { shouldValidate: true }); setValue("isBag", value === "isBag", { shouldValidate: true }); }} > } label={t("None")} /> } label={t("isEgg")} /> } label={t("isFee")} /> } label={t("isBag")} /> {/* _formKey={"type"} columns={typeColumns} validateRow={validationTest} /> _formKey={"uom"} columns={uomColumns} validateRow={validationTest} /> _formKey={"weightUnit"} columns={weightUnitColumns} validateRow={validationTest} /> */} ); }; export default ProductDetails;