"use client"
import { SearchJoResultRequest, fetchJos, updateJo,updateProductProcessPriority } from "@/app/api/jo/actions";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { Criterion } from "../SearchBox";
import SearchResults, { Column, defaultPagingController } from "../SearchResults/SearchResults";
import { EditNote } from "@mui/icons-material";
import { arrayToDateString, arrayToDateTimeString, integerFormatter, dayjsToDateString } from "@/app/utils/formatUtil";
import { orderBy, uniqBy, upperFirst } from "lodash";
import SearchBox from "../SearchBox/SearchBox";
import { useRouter } from "next/navigation";
import { FormProvider, SubmitErrorHandler, SubmitHandler, useForm } from "react-hook-form";
import { StockInLineInput } from "@/app/api/stockIn";
import { JobOrder, JoDetailPickLine, JoStatus } from "@/app/api/jo";
import { Button, Stack, Dialog, DialogTitle, DialogContent, DialogActions, TextField, IconButton, InputAdornment } from "@mui/material";
import { BomCombo } from "@/app/api/bom";
import JoCreateFormModal from "./JoCreateFormModal";
import AddIcon from '@mui/icons-material/Add';
import EditIcon from '@mui/icons-material/Edit';
import QcStockInModal from "../Qc/QcStockInModal";
import { useSession } from "next-auth/react";
import { SessionWithTokens } from "@/config/authConfig";
import { createStockInLine } from "@/app/api/stockIn/actions";
import { msg } from "../Swal/CustomAlerts";
import dayjs from "dayjs";
import { fetchInventories } from "@/app/api/inventory/actions";
import { InventoryResult } from "@/app/api/inventory";
import { PrinterCombo } from "@/app/api/settings/printer";
import { JobTypeResponse } from "@/app/api/jo/actions";
import { DatePicker, LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { updateJoPlanStart } from "@/app/api/jo/actions";
import { arrayToDayjs } from "@/app/utils/formatUtil";

interface Props {
    defaultInputs: SearchJoResultRequest,
    bomCombo: BomCombo[]
    printerCombo: PrinterCombo[];
    jobTypes: JobTypeResponse[];
}

type SearchQuery = Partial<Omit<JobOrder, "id">>;
type SearchParamNames = keyof SearchQuery;

const JoSearch: React.FC<Props> = ({ defaultInputs, bomCombo, printerCombo, jobTypes }) => {
    const { t } = useTranslation("jo");
    const router = useRouter()
    const [filteredJos, setFilteredJos] = useState<JobOrder[]>([]);
    const [inputs, setInputs] = useState(defaultInputs);
    const [pagingController, setPagingController] = useState(
        defaultPagingController
    )
    const [totalCount, setTotalCount] = useState(0)
    const [isCreateJoModalOpen, setIsCreateJoModalOpen] = useState(false)

    const [inventoryData, setInventoryData] = useState<InventoryResult[]>([]);
    const [detailedJos, setDetailedJos] = useState<Map<number, JobOrder>>(new Map());
    const [openOperationPriorityDialog, setOpenOperationPriorityDialog] = useState(false);
    const [operationPriority, setOperationPriority] = useState<number>(50);
    const [selectedJo, setSelectedJo] = useState<JobOrder | null>(null);
    const [selectedProductProcessId, setSelectedProductProcessId] = useState<number | null>(null);
    const [openPlanStartDialog, setOpenPlanStartDialog] = useState(false);
    const [planStartDate, setPlanStartDate] = useState<dayjs.Dayjs | null>(null);
    const [selectedJoForDate, setSelectedJoForDate] = useState<JobOrder | null>(null);
 
    const fetchJoDetailClient = async (id: number): Promise<JobOrder> => {
        const response = await fetch(`/api/jo/detail?id=${id}`);
        if (!response.ok) {
            throw new Error('Failed to fetch JO detail');
        }
        return response.json();
    };

    useEffect(() => {
        const fetchDetailedJos = async () => {
            const detailedMap = new Map<number, JobOrder>();
            
            for (const jo of filteredJos) {
                try {
                    const detailedJo = await fetchJoDetailClient(jo.id);
                    detailedMap.set(jo.id, detailedJo);
                } catch (error) {
                    console.error(`Error fetching detail for JO ${jo.id}:`, error);
                }
            }
            
            setDetailedJos(detailedMap);
        };

        if (filteredJos.length > 0) {
            fetchDetailedJos();
        }
    }, [filteredJos]);

    useEffect(() => {
        const fetchInventoryData = async () => {
            try {
                const inventoryResponse = await fetchInventories({
                    code: "",
                    name: "",
                    type: "",
                    pageNum: 0,
                    pageSize: 1000 
                });
                setInventoryData(inventoryResponse.records);
            } catch (error) {
                console.error("Error fetching inventory data:", error);
            }
        };

        fetchInventoryData();
    }, []);
   
    const handleOpenPriorityDialog = useCallback(async (jo: JobOrder) => {
        setSelectedJo(jo);
        setOperationPriority(jo.productionPriority ?? 50);
        
        // 获取 productProcessId
        try {
            const { fetchProductProcessesByJobOrderId } = await import("@/app/api/jo/actions");
            const processes = await fetchProductProcessesByJobOrderId(jo.id);
            if (processes && processes.length > 0) {
                setSelectedProductProcessId(processes[0].id);
                setOpenOperationPriorityDialog(true);
            } else {
                msg(t("No product process found for this job order"));
            }
        } catch (error) {
            console.error("Error fetching product process:", error);
            msg(t("Error loading product process"));
        }
    }, [t]);
    
    const handleClosePriorityDialog = useCallback((_event?: object, _reason?: "backdropClick" | "escapeKeyDown") => {
        setOpenOperationPriorityDialog(false);
        setSelectedJo(null);
        setSelectedProductProcessId(null);
    }, []);

    const getStockAvailable = (pickLine: JoDetailPickLine) => {
        const inventory = inventoryData.find(inventory => 
            inventory.itemCode === pickLine.code || inventory.itemName === pickLine.name
        );
        
        if (inventory) {
            return inventory.availableQty || (inventory.onHandQty - inventory.onHoldQty - inventory.unavailableQty);
        }
        
        return 0;
    };

    const isStockSufficient = (pickLine: JoDetailPickLine) => {
        const stockAvailable = getStockAvailable(pickLine);
        return stockAvailable >= pickLine.reqQty;
    };

    const getStockCounts = (jo: JobOrder) => {
        return {
            sufficient: jo.sufficientCount,
            insufficient: jo.insufficientCount
        };
    };

    const searchCriteria: Criterion<SearchParamNames>[] = useMemo(() => [
        { label: t("Code"), paramName: "code", type: "text" },
        { label: t("Item Name"), paramName: "itemName", type: "text" },
        { label: t("Plan Start"), label2: t("Plan Start To"), paramName: "planStart", type: "dateRange",  preFilledValue: { 
            from: dayjsToDateString(dayjs(), "input"),
            to: dayjsToDateString(dayjs(), "input")
        } },
        { 
            label: t("Job Type"), 
            paramName: "jobTypeName", 
            type: "select", 
            options: jobTypes.map(jt => jt.name) 
        },
    ], [t, jobTypes])
    const handleOpenPlanStartDialog = useCallback((jo: JobOrder) => {
        setSelectedJoForDate(jo);
        // 将 planStart 数组转换为 dayjs 对象
        if (jo.planStart && Array.isArray(jo.planStart)) {
            setPlanStartDate(arrayToDayjs(jo.planStart));
        } else {
            setPlanStartDate(dayjs());
        }
        setOpenPlanStartDialog(true);
    }, []);
    
    const columns = useMemo<Column<JobOrder>[]>(
        () => [
            {
                name: "planStart",
                label: t("Estimated Production Date"),
                align: "left",
                headerAlign: "left",
                renderCell: (row) => {
                    return (
                        <Stack direction="row" alignItems="center" spacing={1}>
                            <span>{row.planStart ? arrayToDateString(row.planStart) : '-'}</span>
                             {row.status == "planning" && (
                            <IconButton 
                                size="small" 
                                onClick={(e) => {
                                    e.stopPropagation();
                                    handleOpenPlanStartDialog(row);
                                }}
                                sx={{ padding: '4px' }}
                            >
                                <EditIcon fontSize="small" />
                            </IconButton>
                            )}
                        </Stack>
                    );
                }
            },
            {
                name: "productionPriority",
                label: t("Production Priority"),
                renderCell: (row) => {
                    return (
                        <Stack direction="row" alignItems="center" spacing={1}>
                            <span>{integerFormatter.format(row.productionPriority)}</span>
                            <IconButton 
                                size="small" 
                                onClick={(e) => {
                                    e.stopPropagation();
                                    handleOpenPriorityDialog(row);
                                }}
                                sx={{ padding: '4px' }}
                            >
                                <EditIcon fontSize="small" />
                            </IconButton>
                        </Stack>
                    );
                }
            },
            {
                name: "code",
                label: t("Code"),
                flex: 2
            },

            {
                name: "item",
                label: `${t("Item Name")}`,
                renderCell: (row) => {
                    return row.item ? <>{t(row.item.code)} {t(row.item.name)}</> : '-'
                }
            },
            {
                name: "reqQty",
                label: t("Req. Qty"),
                align: "right",
                headerAlign: "right",
                renderCell: (row) => {
                    return integerFormatter.format(row.reqQty)
                }
            },
            {
                name: "item",
                label: t("UoM"),
                align: "left",
                headerAlign: "left",
                renderCell: (row) => {
                    return row.item?.uom ? t(row.item.uom.udfudesc) : '-'
                }
            },
            {
                name: "stockStatus" as keyof JobOrder,
                label: t("BOM Status"),
                align: "left",
                headerAlign: "left",
                renderCell: (row) => {
                    const stockCounts = getStockCounts(row);
                    return (
                        <span style={{ color: stockCounts.insufficient > 0 ? 'red' : 'green' }}>
                            {stockCounts.sufficient}/{stockCounts.sufficient + stockCounts.insufficient}
                        </span>
                    );
                }
            },
            {
                name: "status",
                label: t("Status"),
                renderCell: (row) => {
                    return <span style={{color: row.stockInLineStatus == "escalated" ? "red" : "inherit"}}>
                        {t(upperFirst(row.status))}
                    </span>
                }
            },
            {
                name: "jobTypeName",
                label: t("Job Type"),
                renderCell: (row) => {
                    return row.jobTypeName ? t(row.jobTypeName) : '-'
                }
            },
            {
                name: "id",
                label: t("Actions"),
                renderCell: (row) => { 
                    return (
                        <Button
                            id="emailSupplier"
                            type="button"
                            variant="contained"
                            color="primary"
                            sx={{ width: "150px" }}
                            onClick={() => onDetailClick(row)}
                        >
                            {t("View")}
                        </Button>
                    )
                }
            },
        ], [t, inventoryData, detailedJos, handleOpenPriorityDialog,handleOpenPlanStartDialog]
    )

    // 按照 PoSearch 的模式：创建 newPageFetch 函数
    const newPageFetch = useCallback(
        async (
            pagingController: { pageNum: number; pageSize: number },
            filterArgs: SearchJoResultRequest,
        ) => {
            const params: SearchJoResultRequest = {
                ...filterArgs,
                pageNum: pagingController.pageNum - 1,
                pageSize: pagingController.pageSize,
            };
            const response = await fetchJos(params);
            console.log("newPageFetch params:", params)
            console.log("newPageFetch response:", response)
            if (response && response.records) {
                console.log("newPageFetch - setting filteredJos with", response.records.length, "records");
                setTotalCount(response.total);
                // 后端已经按 id DESC 排序，不需要再次排序
                setFilteredJos(response.records);
                console.log("newPageFetch - filteredJos set, first record id:", response.records[0]?.id);
            } else {
                console.warn("newPageFetch - no response or no records");
                setFilteredJos([]);
            }
        },
        [],
    );
    const handleUpdateOperationPriority = useCallback(async (productProcessId: number, productionPriority: number) => {
        const response = await updateProductProcessPriority(productProcessId, productionPriority)
        if (response) {
            // 刷新数据
            await newPageFetch(pagingController, inputs);
        }
    }, [pagingController, inputs, newPageFetch]);
    const handleConfirmPriority = useCallback(async () => {
        if (!selectedProductProcessId) return;
        await handleUpdateOperationPriority(selectedProductProcessId, Number(operationPriority));
        setOpenOperationPriorityDialog(false);
        setSelectedJo(null);
        setSelectedProductProcessId(null);
    }, [selectedProductProcessId, operationPriority, handleUpdateOperationPriority]);
    // 按照 PoSearch 的模式：使用相同的 useEffect 逻辑
    useEffect(() => {
        newPageFetch(pagingController, inputs);
    }, [newPageFetch, pagingController, inputs]);

    const handleUpdate = useCallback(async (jo: JobOrder) => {
        console.log(jo);
        try {
            if (jo.id) {
                const response = await updateJo({ id: jo.id, status: "storing" });
                console.log(`%c Updated JO:`, "color:lime", response);
                const postData = {
                    itemId: jo?.item?.id!!,
                    acceptedQty: jo?.reqQty ?? 1,
                    productLotNo: jo?.code,
                    productionDate: arrayToDateString(dayjs(), "input"),
                    jobOrderId: jo?.id,
                };
                const res = await createStockInLine(postData);
                console.log(`%c Created Stock In Line`, "color:lime", res);
                msg(t("update success"));
                // 重置为默认输入，让 useEffect 自动触发
                setInputs(defaultInputs);
                setPagingController(defaultPagingController);
            }
        } catch (e) {
            console.log(e);
        } finally {
            // setIsUploading(false)
        }
    }, [defaultInputs, t])

    const getButtonSx = (jo : JobOrder) => {
        const joStatus = jo.status?.toLowerCase();
        const silStatus = jo.stockInLineStatus?.toLowerCase();
        let btnSx = {label:"", color:""};
        switch (joStatus) {
          case "planning": btnSx = {label: t("release jo"), color:"primary.main"}; break;
          case "pending": btnSx = {label: t("scan picked material"), color:"error.main"}; break;
          case "processing": btnSx = {label: t("complete jo"), color:"warning.main"}; break;
          case "storing":
            switch (silStatus) {
                case "pending": btnSx = {label: t("process epqc"), color:"success.main"}; break;
                case "received": btnSx = {label: t("view putaway"), color:"secondary.main"}; break;
                case "escalated":
                    if (sessionToken?.id == jo.silHandlerId) {
                        btnSx = {label: t("escalation processing"), color:"warning.main"};
                        break;
                    }
                default: btnSx = {label: t("view stockin"), color:"info.main"};
            } 
            break;
          case "completed": btnSx = {label: t("view stockin"), color:"info.main"}; break;
          default: btnSx = {label: t("scan picked material"), color:"success.main"};
        }
        return btnSx
    };

    const { data: session } = useSession();
    const sessionToken = session as SessionWithTokens | null;

    const [openModal, setOpenModal] = useState<boolean>(false);
    const [modalInfo, setModalInfo] = useState<StockInLineInput>();

    const onDetailClick = useCallback((record: JobOrder) => {
        router.push(`/jo/edit?id=${record.id}`)
    }, [router])

    const closeNewModal = useCallback(() => {
        setOpenModal(false);
      
        setInputs(defaultInputs);
        setPagingController(defaultPagingController);
    }, [defaultInputs]);

    const onSearch = useCallback((query: Record<SearchParamNames, string>) => {
        const transformedQuery = {
            ...query,
            planStart: query.planStart ? `${query.planStart}T00:00` : query.planStart,
            planStartTo: query.planStartTo ? `${query.planStartTo}T23:59:59` : query.planStartTo,
            jobTypeName: query.jobTypeName && query.jobTypeName !== "All" ? query.jobTypeName : ""
        };
        
   
        setInputs({
            code: transformedQuery.code,
            itemName: transformedQuery.itemName,
            planStart: transformedQuery.planStart,
            planStartTo: transformedQuery.planStartTo,
            jobTypeName: transformedQuery.jobTypeName
        });
       
        setPagingController(defaultPagingController);
    }, [defaultInputs])

    const handleClosePlanStartDialog = useCallback((_event?: object, _reason?: "backdropClick" | "escapeKeyDown") => {
        setOpenPlanStartDialog(false);
        setSelectedJoForDate(null);
        setPlanStartDate(null);
    }, []);
    
    const handleUpdatePlanStart = useCallback(async (jobOrderId: number, planStart: string) => {
        const response = await updateJoPlanStart({ id: jobOrderId, planStart });
        if (response) {
            // 刷新数据
            await newPageFetch(pagingController, inputs);
        }
    }, [pagingController, inputs, newPageFetch]);
    
    const handleConfirmPlanStart = useCallback(async () => {
        if (!selectedJoForDate?.id || !planStartDate) return;
        
        // 将日期转换为后端需要的格式 (YYYY-MM-DDTHH:mm:ss)
        const dateString = `${dayjsToDateString(planStartDate, "input")}T00:00:00`;
        await handleUpdatePlanStart(selectedJoForDate.id, dateString);
        setOpenPlanStartDialog(false);
        setSelectedJoForDate(null);
        setPlanStartDate(null);
    }, [selectedJoForDate, planStartDate, handleUpdatePlanStart]);

    const onReset = useCallback(() => {
     
        setInputs(defaultInputs);
        setPagingController(defaultPagingController);
    }, [defaultInputs])


    const onOpenCreateJoModal = useCallback(() => {
        setIsCreateJoModalOpen(() => true)
    }, [])

    const onCloseCreateJoModal = useCallback(() => {
        setIsCreateJoModalOpen(() => false)
    }, [])

    return <>
        <Stack
            direction="row"
            justifyContent="flex-end"
            spacing={2}
            sx={{ mt: 2 }}
        >
            <Button
                variant="outlined"
                startIcon={<AddIcon />}
                onClick={onOpenCreateJoModal}
            >
                {t("Create Job Order")}
            </Button>
        </Stack>
        <SearchBox
            criteria={searchCriteria}
            onSearch={onSearch}
            onReset={onReset}
        />
       <SearchResults<JobOrder>
            items={filteredJos}
            columns={columns}
            setPagingController={setPagingController}
            pagingController={pagingController}
            totalCount={totalCount}
            isAutoPaging={false}
        />
        <JoCreateFormModal
            open={isCreateJoModalOpen}
            bomCombo={bomCombo}
            jobTypes={jobTypes}
            onClose={onCloseCreateJoModal}
            onSearch={() => {
                setInputs({ ...defaultInputs }); // 创建新对象，确保引用变化
                setPagingController(defaultPagingController);
            }}
        />

        <QcStockInModal
            session={sessionToken}
            open={openModal}
            onClose={closeNewModal}
            inputDetail={modalInfo}
            printerCombo={printerCombo}
        />
                 <Dialog
         open={openOperationPriorityDialog}
         onClose={handleClosePriorityDialog}
         fullWidth
         maxWidth="xs"
     >
         <DialogTitle>{t("Update Production Priority")}</DialogTitle>
         <DialogContent>
             <TextField
                 autoFocus
                 margin="dense"
                 label={t("Production Priority")}
                 type="number"
                 fullWidth
                 value={operationPriority}
                 onChange={(e) => setOperationPriority(Number(e.target.value))}
             />
         </DialogContent>
         <DialogActions>
             <Button onClick={handleClosePriorityDialog}>{t("Cancel")}</Button>
             <Button variant="contained" onClick={handleConfirmPriority}>{t("Save")}</Button>
         </DialogActions>
     </Dialog>
     <Dialog
            open={openPlanStartDialog}
            onClose={handleClosePlanStartDialog}
            fullWidth
            maxWidth="xs"
        >
            <DialogTitle>{t("Update Estimated Production Date")}</DialogTitle>
            <DialogContent>
                <LocalizationProvider dateAdapter={AdapterDayjs}>
                    <DatePicker
                        label={t("Estimated Production Date")}
                        value={planStartDate}
                        onChange={(newValue) => setPlanStartDate(newValue)}
                        slotProps={{
                            textField: {
                                fullWidth: true,
                                margin: "dense",
                                autoFocus: true,
                            }
                        }}
                    />
                </LocalizationProvider>
            </DialogContent>
            <DialogActions>
                <Button onClick={handleClosePlanStartDialog}>{t("Cancel")}</Button>
                <Button 
                    variant="contained" 
                    onClick={handleConfirmPlanStart}
                    disabled={!planStartDate}
                >
                    {t("Save")}
                </Button>
            </DialogActions>
        </Dialog>
    </>


}

export default JoSearch;