import {ElMessage, ElMessageBox, ElLoading, ElNotification} from 'element-plus'

// @ts-ignore
let loading: ElLoading = null;
/**
 * 弹出消息
 */
export const Msg = {
    message(msg: string='', type: any = 'success') {
        this.hideLoading();
        // @ts-ignore
        ElMessage({
            showClose: true,
            message: msg,
            type: type
        })
    },
    confirm(message: string, title: string = '提示', options?: any,type='warning'): Promise<any> {
        // @ts-ignore
        return ElMessageBox.confirm(
            message,
            title,
            {
                distinguishCancelAndClose: true,
                confirmButtonText: options?.ok || '确认',
                cancelButtonText: options?.cancel || '取消',
                type: type,
                buttonSize:'default'
            }
        ).then((v) => {
            //console.log(v)
            return Promise.resolve();
        }).catch(e => {
            //console.error(e)
            return Promise.reject(e);
        })
    },
    prompt(message: string, title: string = '提醒', options?: any): Promise<any> {
        return ElMessageBox.prompt(message, title, {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            inputPattern: options?.pattern,
            inputErrorMessage: options?.error,
            buttonSize:'small',
            draggable:true
        })
    },
    showLoading(text: string = '加载中') {
        loading = ElLoading.service({text: text});
    },
    hideLoading() {
        if (loading) {
            loading.close();
        }
    },
    notify(message: string, title: string = '通知', options?: any) {
        ElNotification({
            title: title,
            message: message,
            duration: options?.duration || 3000,
            type: options?.type || 'info',
            position: options?.position || 'top-right'
        })
    }
};

