import { msgHtml } from "./msgHtml";
import { getSaveBoxApi } from "@/api/modules/boxCode";
import { getSerialNumberByBoxId } from "./getBoxInfo";
import { ElMessageBox } from "element-plus";
const getSaveBox = async (isCarton: any, details: any, datas: any, formTableRef: any, refInput: any, type: any) => {
    const result = await getSaveBoxApi({
        boxId: datas.boxBillNoObj.id,
        isCarton,
        details
    });

    if (result.status === 200) {
        if (result.isSuccess) {
            //自动保存不需要操作成功提示
            if (type != "hideSave") {
                msgHtml("操作成功 !");
            }

            if (isCarton) {
                datas.boxBillNoDisabled = !isCarton;
                datas.boxBillNo = "";
                datas.boxBillNoObj = {};
                datas.formTableData = [];
                refInput.value.focus();
            } else {
                await getSerialNumberByBoxId(datas, formTableRef);
                formTableRef.value.inputElement[0].focus();
            }
        } else {
            msgHtml(result.message);
        }
    } else {
        msgHtml(result.message);
    }
};
//保存时要做的操作
export const initSave = (datas: any, type: any, formTableRef: any, refInput: any) => {
    if (!datas.boxBillNo) {
        msgHtml("箱号不能空 !");
        return;
    }
    let paramsArray = datas.formTableData.filter((item: any) => {
        return item.materialNumber;
    });
    if (!paramsArray.length) {
        msgHtml("箱子不存在物料信息,请装箱 !");
        return;
    }
    let newParamsArray: any[] = [];
    let length = paramsArray.length;
    for (let i = 0; i < length; i++) {
        let item = paramsArray[i];
        if (!item.isOldData) {
            let obj = {
                materialNumber: item.materialNumber,
                serialNumber: item.isNumber ? item.numberCode : item.serialNumber,
                twoSerialNumber: item?.twoSerialNumber ? item?.twoSerialNumber : "",
                specifications: item.specifications
            };
            newParamsArray.push(obj);
        }
    }
    if (type === "hideSave" && !newParamsArray.length) {
        return false;
    }
    if (type === "hideSave" && newParamsArray.length) {
        getSaveBox(false, newParamsArray, datas, formTableRef, refInput, type);
    }
    if (type === "save") {
        getSaveBox(false, newParamsArray, datas, formTableRef, refInput, type);
    }
    if (type === "commit") {
        ElMessageBox.confirm("确认完成装箱, 箱子实物和系统录入一致?", "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
            customClass: "custom-message-box"
        })
            .then(async () => {
                getSaveBox(true, newParamsArray, datas, formTableRef, refInput, type);
            })
            .catch(() => {});
    }
};
