<template>
  <transition
    name="dialog-fade">
    <div class="win-dialog__wrapper" v-show="visible" @click.self="handleWrapperClick">
      <div
        class="win-dialog"
        :style="style"
        ref="dialog">
        <div class="win-dialog__header">
          <slot name="title">
            <span class="win-dialog__title">{{title}}</span>
          </slot>
          <span class="win-dialog__close" @click="handleClose"></span>
        </div>
        <div class="win-dialog__body"><slot></slot></div>
        <div class="win-dialog__footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  name: 'win-dialog',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    width: String,
    title: {
      type: String,
      default: '',
    },
    closeOnClickModal: {
      type: Boolean,
      default: true,
    },
    fullscreen: Boolean,
    top: {
      type: String,
      default: '15vh',
    },
  },
  data() {
    return {
      closed: false,
    };
  },
  watch: {
    visible(val) {
      if (val) {
        this.$winDialog.addWinModal();
      } else {
        this.$winDialog.removeModal();
      }
    },
  },
  computed: {
    style() {
      const style = {};
      if (!this.fullscreen) {
        style.marginTop = this.top;
        if (this.width) {
          style.width = this.width;
        }
      }
      return style;
    },
  },
  methods: {
    handleWrapperClick() {
      this.handleClose();
    },
    handleClose() {
      this.hide();
    },
    hide() {
      this.$emit('update:visible', false);
    },
  },
  mounted() {
    if (this.visible) {
      this.$winDialog.addWinModal();
    } else {
      this.$winDialog.removeModal();
    }
  },
  destroyed() {
  },
};
</script>
<style scoped>
  .win-dialog__wrapper{
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: auto;
    margin: 0;
    z-index: 2001;
  }
  .win-dialog{
    position: relative;
    margin: 0 auto 50px;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,.3);
    box-sizing: border-box;
    width: 50%;
  }
  .win-dialog__header{
    position: relative;
    height: 81px;
    text-align: center;
    background: #F5F5F5;
    border: 1px solid #E9EFF4;
    border-radius: 4px 4px 0 0;
  }
  .win-dialog__title{
    line-height: 81px;
    font-size: 24px;
    color: #333333;
  }
  .win-dialog__close{
    display: inline-block;
    position: absolute;
    right: 25px;
    top: 30px;
    width: 20.3px;
    height: 20.3px;
    cursor: pointer;
    background: url("../../../static/images/icon-delete.svg") no-repeat center;
  }
  .win-dialog__body{
    min-height: 100px;
  }
  .dialog-fade-enter-active{
    transition: all 0.5s ease;
  }
  .dialog-fade-leave-active{
    transition: all 0.5s cubic-bezier(.42, 0, .58, 1);
  }
  .dialog-fade-enter, .dialog-fade-leave-to{
    transform: translateY(-10%);
    opacity: 0;
  }
</style>
