<script setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import BowTarget from "@/components/BowTarget.vue";
import Avatar from "@/components/Avatar.vue";
import { roundsName } from "@/constants";
import { getBattleAPI } from "@/apis";
const selected = ref(0);
const redScores = ref([]);
const blueScores = ref([]);
const tabs = ref([]);
const players = ref([]);
const data = ref({});

const loadArrows = (round) => {
  round.shoots[1].forEach((arrow) => {
    blueScores.value.push(arrow);
  });
  round.shoots[2].forEach((arrow) => {
    redScores.value.push(arrow);
  });
};
onLoad(async (options) => {
  if (!options.battleId) return;
  const result = await getBattleAPI(options.battleId || "57943107462893568");
  data.value = result;
  data.value.teams[1].players.forEach((p, index) => {
    players.value.push(p);
    players.value.push(data.value.teams[2].players[index]);
  });
  Object.values(data.value.rounds).forEach((round, index) => {
    if (round.ifGold) tabs.value.push(`决金箭`);
    else tabs.value.push(`第${roundsName[index + 1]}轮`);
  });
  selected.value = Number(options.selected || 0);
  onClickTab(selected.value);
});
const onClickTab = (index) => {
  selected.value = index;
  redScores.value = [];
  blueScores.value = [];
  loadArrows(data.value.rounds[index]);
};
</script>

<template>
  <Container title="靶纸">
    <view class="container">
      <view>
        <view
          v-for="(tab, index) in tabs"
          :key="index"
          @click="() => onClickTab(index)"
          :class="selected === index ? 'selected-tab' : ''"
        >
          {{ tab }}
        </view>
      </view>
      <view :style="{ margin: '20px 0' }">
        <BowTarget :scores="redScores" :blueScores="blueScores" mode="team" />
      </view>
      <view class="score-container">
        <view
          class="score-row"
          v-for="(player, index) in players"
          :key="index"
          :style="{
            justifyContent: index % 2 === 0 ? 'flex-end' : 'flex-start',
          }"
        >
          <Avatar
            :src="player.avatar"
            :borderColor="index % 2 === 0 ? '#64BAFF' : '#FF6767'"
            :size="36"
          />
          <view>
            <view
              v-for="(score, index) in data.rounds[selected].shoots[
                index % 2 === 0 ? 1 : 2
              ]"
              :key="index"
              class="score-item"
            >
              {{ score.ringX ? "X" : score.ring }}
            </view>
          </view>
        </view>
      </view>
    </view>
  </Container>
</template>

<style scoped>
.container {
  width: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.container > view:nth-child(1) {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: calc(100% - 20px);
  color: #fff9;
  padding: 10px;
  overflow-x: auto;
}
.container > view:nth-child(1)::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
.container > view:nth-child(1) > view {
  border: 1px solid #fff9;
  border-radius: 20px;
  padding: 7px 10px;
  margin: 0 5px;
  font-size: 14px;
  flex: 0 0 auto;
}
.selected-tab {
  background-color: #fed847;
  border-color: #fed847 !important;
  color: #000;
}
.score-row {
  display: flex;
  align-items: flex-start;
  margin-bottom: 5px;
  width: calc(50% - 5px);
  padding-left: 5px;
}
.score-row > view:last-child {
  margin-left: 10px;
  display: grid;
  grid-template-columns: repeat(3, auto);
  gap: 5px;
  margin-right: 5px;
  min-width: 26%;
}
.score-item {
  background-image: url("../static/score-bg.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  color: #fed847;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  width: 10vw;
  height: 10vw;
}
.score-container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
</style>
