<template>
    <a-popconfirm :title="title" ok-text="确认" cancel-text="取消" @confirm="confirm">
        <a>{{ label }}</a>
    </a-popconfirm>
</template>

<script>
import { EMPTY_FUN } from '@/utils';
import { delReq, getReq, postReq, putReq } from '@/utils';

export default {
    props: {
        url: [String, Object],
        cb: {
            type: Function,
            default: EMPTY_FUN,
        },
        title: {
            type: String,
            default: '确认是否删除',
        },
        onOk: {
            type: Function,
            default: EMPTY_FUN,
        },
        label: {
            type: String,
            default: '删除',
        },
    },
    methods: {
        async confirm() {
            if (typeof this.url === 'string') {
                try {
                    await delReq(this.url);
                } catch (error) {
                    return;
                }
            }
            if (typeof this.url === 'object') {
                switch (this.url.method) {
                    case 'get':
                        await getReq(this.url.url, this.url?.data);
                        break;
                    case 'post':
                        await postReq(this.url.url, this.url?.data);
                        break;
                    case 'put':
                        await putReq(this.url.url, {}, { data: this.url?.data });
                        break;
                    case 'del':
                        await delReq(this.url.url, {}, { data: this.url?.data });
                        break;
                }
            }
            if (this.onOk) {
                await this.onOk();
            }
            this?.cb();
        },
    },
};
</script>
