<template>
  <view class="modal-mask">
    <transition name="confirm-fade">
      <view class="confirm-container" v-show="showFlag">
        <view class="popup-title">{{ title }}</view>
        <view class="popup-content">{{ text }}</view>
        <view class="flex-row modal-footer">
          <view class="show-btn cencel-btn-pop" v-if="showCancelButton" @tap="cancel">{{ cancelBtnText }}</view>
          <view class="show-btn ok-btn-pop" @tap="confirm">{{ confirmBtnText }}</view>
        </view>
      </view>
    </transition>
  </view>
</template>

<script>
export default {
  data() {
    return {
      showFlag: false,
      title: "温馨提示",
      text: '请传入描述文本', // 描述文本
      confirmBtnText: "确定",
      cancelBtnText: "取消",
      showCancelButton: true, // 是否显示取消按钮
    };
  },
  methods: {
    show(cb) {
      this.showFlag = true;
      typeof cb === "function" && cb.call(this, this);
      return new Promise((resolve, reject) => {
        this.reject = reject;
        this.resolve = resolve;
      });
    },
    cancel() {
      this.reject("cancel")
      this.hide()
    },
    confirm() {
      this.resolve("confirm")
      this.hide()
    },
    hide() {
      this.showFlag = false
      document.body.removeChild(this.$el)
      this.$destroy()
    }
  }
};
</script>

<style lang="scss" scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  .confirm-container {
    background-color: #fff;
    padding: 48rpx;
    border-radius: 40rpx;
    width: 75%;
  
    .popup-title {
      font-weight: 600;
      font-size: 36rpx;
      color: #060809;
      text-align: center;
      margin-bottom: 26rpx;
      font-family: PingFangSC, PingFang SC;
    }
  
    .popup-content {
      text-align: center;
      font-weight: 400;
      font-size: 28rpx;
      color: #828384;
      margin-bottom: 42rpx;
      font-family: PingFangSC, PingFang SC;
      line-height: 40rpx;
    }
  
    &.confirm-fade-enter-active {
      animation: confirm-fadein 0.3s;
  
      .confirm-content {
        animation: confirm-zoom 0.3s;
      }
    }
  }
  .modal-footer {
    width: 100%;
    align-items: center;
    .show-btn {
      border-radius: 40rpx;
      flex: 1;
      text-align: center;
      font-weight: 600;
      font-size: 32rpx;
      border-radius: 40rpx;
      height: 80rpx;
      line-height: 80rpx;
      &:active {
        opacity: 0.7;
      }
    }
    
    .cencel-btn-pop {
      color: #060809;
      background: #EBECEC;
      margin-right: 12rpx;
    }
    
    .ok-btn-pop {
      color: #FFFFFF;
      background: #060809;
    }
  }
}




@keyframes confirm-fadein {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

@keyframes confirm-zoom {
  0% {
    transform: scale(0);
  }

  50% {
    transform: scale(1.1);
  }

  100% {
    transform: scale(1);
  }
}
</style>