"use client";

import { CreateItemInputs } from "@/app/api/settings/item/actions";
import {
  GridColDef,
  GridRowModel,
  GridRenderEditCellParams,
  GridEditInputCell,
  GridRowSelectionModel,
  useGridApiRef,
} from "@mui/x-data-grid";
import { MutableRefObject, useCallback, useMemo, useState } from "react";
import { useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import InputDataGrid, { TableRow } from "../InputDataGrid/InputDataGrid";
import { Box, Grid, Tooltip, Typography } from "@mui/material";
import { ItemQc } from "@/app/api/settings/item";
import { QcChecksInputs } from "@/app/api/settings/qcCheck/actions";
import { GridApiCommunity } from "@mui/x-data-grid/internals";
import { RiceBowl } from "@mui/icons-material";
import EditableSearchResults, {
  Column,
} from "@/components/SearchResults/EditableSearchResults";
import { decimalFormatter, integerFormatter } from "@/app/utils/formatUtil";

type Props = {
  apiRef: MutableRefObject<GridApiCommunity>;
  isEdit: boolean;
};
type EntryError =
  | {
      [field in keyof QcChecksInputs]?: string;
    }
  | undefined;

export type FGRecord = {
  id: string | number;
  code: string;
  name: string;
  inStockQty: number;
  productionQty?: number;
  purchaseQty?: number;
  safetyStock?: number;
  lastMonthAvgStock?: number
};

const ViewByFGDetails: React.FC<Props> = ({ apiRef, isEdit }) => {
  const {
    t,
    i18n: { language },
  } = useTranslation("schedule");

  const {
    formState: { errors, defaultValues, touchedFields },
  } = useFormContext<CreateItemInputs>();
  //   const apiRef = useGridApiRef();

  const dayPeriod = [
    "2025-05-07",
    // '2025-05-12',
    // '2025-05-13',
    // '2025-05-14',
    // '2025-05-15',
    // '2025-05-16',
    // '2025-05-17',
  ];

  const fakeRecordLine = useMemo<FGRecord[][]>(
    () => [
      [
        {
          id: 1,
          code: "mt1",
          name: "material 1",
          inStockQty: 10,
          purchaseQty: 1,
        },
        {
          id: 2,
          code: "mt2",
          name: "material 2",
          inStockQty: 20,
          purchaseQty: 199,
        },
      ],
      [
        {
          id: 3,
          code: "mt3",
          name: "material 3",
          inStockQty: 30,
          purchaseQty: 3,
        },
        {
          id: 4,
          code: "mt4",
          name: "material 4",
          inStockQty: 40,
          purchaseQty: 499,
        },
      ],
      [
        {
          id: 5,
          code: "mt5",
          name: "material 5",
          inStockQty: 50,
          purchaseQty: 5,
        },
        {
          id: 6,
          code: "mt6",
          name: "material 6",
          inStockQty: 60,
          purchaseQty: 699,
        },
      ],
      [
        {
          id: 7,
          code: "mt7",
          name: "material 7",
          inStockQty: 70,
          purchaseQty: 7,
        },
        {
          id: 8,
          code: "mt8",
          name: "material 8",
          inStockQty: 80,
          purchaseQty: 899,
        },
      ],
      [
        {
          id: 9,
          code: "mt9",
          name: "material 9",
          inStockQty: 90,
          purchaseQty: 9,
        },
        {
          id: 10,
          code: "mt10",
          name: "material 10",
          inStockQty: 100,
          purchaseQty: 999,
        },
      ],
      [
        {
          id: 11,
          code: "mt11",
          name: "material 11",
          inStockQty: 110,
          purchaseQty: 11,
        },
        {
          id: 12,
          code: "mt12",
          name: "material 12",
          inStockQty: 120,
          purchaseQty: 1299,
        },
      ],
      [
        {
          id: 13,
          code: "mt13",
          name: "material 13",
          inStockQty: 130,
          purchaseQty: 13,
        },
        {
          id: 14,
          code: "mt14",
          name: "material 14",
          inStockQty: 140,
          purchaseQty: 1499,
        },
      ],
    ],
    [],
  );

  const fakeRecords = useMemo<FGRecord[][]>(
    () => [
      [
        {
          id: 1,
          jobNo: "JO20250507001",
          estimatedProductionTime: "1 hr",
          priority: 85,
          code: "PP1193",
          type: "FG",
          name: "蔥油(1磅) ",
          inStockQty: 1322,
          productionQty: 661,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 100,
              purchaseQty: 20,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 80,
              purchaseQty: 10,
            },
          ],
        },
        {
          id: 2,
          jobNo: "JO20250507002",
          estimatedProductionTime: "2 hrs",
          priority: 80,
          code: " PP1096",
          type: "FG",
          name: "白麵撈",
          inStockQty: 1040,
          productionQty: 520,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 1000,
              purchaseQty: 190.0,
            },
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
              inStockQty: 1000,
              purchaseQty: 250.0,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "蔥油",
              inStockQty: 1322,
              purchaseQty: 0,
            },
          ],
        },
        {
          id: 3,
          jobNo: "JO20250507003",
          estimatedProductionTime: "5 hrs : 15 mins",
          priority: 35,
          code: "PP1080",
          type: "FG",
          name: "咖哩汁",
          inStockQty: 2400,
          productionQty: 1200.0,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 108.88,
            },
            {
              id: 2,
              code: "GI3236",
              type: "Material",
              name: "清水(煮過牛腩)",
              inStockQty: 317.52,
              purchaseQty: 635.04,
            },
            {
              id: 3,
              code: "MG1700",
              type: "Material",
              name: "STERILTOM 意大利茄粒",
              inStockQty: 9.0,
              purchaseQty: 18.0,
            },
            {
              id: 4,
              code: "FA0533",
              type: "Material",
              name: "乾蔥茸",
              inStockQty: 6.04,
              purchaseQty: 12.08,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 6,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 7,
              code: "FA0056",
              type: "Material",
              name: "洋蔥肉",
              inStockQty: 241.98,
              purchaseQty: 483.96,
            },
            {
              id: 8,
              code: "PP1188",
              type: "Material",
              name: "咖喱膽",
              inStockQty: 36.0,
              purchaseQty: 72.0,
            },
            {
              id: 9,
              code: "PP8001",
              type: "Material",
              name: "咖哩汁箱料粉",
              inStockQty: 77.42,
              purchaseQty: 154.84,
            },
            {
              id: 10,
              code: "PP1096",
              type: "Material",
              name: "白麵撈",
              inStockQty: 60.0,
              purchaseQty: 120.0,
            },
            {
              id: 10,
              code: "NA0476",
              type: "Material",
              name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
              inStockQty: 600.0,
              purchaseQty: 200.0,
            },
          ],
        },
        {
          id: 4,
          jobNo: "JO20250507004",
          estimatedProductionTime: "3 hrs",
          priority: 20,
          code: " PP1188",
          type: "FG",
          name: "咖喱膽",
          inStockQty: 1016.2,
          productionQty: 508.1,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 217.72,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 3,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 4,
              code: "MG1288",
              type: "Material",
              name: "炸紅蔥頭",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 6,
              code: "MG0066",
              type: "Material",
              name: "咖哩料(5斤x16包+2斤/包)",
              inStockQty: 0,
              purchaseQty: 241.98,
            },
          ],
        },
      ],
      [
        {
          id: 1,
          code: "PP1080",
          type: "FG",
          name: "咖哩汁",
          inStockQty: 2400,
          productionQty: 1200.0,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 108.88,
            },
            {
              id: 2,
              code: "GI3236",
              type: "Material",
              name: "清水(煮過牛腩)",
              inStockQty: 317.52,
              purchaseQty: 635.04,
            },
            {
              id: 3,
              code: "MG1700",
              type: "Material",
              name: "STERILTOM 意大利茄粒",
              inStockQty: 9.0,
              purchaseQty: 18.0,
            },
            {
              id: 4,
              code: "FA0533",
              type: "Material",
              name: "乾蔥茸",
              inStockQty: 6.04,
              purchaseQty: 12.08,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 6,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 7,
              code: "FA0056",
              type: "Material",
              name: "洋蔥肉",
              inStockQty: 241.98,
              purchaseQty: 483.96,
            },
            {
              id: 8,
              code: "PP1188",
              type: "Material",
              name: "咖喱膽",
              inStockQty: 36.0,
              purchaseQty: 72.0,
            },
            {
              id: 9,
              code: "PP8001",
              type: "Material",
              name: "咖哩汁箱料粉",
              inStockQty: 77.42,
              purchaseQty: 154.84,
            },
            {
              id: 10,
              code: "PP1096",
              type: "Material",
              name: "白麵撈",
              inStockQty: 60.0,
              purchaseQty: 120.0,
            },
            {
              id: 10,
              code: "NA0476",
              type: "Material",
              name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
              inStockQty: 600.0,
              purchaseQty: 200.0,
            },
          ],
        },
        {
          id: 2,
          code: "PP1193",
          type: "FG",
          name: "蔥油(1磅) ",
          inStockQty: 1322,
          productionQty: 661,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 20,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 10,
            },
          ],
        },
        {
          id: 3,
          code: " PP1188",
          type: "FG",
          name: "咖喱膽",
          inStockQty: 1016.2,
          productionQty: 508.1,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 217.72,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 3,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 4,
              code: "MG1288",
              type: "Material",
              name: "炸紅蔥頭",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 6,
              code: "MG0066",
              type: "Material",
              name: "咖哩料(5斤x16包+2斤/包)",
              inStockQty: 0,
              purchaseQty: 241.98,
            },
          ],
        },
        {
          id: 4,
          code: " PP1096",
          type: "FG",
          name: "白麵撈",
          inStockQty: 1040,
          productionQty: 520,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 190.0,
            },
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
              inStockQty: 0,
              purchaseQty: 250.0,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "蔥油",
              inStockQty: 1322,
              purchaseQty: 0,
            },
          ],
        },
      ],
      [
        {
          id: 1,
          code: "PP1080",
          type: "FG",
          name: "咖哩汁",
          inStockQty: 2400,
          productionQty: 1200.0,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 108.88,
            },
            {
              id: 2,
              code: "GI3236",
              type: "Material",
              name: "清水(煮過牛腩)",
              inStockQty: 317.52,
              purchaseQty: 635.04,
            },
            {
              id: 3,
              code: "MG1700",
              type: "Material",
              name: "STERILTOM 意大利茄粒",
              inStockQty: 9.0,
              purchaseQty: 18.0,
            },
            {
              id: 4,
              code: "FA0533",
              type: "Material",
              name: "乾蔥茸",
              inStockQty: 6.04,
              purchaseQty: 12.08,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 6,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 7,
              code: "FA0056",
              type: "Material",
              name: "洋蔥肉",
              inStockQty: 241.98,
              purchaseQty: 483.96,
            },
            {
              id: 8,
              code: "PP1188",
              type: "Material",
              name: "咖喱膽",
              inStockQty: 36.0,
              purchaseQty: 72.0,
            },
            {
              id: 9,
              code: "PP8001",
              type: "Material",
              name: "咖哩汁箱料粉",
              inStockQty: 77.42,
              purchaseQty: 154.84,
            },
            {
              id: 10,
              code: "PP1096",
              type: "Material",
              name: "白麵撈",
              inStockQty: 60.0,
              purchaseQty: 120.0,
            },
            {
              id: 10,
              code: "NA0476",
              type: "Material",
              name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
              inStockQty: 600.0,
              purchaseQty: 200.0,
            },
          ],
        },
        {
          id: 2,
          code: "PP1193",
          type: "FG",
          name: "蔥油(1磅) ",
          inStockQty: 1322,
          productionQty: 661,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 20,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 10,
            },
          ],
        },
        {
          id: 3,
          code: " PP1188",
          type: "FG",
          name: "咖喱膽",
          inStockQty: 1016.2,
          productionQty: 508.1,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 217.72,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 3,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 4,
              code: "MG1288",
              type: "Material",
              name: "炸紅蔥頭",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 6,
              code: "MG0066",
              type: "Material",
              name: "咖哩料(5斤x16包+2斤/包)",
              inStockQty: 0,
              purchaseQty: 241.98,
            },
          ],
        },
        {
          id: 4,
          code: " PP1096",
          type: "FG",
          name: "白麵撈",
          inStockQty: 1040,
          productionQty: 520,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 190.0,
            },
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
              inStockQty: 0,
              purchaseQty: 250.0,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "蔥油",
              inStockQty: 1322,
              purchaseQty: 0,
            },
          ],
        },
      ],
      [
        {
          id: 1,
          code: "PP1080",
          type: "FG",
          name: "咖哩汁",
          inStockQty: 2400,
          productionQty: 1200.0,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 108.88,
            },
            {
              id: 2,
              code: "GI3236",
              type: "Material",
              name: "清水(煮過牛腩)",
              inStockQty: 317.52,
              purchaseQty: 635.04,
            },
            {
              id: 3,
              code: "MG1700",
              type: "Material",
              name: "STERILTOM 意大利茄粒",
              inStockQty: 9.0,
              purchaseQty: 18.0,
            },
            {
              id: 4,
              code: "FA0533",
              type: "Material",
              name: "乾蔥茸",
              inStockQty: 6.04,
              purchaseQty: 12.08,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 6,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 7,
              code: "FA0056",
              type: "Material",
              name: "洋蔥肉",
              inStockQty: 241.98,
              purchaseQty: 483.96,
            },
            {
              id: 8,
              code: "PP1188",
              type: "Material",
              name: "咖喱膽",
              inStockQty: 36.0,
              purchaseQty: 72.0,
            },
            {
              id: 9,
              code: "PP8001",
              type: "Material",
              name: "咖哩汁箱料粉",
              inStockQty: 77.42,
              purchaseQty: 154.84,
            },
            {
              id: 10,
              code: "PP1096",
              type: "Material",
              name: "白麵撈",
              inStockQty: 60.0,
              purchaseQty: 120.0,
            },
            {
              id: 10,
              code: "NA0476",
              type: "Material",
              name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
              inStockQty: 600.0,
              purchaseQty: 200.0,
            },
          ],
        },
        {
          id: 2,
          code: "PP1193",
          type: "FG",
          name: "蔥油(1磅) ",
          inStockQty: 1322,
          productionQty: 661,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 20,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 10,
            },
          ],
        },
        {
          id: 3,
          code: " PP1188",
          type: "FG",
          name: "咖喱膽",
          inStockQty: 1016.2,
          productionQty: 508.1,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 217.72,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 3,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 4,
              code: "MG1288",
              type: "Material",
              name: "炸紅蔥頭",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 6,
              code: "MG0066",
              type: "Material",
              name: "咖哩料(5斤x16包+2斤/包)",
              inStockQty: 0,
              purchaseQty: 241.98,
            },
          ],
        },
        {
          id: 4,
          code: " PP1096",
          type: "FG",
          name: "白麵撈",
          inStockQty: 1040,
          productionQty: 520,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 190.0,
            },
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
              inStockQty: 0,
              purchaseQty: 250.0,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "蔥油",
              inStockQty: 1322,
              purchaseQty: 0,
            },
          ],
        },
      ],
      [
        {
          id: 1,
          code: "PP1080",
          name: "咖哩汁",
          inStockQty: 2400,
          productionQty: 1200.0,
          lines: [
            {
              id: 1,
              code: "MH0040",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 108.88,
            },
            {
              id: 2,
              code: "GI3236",
              name: "清水(煮過牛腩)",
              inStockQty: 317.52,
              purchaseQty: 635.04,
            },
            {
              id: 3,
              code: "MG1700",
              name: "STERILTOM 意大利茄粒",
              inStockQty: 9.0,
              purchaseQty: 18.0,
            },
            {
              id: 4,
              code: "FA0533",
              name: "乾蔥茸",
              inStockQty: 6.04,
              purchaseQty: 12.08,
            },
            {
              id: 5,
              code: "FA0210",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 6,
              code: "FA0608",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 7,
              code: "FA0056",
              name: "洋蔥肉",
              inStockQty: 241.98,
              purchaseQty: 483.96,
            },
            {
              id: 8,
              code: "PP1188",
              name: "咖喱膽",
              inStockQty: 36.0,
              purchaseQty: 72.0,
            },
            {
              id: 9,
              code: "PP8001",
              name: "咖哩汁箱料粉",
              inStockQty: 77.42,
              purchaseQty: 154.84,
            },
            {
              id: 10,
              code: "PP1096",
              name: "白麵撈",
              inStockQty: 60.0,
              purchaseQty: 120.0,
            },
            {
              id: 10,
              code: "NA0476",
              name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
              inStockQty: 600.0,
              purchaseQty: 200.0,
            },
          ],
        },
        {
          id: 2,
          code: "PP1193",
          name: "蔥油(1磅) ",
          inStockQty: 1322,
          productionQty: 661,
          lines: [
            {
              id: 1,
              code: "MH0040",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 20,
            },
            {
              id: 2,
              code: "FA0161",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 10,
            },
          ],
        },
        {
          id: 3,
          code: " PP1188",
          name: "咖喱膽",
          inStockQty: 1016.2,
          productionQty: 508.1,
          lines: [
            {
              id: 1,
              code: "MH0040",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 217.72,
            },
            {
              id: 2,
              code: "FA0161",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 3,
              code: "FA0608",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 4,
              code: "MG1288",
              name: "炸紅蔥頭",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 5,
              code: "FA0210",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 6,
              code: "MG0066",
              name: "咖哩料(5斤x16包+2斤/包)",
              inStockQty: 0,
              purchaseQty: 241.98,
            },
          ],
        },
        {
          id: 4,
          code: " PP1096",
          name: "白麵撈",
          inStockQty: 1040,
          productionQty: 520,
          lines: [
            {
              id: 1,
              code: "MH0040",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 190.0,
            },
            {
              id: 1,
              code: "MH0040",
              name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
              inStockQty: 0,
              purchaseQty: 250.0,
            },
            {
              id: 2,
              code: "FA0161",
              name: "蔥油",
              inStockQty: 1322,
              purchaseQty: 0,
            },
          ],
        },
      ],
      [
        {
          id: 1,
          code: "PP1080",
          type: "FG",
          name: "咖哩汁",
          inStockQty: 2400,
          productionQty: 1200.0,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 108.88,
            },
            {
              id: 2,
              code: "GI3236",
              type: "Material",
              name: "清水(煮過牛腩)",
              inStockQty: 317.52,
              purchaseQty: 635.04,
            },
            {
              id: 3,
              code: "MG1700",
              type: "Material",
              name: "STERILTOM 意大利茄粒",
              inStockQty: 9.0,
              purchaseQty: 18.0,
            },
            {
              id: 4,
              code: "FA0533",
              type: "Material",
              name: "乾蔥茸",
              inStockQty: 6.04,
              purchaseQty: 12.08,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 6,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 7,
              code: "FA0056",
              type: "Material",
              name: "洋蔥肉",
              inStockQty: 241.98,
              purchaseQty: 483.96,
            },
            {
              id: 8,
              code: "PP1188",
              type: "Material",
              name: "咖喱膽",
              inStockQty: 36.0,
              purchaseQty: 72.0,
            },
            {
              id: 9,
              code: "PP8001",
              type: "Material",
              name: "咖哩汁箱料粉",
              inStockQty: 77.42,
              purchaseQty: 154.84,
            },
            {
              id: 10,
              code: "PP1096",
              type: "Material",
              name: "白麵撈",
              inStockQty: 60.0,
              purchaseQty: 120.0,
            },
            {
              id: 10,
              code: "NA0476",
              type: "Material",
              name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
              inStockQty: 600.0,
              purchaseQty: 200.0,
            },
          ],
        },
        {
          id: 2,
          code: "PP1193",
          type: "FG",
          name: "蔥油(1磅) ",
          inStockQty: 1322,
          productionQty: 661,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 20,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 10,
            },
          ],
        },
        {
          id: 3,
          code: " PP1188",
          type: "FG",
          name: "咖喱膽",
          inStockQty: 1016.2,
          productionQty: 508.1,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 217.72,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 3,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 4,
              code: "MG1288",
              type: "Material",
              name: "炸紅蔥頭",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 6,
              code: "MG0066",
              type: "Material",
              name: "咖哩料(5斤x16包+2斤/包)",
              inStockQty: 0,
              purchaseQty: 241.98,
            },
          ],
        },
        {
          id: 4,
          code: " PP1096",
          type: "FG",
          name: "白麵撈",
          inStockQty: 1040,
          productionQty: 520,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 190.0,
            },
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
              inStockQty: 0,
              purchaseQty: 250.0,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "蔥油",
              inStockQty: 1322,
              purchaseQty: 0,
            },
          ],
        },
      ],
      [
        {
          id: 1,
          code: "PP1080",
          type: "FG",
          name: "咖哩汁",
          inStockQty: 2400,
          productionQty: 1200.0,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 108.88,
            },
            {
              id: 2,
              code: "GI3236",
              type: "Material",
              name: "清水(煮過牛腩)",
              inStockQty: 317.52,
              purchaseQty: 635.04,
            },
            {
              id: 3,
              code: "MG1700",
              type: "Material",
              name: "STERILTOM 意大利茄粒",
              inStockQty: 9.0,
              purchaseQty: 18.0,
            },
            {
              id: 4,
              code: "FA0533",
              type: "Material",
              name: "乾蔥茸",
              inStockQty: 6.04,
              purchaseQty: 12.08,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 6,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 12.08,
            },
            {
              id: 7,
              code: "FA0056",
              type: "Material",
              name: "洋蔥肉",
              inStockQty: 241.98,
              purchaseQty: 483.96,
            },
            {
              id: 8,
              code: "PP1188",
              type: "Material",
              name: "咖喱膽",
              inStockQty: 36.0,
              purchaseQty: 72.0,
            },
            {
              id: 9,
              code: "PP8001",
              type: "Material",
              name: "咖哩汁箱料粉",
              inStockQty: 77.42,
              purchaseQty: 154.84,
            },
            {
              id: 10,
              code: "PP1096",
              type: "Material",
              name: "白麵撈",
              inStockQty: 60.0,
              purchaseQty: 120.0,
            },
            {
              id: 10,
              code: "NA0476",
              type: "Material",
              name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
              inStockQty: 600.0,
              purchaseQty: 200.0,
            },
          ],
        },
        {
          id: 2,
          code: "PP1193",
          type: "FG",
          name: "蔥油(1磅) ",
          inStockQty: 1322,
          productionQty: 661,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 20,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 10,
            },
          ],
        },
        {
          id: 3,
          code: " PP1188",
          type: "FG",
          name: "咖喱膽",
          inStockQty: 1016.2,
          productionQty: 508.1,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 217.72,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "洋蔥粒",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 3,
              code: "FA0608",
              type: "Material",
              name: "粗蒜茸",
              inStockQty: 0,
              purchaseQty: 18.15,
            },
            {
              id: 4,
              code: "MG1288",
              type: "Material",
              name: "炸紅蔥頭",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 5,
              code: "FA0210",
              type: "Material",
              name: "薑茸",
              inStockQty: 0,
              purchaseQty: 6.05,
            },
            {
              id: 6,
              code: "MG0066",
              type: "Material",
              name: "咖哩料(5斤x16包+2斤/包)",
              inStockQty: 0,
              purchaseQty: 241.98,
            },
          ],
        },
        {
          id: 4,
          code: " PP1096",
          type: "FG",
          name: "白麵撈",
          inStockQty: 1040,
          productionQty: 520,
          lines: [
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "大豆油(1噸/桶)",
              inStockQty: 0,
              purchaseQty: 190.0,
            },
            {
              id: 1,
              code: "MH0040",
              type: "Material",
              name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
              inStockQty: 0,
              purchaseQty: 250.0,
            },
            {
              id: 2,
              code: "FA0161",
              type: "Material",
              name: "蔥油",
              inStockQty: 1322,
              purchaseQty: 0,
            },
          ],
        },
      ],
    ],
    [],
  );

  const [pagingController, setPagingController] = useState([
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
    {
      pageNum: 1,
      pageSize: 10,
      totalCount: 0,
    },
  ]);

  const updatePagingController = (updatedObj: any) => {
    setPagingController((prevState) => {
      return prevState.map((item, index) => {
        if (index === updatedObj?.index) {
          return {
            ...item,
            pageNum: item.pageNum,
            pageSize: item.pageSize,
            totalCount: item.totalCount,
          };
        } else return item;
      });
    });
  };

  const columns = useMemo<Column<any>[]>(
    () => [
      {
        field: "jobNo",
        label: t("Job No."),
        type: "read-only",
        // editable: true,
      },
      {
        field: "code",
        label: t("code"),
        type: "read-only",
        // editable: true,
      },
      {
        field: "name",
        label: t("name"),
        type: "read-only",
      },
      {
        field: "type",
        label: t("type"),
        type: "read-only",
        // editable: true,
      },
      // {
      //     field: "inStockQty",
      //     label: "Available Qty",
      //     type: 'read-only',
      //     style: {
      //         textAlign: "right",
      //     },
      //     // editable: true,
      //     renderCell: (row: FGRecord) => {
      //         if (typeof (row.inStockQty) == "number") {
      //             return decimalFormatter.format(row.inStockQty)
      //         }
      //         return row.inStockQty
      //     }
      // },
      {
        field: "productionQty",
        label: t("Demand Qty"),
        type: "input",
        style: {
          textAlign: "right",
        },
        renderCell: (row: FGRecord) => {
          if (typeof row.productionQty == "number") {
            return decimalFormatter.format(row.productionQty ?? 0);
          }
          return row.productionQty;
        },
      },
      {
        field: "estimatedProductionTime",
        label: t("Estimated Production Time"),
        type: "read-only",
        style: {
          textAlign: "right",
        },
      },
      {
        field: "priority",
        label: t("Production Priority"),
        type: "read-only",
        style: {
          textAlign: "right",
        },
        // editable: true,
      },
    ],
    [],
  );

  const overallColumns = useMemo<Column<any>[]>(
    () => [
      {
        field: "code",
        label: t("code"),
        type: "read-only",
        // editable: true,
      },
      {
        field: "name",
        label: t("name"),
        type: "read-only",
      },
      {
        field: "type",
        label: t("type"),
        type: "read-only",
        // editable: true,
      },
      {
        field: "lastMonthAvgStock",
        label: t("Last Month Average Stock"),
        type: "read-only",
        style: {
          textAlign: "right",
        },
        renderCell: (row: FGRecord) => {
          if (typeof row.lastMonthAvgStock == "number") {
            return decimalFormatter.format(row.lastMonthAvgStock);
          }
          return row.lastMonthAvgStock;
        },
        // editable: true,
      },
      {
        field: "safetyStock",
        label: t("Safety Stock"),
        type: "read-only",
        style: {
          textAlign: "right",
        },
        renderCell: (row: FGRecord) => {
          if (typeof row.safetyStock == "number") {
            return decimalFormatter.format(row.safetyStock);
          }
          return row.safetyStock;
        },
        // editable: true,
      },
      {
        field: "inStockQty",
        label: t("Available Qty"),
        type: "read-only",
        style: {
          textAlign: "right",
        },
        renderCell: (row: FGRecord) => {
          if (typeof row.inStockQty == "number") {
            return decimalFormatter.format(row.inStockQty);
          }
          return row.inStockQty;
        },
        // editable: true,
      },
      {
        field: "productionQty",
        label: t("Demand Qty (7 Days)"),
        type: "read-only",
        style: {
          textAlign: "right",
        },
        renderCell: (row: FGRecord) => {
          if (typeof row.productionQty == "number") {
            return decimalFormatter.format(row.productionQty);
          }
          return row.productionQty;
        },
      },
    ],
    [],
  );

  const fakeOverallRecords = useMemo<FGRecord[]>(
    () => [
      {
        id: 1,
        jobNo: "JO20250507001",
        priority: 20,
        code: "PP1193",
        type: "FG",
        name: "蔥油(1磅) ",
        lastMonthAvgStock: 1320,
        safetyStock: 1322,
        inStockQty: 1322,
        productionQty: 4627,
        lines: [
          {
            id: 2,
            code: "MH0040",
            type: "Material",
            name: "大豆油(1噸/桶)",
            inStockQty: 0,
            purchaseQty: 20 * 7,
          },
          {
            id: 3,
            code: "FA0161",
            type: "Material",
            name: "洋蔥粒",
            inStockQty: 0,
            purchaseQty: 10 * 7,
          },
        ],
      },
      {
        id: 2,
        jobNo: "JO20250507002",
        priority: 25,
        code: " PP1096",
        type: "FG",
        name: "白麵撈",
        lastMonthAvgStock: 1040,
        safetyStock: 1040,
        inStockQty: 1040,
        productionQty: 3640,
        lines: [
          {
            id: 1,
            code: "MH0040",
            type: "Material",
            name: "大豆油(1噸/桶)",
            inStockQty: 0,
            purchaseQty: 190.0 * 7,
          },
          {
            id: 1,
            code: "MH0040",
            type: "Material",
            name: "星加坡綠富貴花牌幼白麵粉 (50磅/包)",
            inStockQty: 0,
            purchaseQty: 250.0 * 7,
          },
          {
            id: 2,
            code: "FA0161",
            type: "Material",
            name: "蔥油",
            inStockQty: 1322,
            purchaseQty: 0,
          },
        ],
      },
      {
        id: 3,
        jobNo: "JO20250507003",
        priority: 70,
        code: "PP1080",
        type: "FG",
        name: "咖哩汁",
        lastMonthAvgStock: 2400,
        safetyStock: 2400,
        inStockQty: 2400,
        productionQty: 8400.0 * 7,
        lines: [
          {
            id: 1,
            code: "MH0040",
            type: "Material",
            name: "大豆油(1噸/桶)",
            inStockQty: 54.44,
            purchaseQty: 544.4 * 7,
          },
          {
            id: 2,
            code: "GI3236",
            type: "Material",
            name: "清水(煮過牛腩)",
            inStockQty: 317.52,
            purchaseQty: 3175.2 * 7,
          },
          {
            id: 3,
            code: "MG1700",
            type: "Material",
            name: "STERILTOM 意大利茄粒",
            inStockQty: 9.0,
            purchaseQty: 90 * 7,
          },
          {
            id: 4,
            code: "FA0533",
            type: "Material",
            name: "乾蔥茸",
            inStockQty: 6.04,
            purchaseQty: 60.4 * 7,
          },
          {
            id: 5,
            code: "FA0210",
            type: "Material",
            name: "薑茸",
            inStockQty: 6.04,
            purchaseQty: 60.4 * 7,
          },
          {
            id: 6,
            code: "FA0608",
            type: "Material",
            name: "粗蒜茸",
            inStockQty: 6.04,
            purchaseQty: 60.4 * 7,
          },
          {
            id: 7,
            code: "FA0056",
            type: "Material",
            name: "洋蔥肉",
            inStockQty: 241.98,
            purchaseQty: 2419.8 * 7,
          },
          {
            id: 8,
            code: "PP1188",
            type: "Material",
            name: "咖喱膽",
            inStockQty: 36.0,
            purchaseQty: 360 * 7,
          },
          {
            id: 9,
            code: "PP8001",
            type: "Material",
            name: "咖哩汁箱料粉",
            inStockQty: 77.42,
            purchaseQty: 774.2 * 7,
          },
          {
            id: 10,
            code: "PP1096",
            type: "Material",
            name: "白麵撈",
            inStockQty: 60.0,
            purchaseQty: 600 * 7,
          },
          {
            id: 10,
            code: "NA0476",
            type: "Material",
            name: "2磅份量三邊覆合袋 (0.1x225x260mm)個計",
            inStockQty: 600.0,
            purchaseQty: 6000 * 7,
          },
        ],
      },
      {
        id: 4,
        jobNo: "JO20250507004",
        priority: 80,
        code: " PP1188",
        type: "FG",
        name: "咖喱膽",
        lastMonthAvgStock: 1017,
        safetyStock: 1017,
        inStockQty: 1016.2,
        productionQty: 3556.7,
        lines: [
          {
            id: 1,
            code: "MH0040",
            type: "Material",
            name: "大豆油(1噸/桶)",
            inStockQty: 0,
            purchaseQty: 217.72 * 7,
          },
          {
            id: 2,
            code: "FA0161",
            type: "Material",
            name: "洋蔥粒",
            inStockQty: 0,
            purchaseQty: 18.15 * 7,
          },
          {
            id: 3,
            code: "FA0608",
            type: "Material",
            name: "粗蒜茸",
            inStockQty: 0,
            purchaseQty: 18.15 * 7,
          },
          {
            id: 4,
            code: "MG1288",
            type: "Material",
            name: "炸紅蔥頭",
            inStockQty: 0,
            purchaseQty: 6.05 * 7,
          },
          {
            id: 5,
            code: "FA0210",
            type: "Material",
            name: "薑茸",
            inStockQty: 0,
            purchaseQty: 6.05 * 7,
          },
          {
            id: 6,
            code: "MG0066",
            type: "Material",
            name: "咖哩料(5斤x16包+2斤/包)",
            inStockQty: 0,
            purchaseQty: 241.98 * 7,
          },
        ],
      },
    ],
    [],
  );

  return (
    <Grid container spacing={2}>
      {/* <Grid item xs={12} key={"all"}>
                <Typography variant="overline" display="block" marginBlockEnd={1}>
                    {t("FG Demand List (7 Days)")}
                </Typography>
                <EditableSearchResults<FGRecord>
                    index={7}
                    items={fakeOverallRecords}
                    columns={overallColumns}
                    setPagingController={updatePagingController}
                    pagingController={pagingController[7]}
                    isAutoPaging={false}
                    isEditable={false}
                    isEdit={isEdit}
                    hasCollapse={true}
                />
            </Grid> */}
      {dayPeriod.map((date, index) => (
        <Grid item xs={12} key={index}>
          {/* <Typography variant="overline" display="block" marginBlockEnd={1}>
                        {`${t("FG Demand Date")}: ${date}`}
                    </Typography> */}
          <EditableSearchResults<FGRecord>
            index={1}
            items={fakeRecords[index]} // Use the corresponding records for the day
            columns={columns}
            setPagingController={updatePagingController}
            pagingController={pagingController[index]}
            isAutoPaging={false}
            isEditable={true}
            isEdit={isEdit}
            hasCollapse={true}
          />
        </Grid>
      ))}
    </Grid>
  );
};
export default ViewByFGDetails;
