<script>
export default {
  name: 'MDialog'
}
</script>

<script setup>
const props = defineProps({
  title: {
    type: String,
    default: '提示'
  },
  width: {
    type: String,
    default: '50%'
  },
  top: {
    type: String,
    default: '15vh'
  },
  footer: {
    type: Object
  },
  visible: {
    type: Boolean
  }
})

const emit = defineEmits([
  'update:visible'
])

function handleClose () {
  emit('update:visible', false)
}
</script>

<template>
  <transition name="dialog-fade">
    <!-- @click.self避免冒泡，只有点击自己时才能触发   -->
    <div v-show="visible" class="m-dialog__wrapper" @click.self="handleClose">
      <div class="m-dialog" :style="{width, marginTop: top}">
        <div class="m-dialog__header">
          <slot name="title">
            <!-- 将span放到slot内，这样不仅可以定义title文本，还可以定义样式等 -->
            <span class="m-dialog__title">
              {{ title }}
            </span>
          </slot>
          <button class="m-dialog__headerbtn" @click="handleClose">
            <i class="one-icon-close"></i>
          </button>
        </div>
        <div class="m-dialog__body">
          <!-- 内容可能是除span以外的其他内容，比如列表等，所以在这里使用插槽，并且不规定插槽内具体的标签 -->
          <!-- 并且在这里使用匿名插槽，使用匿名插槽的好处就是不用指定名称，这样在不使用<template v-slot>指定插槽内容的时候，也可以自定义内容 -->
          <slot></slot>
        </div>
        <div v-if="$slots.footer" class="m-dialog__footer">
          <!-- 如果footer不传递内容，则不显示footer -->
          <slot name="footer">
          </slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<style lang="scss" scoped>
.m-dialog__wrapper {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  z-index: 2001;
  background-color: rgba(0,0,0,0.5);

  .m-dialog {
    position: relative;
    margin: 15vh auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    box-sizing: border-box;
    width: 30%;

    &__header {
      padding: 20px 20px 10px;
      .m-dialog__title{
        line-height: 24px;
        font-size: 18px;
        color: #303133;
      }

      .m-dialog__headerbtn {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
        .one-icon-close {
          color:909399
        }
      }
    }

    &__body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }

    &__footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;

      :deep .m-button:first-child {
        margin-right: 20px;
      }
    }
  }
}

.dialog-fade-enter-active {
  animation: fade .3s;
}

.dialog-fade-leave-active {
  animation: fade .3s reverse;
}

@keyframes fade {
  0% {
    opacity: 0;
    transform: translateY(-20px);
  }

  100%{
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
