<template>
  <div v-if="btn" class="AiBack" @click.stop="back">
    <img :src="$cdn + 'home/back.png'" alt="">
    <text>返回</text>
  </div>
  <ai-fixed-btn v-else-if="!isTopPage||custom">
    <div class="AiBack" @click.stop="back">
      <img :src="$cdn + 'home/back.png'" alt="">
      <text>返回</text>
    </div>
  </ai-fixed-btn>
</template>
<script>
import AiFixedBtn from "./AiFixedBtn";

export default {
  name: "AiBack",
  components: {AiFixedBtn},
  inject: {root: {}},
  props: {
    delta: {
      type: Number,
      default: 1
    },
    eventName: {
      type: String,
      default: ''
    },
    data: {
      type: Object | Boolean,
      default: () => {
      }
    },
    custom: Boolean,
    visible: Boolean,
    btn: Boolean
  },
  data() {
    return {
      isTopPage: false
    }
  },
  methods: {
    back() {
      if (this.visible) {
        return this.$parent.$emit(this.eventName, this.data)
      } else if (this.custom) {
        this.$emit("back")
      } else if (this.custom) {
      } else uni.navigateBack({
        success: () => {
          uni.$emit(this.eventName, this.data)
        }
      })
    }
  },
  mounted() {
    this.isTopPage = window.history.length <= 1
  }
}
</script>

<style lang="scss" scoped>
.AiBack {
  width: 108px;
  height: 108px;
  background: #6BA1F9;
  box-shadow: 0 0 12px 0 rgba(0, 0, 0, 0.12);
  border-radius: 50%;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;

  img {
    width: 40px;
    height: 40px;
  }

  text {
    font-size: 26px;
    font-weight: 800;
    color: #FFFFFF;
    line-height: 40px;
  }
}
</style>
