<template>
    <a-popconfirm
        :title="title || $t('table.confirmDelete')"
        :ok-text="$t('table.confirm')"
        :cancel-text="$t('table.cancel')"
        @confirm="confirm"
    >
        <a>{{ label || $t('table.delete') }}</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,
        },
        onOk: {
            type: Function,
            default: EMPTY_FUN,
        },
        label: {
            type: String,
        },
    },
    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>
