import ConfirmComp from './Index.vue'
const Confirm = {};
Confirm.install = function (Vue) {
    const ConfirmConstructor = Vue.extend(ConfirmComp);
    const instance = new ConfirmConstructor();
    instance.$mount(document.createElement('div'));
    document.body.appendChild(instance.$el);
    // 通过Vue的原型注册一个方法
    // 让所有实例共享这个方法
    var watcher=null;
    Vue.prototype.$confirm = ({title='提示',msg='该操作不可逆，是否确认？',type='warning'}) => {
        if(watcher){
            watcher();
            watcher=null;
        }
        instance.title=title;
        instance.msg = msg;
        instance.type = type;
        instance.visible = true;
        instance.flag=0;
        return new Promise(function(resolve,reject){
            watcher=instance.$watch('flag',function(newValue,oldValue){
                if(newValue==0){
                    return;
                }
                resolve(newValue>0);
            })
        })
    };
}
export default Confirm;