<template>
  <transition name="dialog-fade" @after-leave="handleClosed">
    <div class="el-dialog__wrapper" v-show="visible">
      <div
        class="el-dialog"
        :class="[{ 'is-fullscreen': fullscreen, 'el-dialog--center': center }, customClass]"
        ref="dialog"
        :style="style">
        <div class="dlg-header">
          <slot name="title">
            <div class="dlg-titlebar">
              <div class="dlg-title">{{ title }}</div>
              <!-- <span class="dlg-close"
              aria-label="Close"
              v-if="showClose"
              @click="handleClose">x</span> -->
              <focus-icon name="delete"
              class="dlg-close"
              aria-label="Close"
              v-if="showClose"
              @click="handleClose"></focus-icon>
              <!-- <button
                type="button"
                class="el-dialog__headerbtn"
                aria-label="Close"
                v-if="showClose"
                @click="handleClose">
                <focus-icon name="delete"></focus-icon>
              </button> -->
            </div>
          </slot>
        </div>
        <div class="dlg-main" v-if="rendered">
          <div class="dlg-content">
            <slot></slot>
          </div>
        </div>
        <div class="dlg-footer" v-if="$slots.footer">
          <div class="dlg-buttons">
            <slot name="footer"></slot>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import Popup from 'element-ui-focus/src/utils/popup';
  import Migrating from 'element-ui-focus/src/mixins/migrating';
  import emitter from 'element-ui-focus/src/mixins/emitter';
  import FocusIcon from 'element-ui-focus/packages/icon';

  export default {
    name: 'FocusDialog',

    mixins: [Popup, emitter, Migrating],
    components: {
      FocusIcon
    },
    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: 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
      };
    },

    watch: {
      visible(val) {
        this.$emit('update: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');
        }
      }
    },

    computed: {
      style() {
        let style = {};
        if (this.width) {
          style.width = this.width;
        }
        if (!this.fullscreen) {
          style.marginTop = this.top;
        }
        return style;
      }
    },

    methods: {
      getMigratingConfig() {
        return {
          props: {
            'size': 'size is removed.'
          }
        };
      },
      handleWrapperClick() {
        if (!this.closeOnClickModal) return;
        this.handleClose();
      },
      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');
      },
      handleClosed(){
        this.$emit("closed");
      }
    },

    mounted() {
      if (this.visible) {
        this.rendered = true;
        this.open();
        if (this.appendToBody) {
          document.body.appendChild(this.$el);
        }
      }
    }
  };
</script>
