<template>
    <div class="common-btn-box font12" @click="handleBtnClick()">
        <span>合并</span>
    </div>
</template>

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

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

const emits = defineEmits<{
    (e: "handleChildBtnCallback", result: Record<string, any>): void;
}>();

// 接口地址映射
const URLS: Record<string, string> = {
    outboundTaskList: "OutStockTask/merge" //出库任务列表
};

// 提交事件
const handleBtnClick = async () => {
    const routeName: string = $router.currentRoute.value.name as string;
    if (!props.selectionList.length) {
        useMsg("warning", "请选择对应的数据后进行操作 ！");
        return;
    }
    let ids: number[] = [];
    props.selectionList.forEach((item: any) => {
        ids.push(item.id);
    });
    // 根据不同的路由进行不同的参数解构
    const PARAMSS: Record<string, any> = {
        outboundTaskList: [...ids]
    };
    // 内部处理好了请求后的数据再返回到父页面;
    const result = await commitListApi(URLS[routeName], { ids: PARAMSS[routeName] });
    if (result.status === 200) {
        useMsg("success", "合并成功 ！");
        emits("handleChildBtnCallback", {
            type: "Retransmission"
        });
    } else {
        useMsg("error", result.message);
    }
};
</script>

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