import { getBoxByNoApi, getSerialNumberByBoxIdApi } from "@/api/modules/boxCode";
import { cloneDeep } from "lodash-es";
import { FORM_TABLE_DATA_INIT } from "../constant/list";
import { msgHtml } from "./msgHtml";

//根据箱ID获取序列号信息
export const getSerialNumberByBoxId = async (datas: any, formTableRef: any) => {
    const result = await getSerialNumberByBoxIdApi({
        boxId: datas.boxBillNoObj.id,
        pageSize: 5000,
        pageNo: 1
    });

    if (result.status === 200) {
        let data: any = result.data;
        if (!datas.isStatus) {
            datas.formTableData.unshift(cloneDeep(FORM_TABLE_DATA_INIT));
            datas.formTableData = [...datas.formTableData, ...data];
            nextTick(() => {
                formTableRef.value.inputElement[0].focus();
            });
        } else {
            datas.formTableData = data;
        }
        let length = datas.formTableData.length;
        for (let i = 0; i < length; i++) {
            let item = datas.formTableData[i];
            if (item.specifications) {
                item.disabled = item.specifications ? true : false;
            }
        }
    }
};
//获取箱号信息
const getBoxByNo = async (datas: any, row: any, formTableRef: any) => {
    const { boxBillNo } = row;
    const result = await getBoxByNoApi({ boxBillNo });
    if (result.status === 200) {
        let data: any = result.data;
        if (datas.isStatus) {
            msgHtml("该箱已完成装箱,需再次装箱请操作重新装箱 !");
        }
        datas.boxBillNoObj = data;
        if (datas.boxBillNoObj.id) {
            //根据箱信息去获取序列号列表数据
            getSerialNumberByBoxId(datas, formTableRef);
        }
    } else {
        msgHtml("请扫描系统存在的箱号 !");
    }
};

export const initOpen = (datas: any, row: any, formTableRef: any) => {
    getBoxByNo(datas, row, formTableRef);
};
