<template>
    <div class="common-btn-box font12" @click="handleBtnClick()">
        <span>转两件装</span>
    </div>
</template>

<script setup lang="ts">
import { ElMessageBox } from "element-plus";
import { useMsg } from "@/hooks/useMsg";
import { getTransferSnApi, getTransferSnJCApi } from "@/api/modules/barCode";
// const $router = useRouter();
import { WarningFilled } from "@element-plus/icons-vue";
const props: any = defineProps<{
    selectionList?: any[];
    handleChildBtnCallback?: () => void;
    ruleForm: any;
}>();
const emits = defineEmits<{
    (e: "handleChildBtnCallback", result: Record<string, any>): void;
}>();

const getTransferSn = (ids: any) => {
    ElMessageBox.confirm("确定数据转换为两件装, 一旦转换不可回退!", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
    })
        .then(async () => {
            const result: any = await getTransferSnApi({ ids: ids });
            if (result.status === 200) {
                useMsg("success", "操作成功 ！");
                emits("handleChildBtnCallback", {
                    type: "Convert"
                });
            } else {
                useMsg("error", result.message);
            }
        })
        .catch(() => {});
};
//防止xss攻击
// const escapeHtml = (unsafe: any) => {
//     return unsafe
//         .replace(/&/g, "&amp;")
//         .replace(/</g, "&lt;")
//         .replace(/>/g, "&gt;")
//         .replace(/"/g, "&quot;")
//         .replace(/'/g, "&#039;");
// };
// 提交事件
const handleBtnClick = async () => {
    if (!props.selectionList.length) {
        useMsg("warning", `请选择需要转换的数据 ！`);
        return;
    }
    if (props.selectionList.length > 50) {
        useMsg("warning", `请选择50条以内的数据进行转换 ！`);
        return;
    }
    // let isSome = props.selectionList.filter((item: any) => {
    //     return item.useNumber;
    // });
    // let isNumber = props.selectionList.filter((item: any) => {
    //     return item.number % 2 === 1;
    // });
    // let specificationsArr: any = [];
    // let specificationsArr1: any = [];
    // //多种条件
    // if (isSome.length && isNumber.length) {
    //     //单个
    //     isSome.forEach((item: any) => {
    //         specificationsArr.push(item.specifications);
    //     });
    //     specificationsArr = [...new Set(specificationsArr)];
    //     let specifications = specificationsArr.join(",");

    //     //单数
    //     isNumber.forEach((item: any) => {
    //         specificationsArr1.push(item.specifications);
    //     });
    //     specificationsArr = [...new Set(specificationsArr1)];
    //     let specifications1 = specificationsArr1.join(",");
    //     // useMsg("warning", `${specifications1}条码个数为单数, 不支持转两件装 ！`);
    //     useMsg(
    //         "warning",
    //         `${specifications}序列码存在已使用, 请选择未使用序列码的数据; ${specifications1}条码个数为单数, 不支持转两件装 ！`
    //     );
    //     return;
    // }
    // //单个使用
    // if (isSome.length) {
    //     isSome.forEach((item: any) => {
    //         specificationsArr.push(item.specifications);
    //     });
    //     specificationsArr = [...new Set(specificationsArr)];
    //     let specifications = specificationsArr.join(",");
    //     useMsg("warning", `${specifications}序列码存在已使用, 请选择未使用序列码的数据 ！`);
    //     return;
    // }
    // //单数转换
    // if (isNumber.length) {
    //     isNumber.forEach((item: any) => {
    //         specificationsArr1.push(item.specifications);
    //     });
    //     specificationsArr1 = [...new Set(specificationsArr1)];
    //     let specifications1 = specificationsArr1.join(",");
    //     useMsg("warning", `${specifications1}条码个数为单数, 不支持转两件装 ！`);
    //     // return;
    // }

    let ids: any[] = [];
    props.selectionList.forEach((item: any) => {
        //只传一件装数量不为0的
        if (item.isTwo === 1 && item.number) {
            ids.push({
                id: item.id,
                specifications: item.specifications
            });
        }
    });
    const result: any = await getTransferSnJCApi({ ids: ids });
    if (result.status === 200) {
        getTransferSn(ids);
    } else {
        ElMessage({
            showClose: true,
            dangerouslyUseHTMLString: true,
            message: `<div style="line-height:1.5;max-width:800px;max-height: 800px;
    overflow-y: auto; color:red">${result.msg || result.message}</div>`,

            icon: WarningFilled,
            customClass: "custom-message-error"
            // type: "error"
            // duration: 100000
        });
    }
};
</script>

<style lang="scss">
@import "../index.scss";
.custom-message-error {
    // color: red;
    background: var(--el-color-error-light-9) !important;
    border-color: var(--el-color-error-light-8) !important;
}
.custom-message-error > .el-message-icon--info {
    color: red;
}

// .custom-message-error > .el-message__closeBtn {
//     color: #ffffff;
// }

// .el-message {
//     width: 800px;
//     height: 800px;
//     overflow-y: auto;
// }
// .custom-message-error > i {
//     color: red !important;

//     // background-color: var(--el-color-error-light-9) !important;
//     // border-color: var(--el-color-error-light-8) !important;

//     // -el-message-bg-color: var(--el-color-error-light-9);

//     // --el-message-border-color: var(--el-color-error-light-8);
//     // --el-message-text-color: var(--el-color-error);
// }
</style>
