<template>
  <top-tip ref="topTip"></top-tip>
</template>

<script>
  import Vue from 'vue'
  import {
    AlertPlugin, ConfirmPlugin, ToastPlugin, LoadingPlugin
  } from 'vux';

  Vue.use(AlertPlugin);
  Vue.use(ConfirmPlugin);
  Vue.use(ToastPlugin);
  Vue.use(LoadingPlugin);
  import {Layer} from "../math/layer";
  import TopTip from "./toptip";

  export default {
    name: "layerInit",
    props: {
      alert: {
        type: Boolean,
        default: true
      },

      error: {
        type: Boolean,
        default: true
      },
      success: {
        type: Boolean,
        default: true
      },
      toast: {
        type: Boolean,
        default: true
      },
      loading: {
        type: Boolean,
        default: true
      },
      confirm: {
        type: Boolean,
        default: true
      },
    },
    components: {
      TopTip,
    },

    data() {
      return {}
    },

    created() {
      if (this.alert) {
        Layer.prototype.alert = (msg, title = '出错啦') => {
          const _this = this;
          return new Promise(function (resolve, reject) {
            _this.$vux.alert.show({
              title: title,
              content: msg,
              'mask-z-index':99999,
              onHide() {
                resolve();
              }
            });
          });
        };
      }

      if (this.loading) {

        Layer.prototype.loading = (text = '数据加载中') => {
          this.$vux.loading.show({
            text: text
          });
        };
        Layer.prototype.hideLoading = () => {
          this.$vux.loading.hide()
        };
      }

      if (this.error) {
        Layer.prototype.error = Layer.prototype.toptip = (msg, type = 'error', closeTime = 3000) => {
          this.$refs.topTip.show(msg, type, closeTime);
        };
      }

      if (this.confirm) {
        Layer.prototype.confirm = (msg, title = '操作确认') => {
          const _this = this;
          return new Promise(function (resolve, reject) {
            _this.$vux.confirm.show({
              title: title,
              content: msg,
              onCancel() {
                reject();
              },
              onConfirm() {
                resolve();
              }
            })
          });
        };
      }

      if (this.toast) {
        Layer.prototype.toast = (msg = '操作成功', type = '', time = 2000) => {
          const _this = this;
          return new Promise(function (resolve, reject) {
            _this.$vux.toast.show({
              type,
              time,
              text: msg,
              onHide: function () {
                resolve()
              }
            });
          });
        };
      }

      if (this.success) {
        Layer.prototype.success = function (msg) {
          this.toptip(msg, 'success')
        };
      }
    },
    methods: {}
  }
</script>

<style scoped>

</style>
