<template>
    <div :class="isGenerate ? 'common-btn-box2 font12' : 'common-btn-box font12'" @click="handleBtnClick()">
        <span>生成</span>
    </div>
</template>

<script setup lang="ts">
import { Decimal } from "decimal.js";
import { useMsg } from "@/hooks/useMsg";
import { commitListApi } from "@/api/modules/global";
const $router = useRouter();
// 接口地址映射
const URLS: Record<string, string> = {
    boxMarkDetails: "BoxMark/generate"
};
const props = defineProps<{
    selectionList?: any[];
    handleChildBtnCallback?: () => void;
    ruleForm: any;
    isGenerate: any;
    isNumberCode: any;
}>();
const emits = defineEmits<{
    (e: "handleChildBtnCallback", result: Record<string, any>): void;
}>();
//未开启数字区间号的验证消息
const MSGS: any = {
    orderBillNo: "订单号不能为空 ！",
    materialNumber: "规格型号不能空 ！",
    productQty: "产品数量不能为空 ！",
    cratingQty: "装箱数量不能为空 ！"
};
//开启数字区间号的验证消息
const MSGS1: any = {
    orderBillNo: "订单号不能为空 ！",
    materialNumber: "规格型号不能空 ！",
    productQty: "产品数量不能为空 ！",
    cratingQty: "装箱数量不能为空 ！",
    beginNumber: "数字区间号不能为空 ！"
};
//未开启数字区间号的验证消息-key
const MSGS_KEY: any = ["orderBillNo", "materialNumber", "productQty", "cratingQty"];
//开启数字区间号的验证消息-key
const MSGS_KEY1: any = ["orderBillNo", "materialNumber", "productQty", "cratingQty", "beginNumber"];
// 提交事件
const handleBtnClick = async () => {
    if (props.isGenerate) {
        return;
    }
    const routeName: string = $router.currentRoute.value.name as string;
    //验证
    for (let key in props.ruleForm) {
        if (!props.isNumberCode) {
            if (!props.ruleForm[key] && MSGS_KEY.includes(key)) {
                useMsg("warning", MSGS[key]);
                return;
            }
        } else {
            if (!props.ruleForm[key] && MSGS_KEY1.includes(key)) {
                useMsg("warning", MSGS1[key]);
                return;
            }
        }
    }
    //计算
    //产品数量
    let productQty: any = Number(props.ruleForm.productQty);
    //箱唛计算
    let boxMarkNumber: any = new Decimal(productQty).div(props.ruleForm.cratingQty).toString();
    //净重
    let cratingNetWeightQty: any = Number(props.ruleForm.cratingNetWeightQty);
    //毛重
    let cratingGrossWeightQty: any = Number(props.ruleForm.cratingGrossWeightQty);
    //装箱数量
    let cratingQty: any = Number(props.ruleForm.cratingQty);
    //箱唛个数
    let boxMarkNumberRulest: any = 0;
    //是否尾箱
    let mantissaIndex: any = 0;
    //尾箱净重
    let tailboxNetWeightQty: any = 0;
    //尾箱毛重
    let tailboxGrossWeightQty: any = 0;
    //尾箱数量
    let tailboxQty: any = 0;
    if (cratingGrossWeightQty < cratingNetWeightQty) {
        useMsg("warning", "毛重数量不能小于净重数量 !");
        return;
    }
    //如果箱唛个数有小数则向上取整为整数0.123为1 产品数量/装箱数量(不是整数则向上取整)
    boxMarkNumberRulest = boxMarkNumber.indexOf(".") ? Math.ceil(boxMarkNumber) : boxMarkNumber;
    //箱唛个数需要大于1才有尾箱
    //比如箱唛有3.5个,尾箱的下标就是4,尾箱的数量就是1
    if (boxMarkNumberRulest > 1) {
        //尾箱的下标 产品数量/装箱数量(不是整数向上取整的箱唛为尾箱) 计算方式和箱唛是一样的,所以直接取箱唛结果值即可
        mantissaIndex = boxMarkNumberRulest;
        //尾箱数量计算 产品数量- (装箱数量*箱唛个数(不是整箱则不计入，向下取整))
        if (mantissaIndex) {
            tailboxQty = productQty - cratingQty * Math.floor(boxMarkNumber);
        }

        //尾箱净重 尾数箱净重=净重/装箱数量*尾数箱数量
        if (mantissaIndex && cratingNetWeightQty) {
            tailboxNetWeightQty = (cratingNetWeightQty / cratingQty) * tailboxQty;
        }
        //尾箱毛重计算 尾数箱毛重=尾数箱净重+（毛重-净重）
        if (mantissaIndex) {
            tailboxGrossWeightQty = tailboxNetWeightQty + (cratingGrossWeightQty - cratingNetWeightQty);
        }
        if (tailboxQty === 0) {
            tailboxNetWeightQty = 0;
            tailboxQty = 0;
            tailboxGrossWeightQty = 0;
        }
    }

    //当装箱数量>产品数量
    let ruleFormClone: any = {};
    if (cratingQty > productQty) {
        cratingGrossWeightQty = cratingGrossWeightQty / (cratingQty / productQty);
        cratingNetWeightQty = cratingNetWeightQty / (cratingQty / productQty);
        ruleFormClone.cratingGrossWeightQty = cratingGrossWeightQty.toFixed(2);
        ruleFormClone.cratingNetWeightQty = cratingNetWeightQty.toFixed(2);
    }

    let params = {
        tailboxGrossWeightQty: tailboxGrossWeightQty ? Number(tailboxGrossWeightQty).toFixed(2) : 0,
        tailboxNetWeightQty: tailboxNetWeightQty ? Number(tailboxNetWeightQty).toFixed(2) : 0,
        tailboxQty: tailboxQty
    };
    let rulest = {
        ...props.ruleForm,
        ...params,
        ...ruleFormClone
    };
    //需要转换成number的值
    const NUMBER_ZH = [
        "productQty",
        "cratingQty",
        "cratingNetWeightQty",
        "cratingGrossWeightQty",
        "tailboxQty",
        "tailboxNetWeightQty",
        "tailboxGrossWeightQty",
        "beginNumber"
    ];
    for (let key in rulest) {
        if (NUMBER_ZH.includes(key)) {
            rulest[key] = rulest[key] ? Number(rulest[key]) : 0;
        }
    }
    const result = await commitListApi(URLS[routeName], { ...rulest });
    if (result.status === 200) {
        useMsg("success", "操作成功 ！");
        emits("handleChildBtnCallback", {
            type: "Generate",
            data: { ...params, ...ruleFormClone },
            printList: result.data
        });
    } else {
        useMsg("error", result.message);
    }
};
</script>

<style scoped>
@import "../index.scss";
</style>
@/api/modules/operationButtons
