"use strict";

import { util } from "../../util/util.js";

class khcAlert {
    /**
     * @param data 可以是字符串或空字符串，也可以是obj对象：{title:'标题',text:'内容'}
     * @param callback 回调函数
     */
    static showDelConfirm(data,callback) {
        data=this.toData(data);
        data.title=!util.isNull(data.title)?data.title:'请确认是否删除';
        data.text=!util.isNull(data.text)?data.text:'数据删除后将不可恢复，确认您是否删除数据';
        data.icon=!util.isNull(data.icon)?data.icon:'warning';
        data.showCancelButton=!util.isNull(data.showCancelButton)?data.showCancelButton:true;
        data.confirmButtonColor=!util.isNull(data.confirmButtonColor)?data.confirmButtonColor:'#DC143C';
        data.cancelButtonColor=!util.isNull(data.cancelButtonColor)?data.cancelButtonColor:'#3085d6';
        data.confirmButtonText=!util.isNull(data.confirmButtonText)?data.confirmButtonText:'删除';
        data.allowOutsideClick=!util.isNull(data.allowOutsideClick)?data.allowOutsideClick:false//删除操作不允许点击其他区域关闭模态框
        data.preConfirm=data.preConfirm!=undefined?data.preConfirm:null;
        khcAlert.show(data,callback);
    }
    static showConfirm(data,callback) {
        data=this.toData(data);
        data.icon=!util.isNull(data.icon)?data.icon:'warning';
        data.showCancelButton=!util.isNull(data.showCancelButton)?data.showCancelButton:true;
        data.confirmButtonColor=!util.isNull(data.confirmButtonColor)?data.confirmButtonColor:'#DC143C';
        data.cancelButtonColor=!util.isNull(data.cancelButtonColor)?data.cancelButtonColor:'#3085d6';
        data.confirmButtonText=!util.isNull(data.confirmButtonText)?data.confirmButtonText:'确定';
        data.allowOutsideClick=!util.isNull(data.allowOutsideClick)?data.allowOutsideClick:false;//确认操作不允许点击其他区域关闭模态框
        data.preConfirm=data.preConfirm!=undefined?data.preConfirm:null;
        khcAlert.show(data,callback);
    }
    static show(data, callback) {
        Swal.fire({
            icon: data.icon,
            title: data.title,//标题
            text: data.text,//显示内容
            showCancelButton: data.showCancelButton,//是否展示关闭按钮
            confirmButtonColor: data.confirmButtonColor,//确认按钮颜色
            cancelButtonColor: data.cancelButtonColor,//关闭按钮颜色
            confirmButtonText: data.confirmButtonText,//确认框按钮文名
            allowOutsideClick: data.allowOutsideClick,//是否允许对话框以外的点击关闭
            showLoaderOnConfirm: true,//是否点击确认框时展示加载样式---暂时写死都显示加载样式有需求时再加入到配置里（注：需搭配 preConfirm函数）
            preConfirm:data.preConfirm,//在执行confirm按钮之前执行该函数，如果返回false为阻止弹窗关闭，undefined为默认结果，也可以返回其他值，可以通过result.value获取对应的值
            showClass: {
                popup: 'animate__animated animate__fadeInDown'
            },
            hideClass: {
                popup: 'animate__animated animate__fadeOutUp'
            }
        }).then((result) => {
            if (result.value) {
                if (callback != undefined) {
                    callback();
                }
            }
        })
    }

    /**
     * @param data 可以是字符串，也可以是obj对象：{title:'标题',text:'内容'}
     * @param callback 回调函数
     */
    static showSystemError(data,callback) {
        data=this.toData(data);
        if(util.isNull(data.text)){
            data.text = '系统错误，请与系统管理员联系！'
        }
        data.icon = 'error';
        data.showCancelButton = false;
        data.cancelButtonColor = '#d33';
        data.confirmButtonColor = '#3085d6';
        data.confirmButtonText = '确定';
        data.allowOutsideClick=!util.isNull(data.allowOutsideClick)?data.allowOutsideClick:true;//错误提示允许点击其他区域关闭模态框
        data.preConfirm=data.preConfirm!=undefined?data.preConfirm:null;
        khcAlert.show(data,callback);
    }
    static toData(data){
        if(typeof data == 'string'){
            return {title:data}
        }else{
            return data;
        }
    }
    /**
     * @param data 可以是字符串，也可以是obj对象：{title:'标题',text:'内容'}
     * @param callback 回调函数
     */
    static showWarningMessage(data,callback) {
        data=this.toData(data);
        if(util.isNull(data.text)){
            data.text = ''
        }
        data.icon = 'warning';
        data.showCancelButton = false;
        data.cancelButtonColor = '#d33';
        data.confirmButtonColor = '#3085d6';
        data.confirmButtonText = '确定';
        data.allowOutsideClick=!util.isNull(data.allowOutsideClick)?data.allowOutsideClick:true;//警告,允许点击其他区域关闭模态框
        data.preConfirm=data.preConfirm!=undefined?data.preConfirm:null;
        khcAlert.show(data,callback);
    }
    static showSuccessMessage(title,showTime) {
        if(util.isNull(showTime)||showTime==0){
            showTime=1000;
        }
        Swal.fire({
            icon: 'success',
            title: title,
            timer: showTime,
            timerProgressBar: true,
            confirmButtonText: '确定',
            confirmButtonColor: '#3085d6',
            didOpen: () => {
                Swal.showLoading()
            },
        }).then((result) => {
            if (result.dismiss === Swal.DismissReason.timer) {
                //true 计时器关闭
            }
        })

    }
}
export { khcAlert }