<template>
  <!-- 遮罩的蒙版 -->
  <transition name="fade">
    <div
      class="my-dialog__wrapper"
      v-if="visible"
      @click.self="$emit('update:visible', !visible)"
    >
      <!-- 操纵区域 -->
      <div class="my-dialog" :style="{ marginTop: top, width }">
        <!-- 头部 -->
        <div class="my-dialog__header">
          <span class="my-dialog__title">
            <!-- slot中的内容 叫 后备内容 不传插槽 用内部的值 传入了插槽 用 插槽的值 -->
            <slot name="title"> {{ title }}</slot>
          </span>
          <!-- 关闭按钮 -->
          <button
            v-if="showClose"
            type="button"
            aria-label="Close"
            class="my-dialog__headerbtn"
          >
            <i
              class="iconfont icon-close"
              @click.self="$emit('update:visible', !visible)"
            ></i>
          </button>
        </div>
        <!-- 主体 -->
        <div class="my-dialog__body">
          <slot></slot>
        </div>
        <!-- 脚部 -->
        <div class="my-dialog__footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  name: 'my-dialog',
  props: {
    // 是否显示
    visible: {
      type: Boolean,
      default: false
    },
    // 是否显示关闭按钮
    showClose: {
      type: Boolean,
      default: true
    },
    // 顶部的间隙
    top: {
      type: String,
      default: '0'
    },
    // 操纵区域的宽度
    width: {
      type: String,
      default: '50%'
    },
    // 标题
    title: {
      type: String,
      default: '标题'
    }
  }
}
</script>

<style lang="less">
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
.my-dialog__wrapper {
  z-index: 2011;
  margin: 0;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  background: rgba(0, 0, 0, 0.3);
  .my-dialog {
    position: relative;
    margin: 0 auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    box-sizing: border-box;
    width: 50%;
    .my-dialog__header {
      padding: 20px 20px 10px;
      .my-dialog__title {
        line-height: 24px;
        font-size: 18px;
        color: #303133;
      }
      .my-dialog__headerbtn {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
        .iconfont {
          color: #909399;
        }
      }
    }
    .my-dialog__body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    .my-dialog__footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
    }
  }
}
</style>
