<template>
    <div class="common-btn-box font12" @click="handleBtnClick()">
        <span>{{ title }}</span>
    </div>
</template>

<script setup lang="ts">
import { useMsg } from "@/hooks/useMsg";
import { operationButtonsApi } from "@/api/modules/operationButtons";
const $router = useRouter();

const props = defineProps<{
    selectionList: any[];
}>();

// 接口地址映射
const URLS: Record<string, string> = {
    antiCode: "SecurityNumber/ExportAll",
    barCode: "SerialNumber/ExportAll"
};
//路由名称(根据不同的路由可以做不同的操作)
let name = ref<any>($router.currentRoute.value.name);
//按钮名称
const title: any = computed(() => {
    const TITLES: any = { antiCode: "下载防伪码", barCode: "下载条码" };
    return TITLES[name.value];
});
// 提交事件
const handleBtnClick = async () => {
    let ids: any[] = [];

    if (!props.selectionList.length) {
        useMsg("warning", "请选择要下载的数据 !");
        return;
    }

    let isTwo1 = props.selectionList.some((item: any) => {
        return item.isTwo == 1;
    });
    let isTwo2 = props.selectionList.some((item: any) => {
        return item.isTwo == 2;
    });

    if (isTwo1 && isTwo2) {
        useMsg("warning", "数据含有单件装和两件装条码, 请分开选择数据进行下载 !");
        return;
    }

    let isTwo = props.selectionList[0].isTwo;
    props.selectionList.forEach((item: any) => {
        ids.push(item.id);
    });
    let params: any = {
        antiCode: { ids },
        barCode: { ids, isTwo }
    };
    const result = await operationButtonsApi(URLS[name.value], params[name.value]);
    if (result.status === 200 && result.isSuccess) {
        useMsg("success", "导出成功请前往导出列表中进行下载 ！");
    } else {
        useMsg("warning", "导出失败,请稍后重试 ！");
    }
};
</script>

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