import { Message, MessageBox } from "element-ui";
import Vue from "vue";
import { WaitingIf } from "@/services/waiting.service";
import waitingService from "@/services/waiting.service";

const AmUI: any = {};
AmUI.install = () => {
  /** toast **/
  Vue.prototype.$message = (msg: string) => {
    Message(msg);
  };
  Vue.prototype.$toast = (msg: any) => {
    Message(msg);
  };
  /** 提示对话框 **/
  Vue.prototype.$amAlert = (msg: string, title: string, callback?: Function) => {
    void MessageBox({
      title: title || "提示",
      message: msg || "",
      closeOnClickModal: false, //是否可通过点击遮罩关闭 MessageBox
      callback: action => {
        if (action == "confirm" && callback instanceof Function) {
          callback();
        }
      }
    });
  };
  /** 确认对话框 **/
  Vue.prototype.$amConfirm = (msg: string, title: string, sfn: Function, ffn: Function, btns: any = { confirm: "确定", cancel: "取消" }) => {
    void MessageBox({
      title: title || "确认提示",
      message: msg || "",
      confirmButtonText: btns.confirm,
      cancelButtonText: btns.cancel,
      showCancelButton: true,
      closeOnClickModal: false, //是否可通过点击遮罩关闭 MessageBox
      // center: true,
      callback: action => {
        if (action == "confirm") {
          if (sfn instanceof Function) sfn();
        } else {
          if (ffn instanceof Function) ffn();
        }
      }
    });
  };
  /** 提交对话框 **/
  Vue.prototype.$amPrompt = (msg: string, title: string, sfn: Function, ffn: Function, btns: any = { confirm: "确定", cancel: "取消" }) => {
    void MessageBox({
      title: title || "确认提示",
      message: msg || "",
      confirmButtonText: btns.confirm,
      cancelButtonText: btns.cancel,
      showCancelButton: true,
      closeOnClickModal: false, //是否可通过点击遮罩关闭 MessageBox
      // center: true,
      showInput: true,
      callback: action => {
        if (action == "confirm") {
          if (sfn instanceof Function) sfn();
        } else {
          if (ffn instanceof Function) ffn();
        }
        console.log(action);
      }
    });
  };
  /** 等待圈 */
  Vue.prototype.$amLoading = waitingService;
};
export default AmUI;

export interface AmMessageBoxShortcutMethod {
  (message: string, title: string, sfn: Function, ffn?: Function, options?: any): void;
  (message: string, title?: string, sfn?: Function): void;
  (message: any): void;
}

declare module "vue/types/vue" {
  interface Vue {
    /** Show an alert message box */
    $toast: AmMessageBoxShortcutMethod;

    /** Show an alert message box */
    $amAlert: AmMessageBoxShortcutMethod;

    /** Show a confirm message box */
    $amConfirm: AmMessageBoxShortcutMethod;

    /** Show a prompt message box */
    $amPrompt: AmMessageBoxShortcutMethod;

    $amLoading: WaitingIf;
  }
}
