/*
 * @Description: 消息系统
 * @Author: sql
 * @Date: 2018-11-26 11:06:55
 * @LastEditTime: 2018-11-30 11:25:43
 */

class Pigeon {
  constructor(vue) {
    this.vue = vue
  }

  /**
   * 系统提醒
   * @param {string} msg 提醒信息
   * @param {int} type 0 信息， 1 成功， 2警告， 3 错误
   */
  message(msg, type = 0) {
    if (msg == '' || msg == null) {
      this.vue.$message.error('msg不能为空！！！')
      return false
    }
    switch (type) {
      case 0:
        this.vue.$message({ customClass: 'crm_message crm_msg_info', message: msg })
        break
      case 1:
        this.vue.$message.success({ customClass: 'crm_message crm_msg_suc', message: msg })
        break
      case 2:
        this.vue.$message.warning({
          customClass: 'crm_message crm_msg_war',
          message: msg,
        })
        break
      case 3:
        this.vue.$message.error({ customClass: 'crm_message crm_msg_err', message: msg })
        break
      default:
        this.vue.$message({ customClass: 'crm_message crm_msg_info', message: msg })
        break
    }
  }

  /**
   * 成功的系统提醒
   * @param {string} msg
   */
  MessageOK(msg) {
    this.message(msg, 1)
  }

  /**
   * 错误的系统提醒
   * @param {string} msg
   */
  MessageError(msg) {
    this.message(msg, 3)
  }

  /**
   * 警告的系统提醒
   * @param {string} msg
   */
  MessageWarning(msg) {
    this.message(msg, 2)
  }

  /**
   * 普通的系统提醒
   * @param {string} msg
   */
  MessageInfo(msg) {
    this.message(msg, 0)
  }

  /**
   * 确认消息
   * @param {string} msg 提示信息
   * @param {string} title 头部
   * @param {function} confirmHandle 确认回调函数
   * @param {function} cancelHandle 取消回调函数
   * @param {string} confirmText 确认按钮文字
   * @param {string} cancalText 取消按钮文字
   * @param {string} settings 其他配置项
   */
  MessageConfirm(msg, title, confirmText, cancalText, confirmHandle, cancelHandle, settings = {}) {
    this.vue
      .$confirm(msg, title, {
        confirmButtonText: confirmText,
        cancelButtonText: cancalText,
        customClass: 'crm_message_box',
        ...settings,
      })
      .then(({ value }) => {
        confirmHandle(value)
      })
      .catch(() => {
        cancelHandle()
      })
  }

  /**
   * 提交内容
   * @param {string} msg 提示信息
   * @param {function} confirmHandle 确认回调函数
   * @param {function} cancelHandle 取消回调函数
   * @param {string} confirmText 确认按钮文字
   * @param {string} cancalText 取消按钮文字
   * @param {string} pattern 提交内容正则
   * @param {string} errorMsg 提交内容错误提醒
   */
  MessagePrompt(
    msg,
    confirmHandle,
    cancelHandle,
    settings = {
      inputType: 'text',
      inputPattern: null,
      inputErrorMessage: null,
    }
  ) {
    this.vue
      .$prompt(msg, '系统提示', {
        confirmButtonText: settings.confirmButtonText || '确定',
        cancelButtonText: settings.cancelButtonText || '取消',
        ...settings,
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '执行中...'
            setTimeout(() => {
              done()
              setTimeout(() => {
                instance.confirmButtonLoading = false
              }, 100)
            }, 500)
          } else {
            instance.cancelButtonLoading = true
            instance.cancelButtonText = '执行中...'
            setTimeout(() => {
              done()
              setTimeout(() => {
                instance.cancelButtonLoading = false
              }, 100)
            }, 500)
          }
        },
      })
      .then(({ value }) => {
        confirmHandle(value)
      })
      .catch(() => {
        cancelHandle()
      })
  }
}

export default Pigeon
