<template>
  <transition name="fade"
              @after-enter="afterEnter"
              @after-leave="afterLeave">
    <div class="promptBox"
         v-show="show"
         v-if="status"
         @click="close"
         @touchmove.prevent>
      <div class="wrap"
           @click.stop>
        <slot></slot>
      </div>
    </div>
  </transition>
</template>
<script>
import bus from "../../util/bus.js";
export default {
  props: {
    visible: {
      required: true,
      type: Boolean
    }
  },
  data() {
    return {
      show: false,
      status: true,
      transitionend: false
    };
  },
  methods: {
    afterEnter() {
      this.transitionend = true;
    },
    afterLeave() {
      this.status = false;
      this.$parent.$destroy();
    },
    close() {
      if (this.show && this.transitionend) {
        this.show = false;
        this.$emit("close");
      }
    }
  },
  watch: {
    visible(val) {
      this.show = val;
    }
  },
  mounted() {
    this.show = this.visible;
    window.addEventListener('popstate', () => {
      this.show = false;
    })
  }
};
</script>
<style lang="scss">
.promptBox {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(#000, 0.3);
  display: flex;
  z-index: 999;
  .wrap {
    margin: auto;
    background-color: #fff;
    width: 6.6rem;
    text-align: center;
    padding-bottom: 0.3rem;
    border-radius: 0.08rem;
    overflow: hidden;
  }

  .head {
    height: 1rem;
    line-height: 1rem;
    font-size: 0.32rem;
    border-bottom: 1px solid #e5e5e5;
  }

  .body {
    font-size: 0.32rem;
    padding: 0.3rem;
    div {
      height: 0.68rem;
      line-height: 0.68rem;
    }
  }

  .confirm {
    width: 5.75rem;
    height: 0.9rem;
    line-height: 0.9rem;
    background-color: #32a1ff;
    margin: 0 auto 0.15rem;
    color: #fff;
    font-size: 0.36rem;
    border-radius: 0.08rem;
  }

  .tip {
    font-size: 0.24rem;
    color: #ccc;
    display: inline-block;
    margin: 0 auto;
    padding-left: 2em;
    background: url(~@/assets/checkbox.png) no-repeat left/auto 80%;
  }

  .tip.checked {
    background-image: url(~@/assets/checkbox_checked.png);
  }

  .is-discount,
  .is-discount .red {
    color: #999;
    font-size: 90%;
    text-decoration: line-through;
  }
}
</style>