<template>
  <transition name="el-message-fade" @after-leave="handleAfterLeave">
    <div
      :class="[
        'el-message',
        type && !iconClass ? `el-message--${ type }` : '',
        center ? 'is-center' : '',
        showClose ? 'is-closable' : '',
        customClass
      ]"
      :style="positionStyle"
      v-show="visible"
      @mouseenter="clearTimer"
      @mouseleave="startTimer"
      role="alert">
     <div class="el-message__content">
        <div class="header">
          <i :class="iconClass" v-if="iconClass"></i>
          <i :class="typeClass" v-else></i>
          <p v-if="!dangerouslyUseHTMLString">{{ message }}</p>
          <p v-else-if="dangerouslyUseHTMLString" v-html="message"></p>
          <i v-if="showClose" class="el-message__closeBtn el-icon-close" @click="close"></i>
          <span v-if="!showClose && detailRight" @click="detail">查看详情</span>
        </div>
        <div class="content" v-if="bodyVisible || detailBottom">
          <p v-if="bodyVisible">{{content}}</p>
          <span v-if="detailBottom" @click="detail">查看详情</span>
        </div>
     </div>
    </div>
  </transition>
</template>

<script type="text/babel">
  const typeMap = {
    success: 'chenggong1',
    info: 'zhengchang',
    warning: 'weishimingrenzheng',
    error: 'shibai'
  };

  export default {
    data() {
      return {
        content: '',
        bodyVisible: false,
        detailBottom: false,
        detailRight: false,
        visible: false,
        message: '',
        duration: 3000,
        type: 'info',
        iconClass: '',
        customClass: '',
        onClose: null,
        showClose: false,
        closed: false,
        verticalOffset: 60,
        timer: null,
        dangerouslyUseHTMLString: false,
        center: false,
        checkDetail: null
      };
    },

    computed: {
      typeClass() {
        return this.type && !this.iconClass
          ? `el-message__icon ccxd-icon-${ typeMap[this.type] }`
          : '';
      },
      positionStyle() {
        return {
          'top': `${ this.verticalOffset }px`
        };
      }
    },

    watch: {
      closed(newVal) {
        if (newVal) {
          this.visible = false;
        }
      }
    },

    methods: {
      detail() {
        if (typeof this.checkDetail === 'function') this.checkDetail(this);
        else return;
      },

      handleAfterLeave() {
        this.$destroy(true);
        this.$el.parentNode.removeChild(this.$el);
      },

      close() {
        this.closed = true;
        if (typeof this.onClose === 'function') {
          this.onClose(this);
        }
      },

      clearTimer() {
        clearTimeout(this.timer);
      },

      startTimer() {
        if (this.duration > 0) {
          this.timer = setTimeout(() => {
            if (!this.closed) {
              this.close();
            }
          }, this.duration);
        }
      },
      keydown(e) {
        if (e.keyCode === 27) { // esc关闭消息
          if (!this.closed) {
            this.close();
          }
        }
      }
    },
    mounted() {
      this.startTimer();
      document.addEventListener('keydown', this.keydown);
    },
    beforeDestroy() {
      document.removeEventListener('keydown', this.keydown);
    }
  };
</script>
