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

<script setup lang="ts">
import { ElMessageBox } from "element-plus";
import { useMsg } from "@/hooks/useMsg";
import { commitListApi } from "@/api/modules/global";
const $router = useRouter();
// 接口地址映射
const URLS: Record<string, string> = {
    boxMarkIndex: "BoxMark/delete", //入库任务列表
    subscriptionIndex: "SubscribeNotification/Delete" //订阅列表
};
const props: any = defineProps<{
    selectionList?: any[];
    handleChildBtnCallback?: () => void;
    ruleForm: any;
}>();
const emits = defineEmits<{
    (e: "handleChildBtnCallback", result: Record<string, any>): void;
}>();

// 提交事件
const handleBtnClick = () => {
    if (!props.selectionList.length) {
        useMsg("warning", "请选择需要删除的数据 ！");
        return;
    }
    let ids: number[] = [];
    props.selectionList.forEach((item: any) => {
        ids.push(item.id);
    });
    ElMessageBox.confirm("您确定进行删除操作吗?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
    })
        .then(async () => {
            const routeName: string = $router.currentRoute.value.name as string;
            const result = await commitListApi(URLS[routeName], { ids: ids });
            if (result.status === 200) {
                useMsg("success", "删除成功 ！");
                emits("handleChildBtnCallback", {
                    type: "Del"
                });
            } else {
                useMsg("error", result.message);
            }
        })
        .catch(() => {});
};
</script>

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