<script setup>
import { ref, onMounted, onUnmounted } from "vue";
const isIos = ref(true);

const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  onBack: {
    type: Function,
    default: null,
  },
});

const onClick = () => {
  if (props.onBack) props.onBack();
  else uni.navigateBack();
};

const loading = ref(false);
const showLoader = ref(false);
const updateLoading = (value) => {
  loading.value = value;
};

onMounted(() => {
  const deviceInfo = uni.getDeviceInfo();
  isIos.value = deviceInfo.osName === "ios";
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  if (
    currentPage.route === "pages/battle-room" ||
    currentPage.route === "pages/team-match" ||
    currentPage.route === "pages/melee-match"
  ) {
    showLoader.value = true;
  }
  uni.$on("update-header-loading", updateLoading);
});
onUnmounted(() => {
  uni.$off("update-header-loading", updateLoading);
});
</script>

<template>
  <view class="container" :style="{ paddingTop: isIos ? '80rpx' : '50rpx' }">
    <view class="back-btn" @click="onClick">
      <image src="../static/back.png" mode="widthFix" />
    </view>
    <view>
      <block
        v-if="
          '-凹造型-感知距离-小试牛刀'.indexOf(title) === -1 ||
          '-凹造型-感知距离-小试牛刀'.indexOf(title) === 10
        "
      >
        <text>{{ title }}</text>
      </block>
      <block
        v-if="
          title &&
          '-凹造型-感知距离-小试牛刀'.indexOf(title) !== -1 &&
          '-凹造型-感知距离-小试牛刀'.indexOf(title) !== 10
        "
      >
        <view class="first-try-steps">
          <text :class="title === '-凹造型' ? 'current-step' : ''">凹造型</text>
          <text>-</text>
          <text :class="title === '-感知距离' ? 'current-step' : ''"
            >感知距离</text
          >
          <text>-</text>
          <text :class="title === '-小试牛刀' ? 'current-step' : ''"
            >小试牛刀</text
          >
        </view>
      </block>
    </view>
    <image
      :style="{ opacity: showLoader && loading ? 1 : 0 }"
      src="../static/btn-loading.png"
      mode="widthFix"
      class="loading"
    />
  </view>
</template>

<style scoped>
.container {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: 72vw;
  height: 120rpx;
  /* margin-top: var(--status-bar-height); */
  padding-left: 15px;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
}
.back-btn {
  display: flex;
  align-items: center;
}
.back-btn > image {
  width: 22px;
  height: 22px;
  margin-right: 10px;
}
.first-try-steps {
  display: flex;
  align-items: center;
  color: #fff6;
  font-size: 14px;
}
.first-try-steps > text {
  transition: all 0.3s ease;
}
.first-try-steps > text:nth-child(2),
.first-try-steps > text:nth-child(4) {
  margin: 0 5px;
}
.current-step {
  font-size: 16px;
  color: #fff;
}
.loading {
  width: 20px;
  height: 20px;
  margin-left: 10px;
  transition: all 0.3s ease;
  background-blend-mode: darken;
  animation: rotate 2s linear infinite;
}
</style>
