"use client"; import { PurchaseQcResult, PurchaseQCInput } from "@/app/api/po/actions"; import { Box, Card, CardContent, Checkbox, FormControl, FormControlLabel, Grid, Radio, RadioGroup, Stack, Tab, Tabs, TabsProps, TextField, Tooltip, Typography, } from "@mui/material"; import { useFormContext, Controller } from "react-hook-form"; import { useTranslation } from "react-i18next"; import StyledDataGrid from "../StyledDataGrid"; import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useState } from "react"; import { GridColDef, GridRowIdGetter, GridRowModel, useGridApiContext, GridRenderCellParams, GridRenderEditCellParams, useGridApiRef, GridRowSelectionModel, } from "@mui/x-data-grid"; import InputDataGrid from "../InputDataGrid"; import { TableRow } from "../InputDataGrid/InputDataGrid"; import TwoLineCell from "./TwoLineCell"; import QcSelect from "./QcSelect"; import { GridEditInputCell } from "@mui/x-data-grid"; import { StockInLine } from "@/app/api/po"; import { stockInLineStatusMap } from "@/app/utils/formatUtil"; import { fetchQcItemCheck, fetchQcResult } from "@/app/api/qc/actions"; import { QcItemWithChecks } from "@/app/api/qc"; import axios from "@/app/(main)/axios/axiosInstance"; import { NEXT_PUBLIC_API_URL } from "@/config/api"; import axiosInstance from "@/app/(main)/axios/axiosInstance"; import EscalationComponent from "./EscalationComponent"; import QcDataGrid from "./QCDatagrid"; import StockInFormVer2 from "./StockInFormVer2"; import { dummyEscalationHistory, dummyQCData, QcData } from "./dummyQcTemplate"; import { ModalFormInput } from "@/app/api/po/actions"; import { escape } from "lodash"; interface Props { itemDetail: StockInLine; qc: QcItemWithChecks[]; disabled: boolean; qcItems: QcData[] setQcItems: Dispatch> } type EntryError = | { [field in keyof QcData]?: string; } | undefined; type QcRow = TableRow, EntryError>; // fetchQcItemCheck const QcFormVer2: React.FC = ({ qc, itemDetail, disabled, qcItems, setQcItems }) => { const { t } = useTranslation("purchaseOrder"); const apiRef = useGridApiRef(); const { register, formState: { errors, defaultValues, touchedFields }, watch, control, setValue, getValues, reset, resetField, setError, clearErrors, } = useFormContext(); const [tabIndex, setTabIndex] = useState(0); const [rowSelectionModel, setRowSelectionModel] = useState(); const [escalationHistory, setEscalationHistory] = useState(dummyEscalationHistory); const [qcResult, setQcResult] = useState(); const qcAccept = watch("qcAccept"); // const [qcAccept, setQcAccept] = useState(true); // const [qcItems, setQcItems] = useState(dummyQCData) const column = useMemo( () => [ { field: "escalation", headerName: t("escalation"), flex: 1, }, { field: "supervisor", headerName: t("supervisor"), flex: 1, }, ], [] ) const handleTabChange = useCallback>( (_e, newValue) => { setTabIndex(newValue); }, [], ); //// validate form const accQty = watch("acceptQty"); const validateForm = useCallback(() => { console.log(accQty); if (accQty > itemDetail.acceptedQty) { setError("acceptQty", { message: `${t("acceptQty must not greater than")} ${ itemDetail.acceptedQty }`, type: "required", }); } if (accQty < 1) { setError("acceptQty", { message: t("minimal value is 1"), type: "required", }); } if (isNaN(accQty)) { setError("acceptQty", { message: t("value must be a number"), type: "required", }); } }, [accQty]); useEffect(() => { clearErrors(); validateForm(); }, [clearErrors, validateForm]); const columns = useMemo( () => [ { field: "escalation", headerName: t("escalation"), flex: 1, }, { field: "supervisor", headerName: t("supervisor"), flex: 1, }, ], [], ); /// validate datagrid const validation = useCallback( (newRow: GridRowModel): EntryError => { const error: EntryError = {}; // const { qcItemId, failQty } = newRow; return Object.keys(error).length > 0 ? error : undefined; }, [], ); function BooleanEditCell(params: GridRenderEditCellParams) { const apiRef = useGridApiContext(); const { id, field, value } = params; const handleChange = (e: React.ChangeEvent) => { apiRef.current.setEditCellValue({ id, field, value: e.target.checked }); apiRef.current.stopCellEditMode({ id, field }); // commit immediately }; return ; } const qcColumns: GridColDef[] = [ { field: "qcItem", headerName: t("qcItem"), flex: 2, renderCell: (params) => ( {params.value}
{params.row.qcDescription}
), }, { field: 'isPassed', headerName: t("qcResult"), flex: 1.5, renderCell: (params) => { const currentValue = params.value; return ( { const value = e.target.value; setQcItems((prev) => prev.map((r): QcData => (r.id === params.id ? { ...r, isPassed: value === "true" } : r)) ); }} name={`isPassed-${params.id}`} > } label="合格" sx={{ color: currentValue === true ? "green" : "inherit", "& .Mui-checked": {color: "green"} }} /> } label="不合格" sx={{ color: currentValue === false ? "red" : "inherit", "& .Mui-checked": {color: "red"} }} /> ); }, }, { field: "failedQty", headerName: t("failedQty"), flex: 1, // editable: true, renderCell: (params) => ( { const v = e.target.value; const next = v === '' ? undefined : Number(v); if (Number.isNaN(next)) return; setQcItems((prev) => prev.map((r) => (r.id === params.id ? { ...r, failedQty: next } : r)) ); }} onClick={(e) => e.stopPropagation()} onMouseDown={(e) => e.stopPropagation()} onKeyDown={(e) => e.stopPropagation()} inputProps={{ min: 0 }} sx={{ width: '100%' }} /> ), }, { field: "remarks", headerName: t("remarks"), flex: 2, renderCell: (params) => ( { const remarks = e.target.value; // const next = v === '' ? undefined : Number(v); // if (Number.isNaN(next)) return; setQcItems((prev) => prev.map((r) => (r.id === params.id ? { ...r, remarks: remarks } : r)) ); }} onClick={(e) => e.stopPropagation()} onMouseDown={(e) => e.stopPropagation()} onKeyDown={(e) => e.stopPropagation()} inputProps={{ min: 0 }} sx={{ width: '100%' }} /> ), }, ] useEffect(() => { console.log(itemDetail); }, [itemDetail]); // Set initial value for acceptQty useEffect(() => { if (itemDetail?.acceptedQty !== undefined) { setValue("acceptQty", itemDetail.acceptedQty); } }, [itemDetail?.acceptedQty, setValue]); // const [openCollapse, setOpenCollapse] = useState(false) const [isCollapsed, setIsCollapsed] = useState(false); const onFailedOpenCollapse = useCallback((qcItems: QcData[]) => { const isFailed = qcItems.some((qc) => !qc.isPassed) console.log(isFailed) if (isFailed) { setIsCollapsed(true) } else { setIsCollapsed(false) } }, []) // const handleRadioChange = useCallback((event: React.ChangeEvent) => { // const value = event.target.value === 'true'; // setValue("qcAccept", value); // }, [setValue]); useEffect(() => { console.log(itemDetail); }, [itemDetail]); useEffect(() => { // onFailedOpenCollapse(qcItems) // This function is no longer needed }, [qcItems]); // Removed onFailedOpenCollapse from dependency array return ( <> {tabIndex == 0 && ( <> {/* apiRef={apiRef} columns={qcColumns} _formKey="qcResult" validateRow={validation} /> */} {/* */} )} {tabIndex == 1 && ( <> {/* */} {t("Escalation Info")} { setRowSelectionModel(newRowSelectionModel); }} /> )} ( { const value = e.target.value === 'true'; if (!value && Boolean(errors.acceptQty)) { setValue("acceptQty", itemDetail.acceptedQty); } field.onChange(value); }} > } label="接受" /> } label="不接受及上報" /> )} /> {/* {t("Escalation Result")} */} ); }; export default QcFormVer2;