<template>
  <transition
    name="dialog-fade"
    @after-enter="afterEnter"
    @after-leave="afterLeave">
    <div class="el-dialog__wrapper" v-show="visible" @click.self="handleWrapperClick">
      <div
        class="el-dialog"
        :class="[{ 'is-fullscreen': fullStatus, 'el-dialog--center': center }, customClass]"
        ref="dialog"
        :style="style">
        <div class="el-dialog__header">
          <slot name="title">
            <span class="el-dialog__title">{{ title }}</span>
          </slot>
          <!--  -->
          <button
            type="button"
            style="color: rgb(153, 153, 153);background: 0px 0px;position: absolute;outline: 0px;cursor: pointer;font-size: 16px;width: 28px;height: 28px;top: 0px;line-height: 1; border-radius: 28px;border: 1px solid #DCDCDC;right: 0;margin: 13px 80px;text-align: center;"
            :class="['Special', SpecChar && 'SpecChar']"
            aria-label="Special"
            v-if="!showFull && headerInstance"
            @click="keyboardBtn">
            <i class="el-icon-qz-iconoumu elfont" :class="[keyboardOpen==false ? 'keyboard_btn_open':'keyboard_btn_close']"></i>
          </button>
          <button
            type="button"
            class="el-dialog__headerbtn"
            aria-label="Close"
             v-if="!showFull && showClose"
            @click="handleClose">
            <i class="el-dialog__close el-icon el-icon-close"></i>
          </button>
          <!--  -->
          <div class="el-dialog__tools" v-if="showFull">
            <button
              type="button"
              :class="['Special', SpecChar && 'SpecChar']"
              aria-label="Special"
              @click="keyboardBtn"
              v-if="headerInstance"
            >
              <i class="el-icon-qz-iconoumu elfont" :class="[keyboardOpen==false ? 'keyboard_btn_open':'keyboard_btn_close']"></i>
            </button>
            <button
              type="button"
              aria-label="FullScreen"
              @click="handleFull"
              v-if="showFull"
            >
              <i class="el-icon el-icon-rank"></i>
            </button>
            <button
              type="button"
              aria-label="Close"
              @click="handleClose"
              v-if="showClose"
            >
              <i class="el-dialog__close el-icon el-icon-close"></i>
            </button>
          </div>
        </div>
        <div class="el-dialog__body" v-if="rendered"><slot></slot></div>
        <div class="el-dialog__footer" v-if="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import Popup from 'element-ui-qz/src/utils/popup';
  import Migrating from 'element-ui-qz/src/mixins/migrating';
  import emitter from 'element-ui-qz/src/mixins/emitter';

  export default {
    name: 'ElDialog',

    mixins: [Popup, emitter, Migrating],

    props: {
      title: {
        type: String,
        default: ''
      },

      modal: {
        type: Boolean,
        default: true
      },

      modalAppendToBody: {
        type: Boolean,
        default: true
      },

      appendToBody: {
        type: Boolean,
        default: false
      },

      lockScroll: {
        type: Boolean,
        default: true
      },

      closeOnClickModal: {
        type: Boolean,
        default: true
      },

      closeOnPressEscape: {
        type: Boolean,
        default: true
      },

      showFull: {
        type: Boolean,
        default: false
      },

      showClose: {
        type: Boolean,
        default: true
      },

      width: String,

      fullscreen: Boolean,

      customClass: {
        type: String,
        default: ''
      },

      top: {
        type: String,
        default: '15vh'
      },
      beforeClose: Function,
      center: {
        type: Boolean,
        default: false
      }
    },

    data() {
      return {
        closed: false,
        headerInstance: '',
        fullStatus: false
      };
    },

    watch: {
      visible(val) {
        if (val) {
          this.closed = false;
          this.$emit('open');
          this.$el.addEventListener('scroll', this.updatePopper);
          this.$nextTick(() => {
            this.$refs.dialog.scrollTop = 0;
          });
          if (this.appendToBody) {
            document.body.appendChild(this.$el);
          }
        } else {
          this.$el.removeEventListener('scroll', this.updatePopper);
          if (!this.closed) this.$emit('close');
        }
      },
      fullscreen: {
        handler(value) {
          this.fullStatus = value;
        },
        deep: true,
        immediate: true
      }
    },

    computed: {
      style() {
        let style = {};
        if (!this.fullStatus) {
          style.marginTop = this.top;
          if (this.width) {
            style.width = this.width;
          }
        }
        return style;
      },
      keyboardOpen() {
        return this.headerInstance && this.headerInstance.keyboardOpen;
      },
      SpecChar() {
        return localStorage.getItem('SpecChar') === 'true';
      }
    },

    methods: {
      getMigratingConfig() {
        return {
          props: {
            'size': 'size is removed.'
          }
        };
      },
      handleWrapperClick() {
        if (!this.closeOnClickModal) return;
        this.handleClose();
      },
      keyboardBtn() {
        this.headerInstance.keyboardBtn();
      },
      handleFull() {
        this.fullStatus = !this.fullStatus;
        this.$emit('handleFull', this);
      },
      handleClose() {
        if (typeof this.beforeClose === 'function') {
          this.beforeClose(this.hide);
        } else {
          this.hide();
        }
      },
      hide(cancel) {
        if (cancel !== false) {
          this.$emit('update:visible', false);
          this.$emit('close');
          this.closed = true;
        }
      },
      updatePopper() {
        this.broadcast('ElSelectDropdown', 'updatePopper');
        this.broadcast('ElDropdownMenu', 'updatePopper');
      },
      afterEnter() {
        this.$emit('opened');
      },
      afterLeave() {
        this.$emit('closed');
      }
    },

    mounted() {
      if (this.visible) {
        this.rendered = true;
        this.open();
        if (this.appendToBody) {
          document.body.appendChild(this.$el);
        }
      }
      let _dom = document.getElementById('header-mine-new') || document.getElementById('headerMine');
      this.headerInstance = _dom && _dom.__vue__;
    },

    destroyed() {
      // if appendToBody is true, remove DOM node after destroy
      if (this.appendToBody && this.$el && this.$el.parentNode) {
        this.$el.parentNode.removeChild(this.$el);
      }
    }
  };
</script>
