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

<script setup lang="ts">
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> = {
    warehouseReceiptList: "InStock/Sync", //入库单列表
    inventoryList: "TakeStock/Sync", //盘点单列表
    outboundOrderList: "OutStock/Sync" //出库单列表
};

// 提交事件
const handleBtnClick = async () => {
    if (!props.selectionList.length) {
        useMsg("warning", "请选择需要重传的数据 ！");
        return false;
    }
    if (props.selectionList.length > 10) {
        useMsg("warning", "最多操作10条数据 ！");
        return false;
    }
    let some = props.selectionList.some((item: any) => {
        return item.successSync === "成功" || item.successSync === "--";
    });

    if (some) {
        useMsg("warning", "请选择同步结果为“同步失败和同步中”的数据进行重传 ！");
        return false;
    }
    let ids: any[] = [];
    props.selectionList.forEach((item: any) => {
        ids.push(item.id);
    });
    // 根据不同的路由进行不同的参数解构
    const PARAMSS: Record<string, any> = {
        outboundOrderList: [...ids],
        warehouseReceiptList: [...ids],
        inventoryList: [...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);
    }
};
//渲染按钮
const isBtnShow = computed(() => {
    const btnShowObj: any = {
        outboundOrderList: false,
        warehouseReceiptList: false,
        inventoryList: false
    };
    let btnName = routeName + "Retransmission";
    btnShowObj[routeName] = authStore.btnsAuthList.includes(btnName);
    return btnShowObj;
});
</script>

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