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: 1000,
        pageNo: 1
    });

    if (result.status === 200) {
        let data: any = result.data;
        if (!datas.isStatus) {
            datas.formTableData = [...data];
            datas.formTableData.unshift(cloneDeep(FORM_TABLE_DATA_INIT));
            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;
            }
        }
        // nextTick(() => {
        //     formTableRef.value.inputElement[0].focus();
        // });
    }
};
//获取箱号信息
const getBoxByNo = async (datas: any, formTableRef: any) => {
    const { boxBillNo } = datas;
    const result = await getBoxByNoApi({ boxBillNo });
    if (result.status === 200) {
        let data: any = result.data;
        if (data.status === "已完成") {
            datas.isStatus = true;
            msgHtml("该箱已完成装箱,需再次装箱请操作重新装箱 !");
        }
        datas.boxBillNoObj = data;
        datas.boxBillNoDisabled = true;
        if (datas.boxBillNoObj.id) {
            //根据箱信息去获取序列号列表数据
            getSerialNumberByBoxId(datas, formTableRef);
        }
    } else {
        msgHtml("请扫描系统存在的箱号 !");
    }
};
export const getBoxInfo = (datas: any, formTableRef: any) => {
    let boxCodeNumberClone = datas.boxBillNo;
    if (!boxCodeNumberClone) {
        msgHtml("请扫描系统存在的箱号 !");
        return;
    }
    //判断是否以CTN开头,先转换成大写再做对比
    let ctnStr = boxCodeNumberClone.substring(0, 3).toUpperCase();
    if (ctnStr !== "CTN") {
        msgHtml("请扫描系统存在的箱号 !");
        setTimeout(() => {
            datas.boxBillNo = "";
        }, 500);
        return;
    }
    //长度不够
    if (boxCodeNumberClone.length < 11) {
        msgHtml("请扫描系统存在的箱号 !");
        setTimeout(() => {
            datas.boxBillNo = "";
        }, 500);
        return;
    }

    //箱碼最小长度必须要11位
    if (boxCodeNumberClone.length >= 11) {
        //获取箱码信息
        getBoxByNo(datas, formTableRef);
    }
};
