import Confirm from '@/components/Common/confirm'
import Vue from 'vue'
import Vuetify from 'vuetify/lib'

/**
 *
 * @param {String} title
 * @param {String} msg
 * @param {Object} option 配置 { width, cancelCallback }
 * @returns
 */
Vue.prototype.$confirm = function (title, msg, option) {
  const opt = Confirm.data() // 获取组件中的默认配置
  return new Promise((resolve, reject) => {
    opt.title = title || opt.title
    opt.msg = msg || opt.msg
    Object.assign(opt.option, option || {})

    const ConfirmConstructor = Vue.extend(Confirm)

    const instance = new ConfirmConstructor({
      data: opt
    })

    // 这个得有，不然主题会出问题
    instance.$vuetify = new Vuetify().framework

    instance.vm = instance.$mount()

    document.body.appendChild(instance.vm.$el)
    instance.vm.show = true

    instance.sure = function () {
      resolve()
      instance.vm.show = false
      document.body.removeChild(instance.vm.$el)
    }
    instance.cancel = function () {
      // reject(new Error('cancel'))
      if (option?.cancelCallback) reject(new Error('cancel'))
      instance.vm.show = false
      document.body.removeChild(instance.vm.$el)
    }
  })
}
