<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> = {
    ofInboundInstructionsList: "InStockTask/Refresh", //入库任务列表
    outboundTaskList: "OutStockTask/Refresh" //出库任务列表
};

// 提交事件
const handleBtnClick = async () => {
    let ids: number[] = [];
    props.selectionList.forEach((item: any) => {
        ids.push(item.id);
    });
    // 根据不同的路由进行不同的参数解构
    const PARAMSS: Record<string, any> = {
        ofInboundInstructionsList: [...ids],
        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);
    }
};
//渲染按钮
const isBtnShow = computed(() => {
    const btnShowObj: any = {
        ofInboundInstructionsList: false,
        outboundTaskList: false
    };
    let btnName = routeName + "Refresh";
    btnShowObj[routeName] = authStore.btnsAuthList.includes(btnName);
    return btnShowObj;
});
</script>

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