<template>
  <div class="general-dialog" v-if="value">
    <!-- 内容区域 -->
    <div class="dialog__container">
      <div class="dialog__close iconfont icon-close" @click="$emit('change', false)"></div>
      <div class="dialog__title">
        <slot name="title"></slot>
        <span v-text="title" v-if="!$slots.title"></span>
      </div>
      <div class="dialog__content">
        <slot></slot>
        <div class="dialog__content_text" v-text="content" v-if="!$slots.default"></div>
      </div>
      <div class="dialog__footer">
        <slot name="footer"></slot>
        <template v-if="!$slots.footer">
          <div class="btn confirm" @click="$emit('confirm')" v-if="confirm">
            {{ confirmText ? confirmText : '确定' }}
          </div>
          <div class="btn cancel" @click="$emit('cancel')" v-if="cancel">
            {{ cancelText ? cancelText : '取消' }}
          </div>
        </template>
      </div>
    </div>
    <!-- 遮罩层 -->
    <div class="dialog__mask"></div>
  </div>
</template>

<script>
export default {
  model: {
    prop: 'value',
    event: 'change',
  },
  props: {
    title: {
      type: String,
      default: '提示',
    },
    content: String,
    value: {
      type: Boolean,
      default: false,
    },
    confirm: {
      type: Boolean,
      default: true,
    },
    cancel: {
      type: Boolean,
      default: false,
    },
    confirmText: String,
    cancelText: String,
  },
  watch: {
    value(val) {
      this.$nextTick(() => {
        val ? document.body.classList.add('scroll-none') : document.body.classList.remove('scroll-none')
      })
    },
  },
}
</script>

<style lang="scss" scoped>
@media screen and (min-width: 768px) {
  .general-dialog {
    position: relative;
    z-index: 999;
    .dialog__container {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      min-width: 400px;
      background: #fff;
      box-sizing: border-box;
      padding: 40px 32px 32px;
      z-index: 1;
      border-radius: 4px;

      .dialog__close {
        position: absolute;
        width: 36px;
        height: 36px;
        right: 8px;
        top: 8px;
        text-align: center;
        line-height: 36px;
        font-size: 24px;
        color: #b2b2b2;
        transition: all 0.2s;
        cursor: pointer;

        &:hover {
          color: #666;
        }
      }

      .dialog__title {
        font-size: 24px;
        color: #282828;
        text-align: center;
      }
      .dialog__content {
        padding: 32px 0;
        .dialog__content_text {
          font-size: 16px;
          color: #282828;
          text-align: center;
        }
      }
      .dialog__footer {
        .btn {
          height: 46px;
          border-radius: 4px;
          text-align: center;
          box-sizing: border-box;
          font-size: 14px;
          margin: 0 auto;
          cursor: pointer;

          &.confirm {
            line-height: 46px;
            background: #f3920d;
            color: #fff;
          }

          &.cancel {
            line-height: 44px;
            border: 1px solid #f3920d;
            color: #f3920d;
            margin-top: 16px;
          }
        }
      }
    }
    .dialog__mask {
      position: fixed;
      width: 100vw;
      height: 100vh;
      left: 0;
      top: 0;
      background: #000;
      opacity: 0.4;
    }
  }
}

@media screen and (max-width: 768px) {
  .general-dialog {
    position: relative;
    z-index: 999;
    .dialog__container {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      min-width: 76vw;
      background: #fff;
      box-sizing: border-box;
      padding: 7vw 7vw 6.4vw;
      z-index: 1;
      border-radius: 0.4vw;

      .dialog__close {
        position: absolute;
        width: 5.6vw;
        height: 5.6vw;
        right: 3.2vw;
        top: 3.2vw;
        text-align: center;
        line-height: 5.6vw;
        font-size: 4.8vw;
        color: #b2b2b2;
        transition: all 0.2s;
        cursor: pointer;

        &:hover {
          color: #666;
        }
      }

      .dialog__title {
        font-size: 4.8vw;
        color: #282828;
        text-align: center;
      }
      .dialog__content {
        padding: 3.2vw 0;
        .dialog__content_text {
          font-size: 3.6vw;
          color: #282828;
          line-height: 5.6vw;
          text-align: center;
        }
      }
      .dialog__footer {
        .btn {
          height: 8.8vw;
          border-radius: 0.4vw;
          text-align: center;
          box-sizing: border-box;
          font-size: 3.6vw;
          margin: 0 auto;
          cursor: pointer;

          &.confirm {
            line-height: 8.8vw;
            background: #f3920d;
            color: #fff;
          }

          &.cancel {
            line-height: 8.8vw;
            border: 0.1vw solid #f3920d;
            color: #f3920d;
            margin-top: 2.4vw;
          }
        }
      }
    }
    .dialog__mask {
      position: fixed;
      width: 100vw;
      height: 100vh;
      left: 0;
      top: 0;
      background: #000;
      opacity: 0.4;
    }
  }
}
</style>
