<template>
    <div class="common-btn-box font12" @click="handleBtnClick()" v-if="isBtnShow[routeName]">
        <span>反作废</span>
    </div>
</template>

<script setup lang="ts">
import { ElMessageBox } from "element-plus";
import { useMsg } from "@/hooks/useMsg";
import { commitListApi } from "@/api/modules/global";
import { useAuthStore } from "@/stores/modules/auth";
const authStore = useAuthStore();
const $router = useRouter();
const routeName: string = $router.currentRoute.value.name as string;
const props = defineProps<{
    selectionList: any[];
    handleChildBtnCallback?: () => void;
}>();

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

// 接口地址映射
const URLS: Record<string, string> = {
    outboundTaskList: "OutStockTask/NoRepeal", //出库任务列表
    ofInboundInstructionsList: "InStockTask/NoRepeal" //入库任务列表
};
const WARNS: Record<string, string> = {
    outboundTaskList: "出库", //出库任务列表
    ofInboundInstructionsList: "入库" //入库任务列表
};
// 提交事件
const handleBtnClick = async () => {
    console.log(props.selectionList, "=props.selectionList=");
    if (!props.selectionList.length) {
        useMsg("warning", "请选择对应的数据后进行操作 ！");
        return;
    }

    console.log(props.selectionList.length, "=props.selectionList.length=");
    ElMessageBox.confirm(`确认反作废${WARNS[routeName]}数据且金蝶对应单据已处理?`, "操作提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
    })
        .then(async () => {
            let ids: number[] = [];
            props.selectionList.forEach((item: any) => {
                if (item.detailsId) {
                    ids.push(item.detailsId);
                }
                if (item.detailId) {
                    ids.push(item.detailId);
                }
            });

            // 根据不同的路由进行不同的参数解构
            const PARAMSS: Record<string, any> = {
                ofInboundInstructionsList: [...ids],
                outboundTaskList: [...ids]
            };
            console.log(URLS[routeName], "=URLS[routeName]=");
            // 内部处理好了请求后的数据再返回到父页面;
            const result = await commitListApi(URLS[routeName], { ids: PARAMSS[routeName] });
            if (result.status === 200) {
                useMsg("success", "操作成功，对应数据已反作废 ！");
                emits("handleChildBtnCallback", {
                    type: "ToVoid"
                });
            } else {
                useMsg("error", result.message);
            }
        })
        .catch(() => {
            // ElMessage({
            //     type: "info",
            //     message: "发生未知错误，请稍后再进行操作 !"
            // });
        });
};
//渲染按钮
const isBtnShow = computed(() => {
    const btnShowObj: any = {
        outboundTaskList: false,
        ofInboundInstructionsList: false
    };
    let btnName = routeName + "NoToVoid";
    btnShowObj[routeName] = authStore.btnsAuthList.includes(btnName);
    return btnShowObj;
});
</script>

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