<script setup>
import { ref, watch } from "vue";
import BowTarget from "@/components/BowTarget.vue";
import Avatar from "@/components/Avatar.vue";
import { roundsName } from "@/constants";
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  onClose: {
    type: Function,
    default: () => {},
  },
  data: {
    type: Object,
    default: () => ({}),
  },
});
const selected = ref(0);
const scores = ref([]);
const tabs = ref(["所有轮次"]);
const players = ref([]);
const onClickTab = (index) => {
  selected.value = index;
  scores.value = [];
  if (index === 0) {
    Object.values(props.data.roundsData).forEach((round) => {
      Object.values(round).forEach((arrows) => {
        scores.value = [...scores.value, ...Object.values(arrows)];
      });
    });
  } else {
    Object.values(props.data.roundsData[index]).forEach((arrows) => {
      scores.value = [...scores.value, ...Object.values(arrows)];
    });
  }
};
watch(
  () => props.data,
  (value) => {
    if (value.winner === 0) {
      players.value = [
        ...Object.values(value.redPlayers),
        ...Object.values(value.bluePlayers),
      ];
    } else if (value.winner === 1) {
      players.value = [
        ...Object.values(value.bluePlayers),
        ...Object.values(value.redPlayers),
      ];
    }
    Object.keys(value.roundsData).forEach((key) => {
      tabs.value.push(`第${roundsName[key]}轮`);
    });
    onClickTab(0);
  }
);
</script>

<template>
  <view class="container" :style="{ display: show ? 'flex' : 'none' }">
    <view>
      <text>1v1排位赛</text>
      <view @click="onClose">
        <image src="../static/close-white.png" mode="widthFix" />
      </view>
    </view>
    <view>
      <view
        v-for="(tab, index) in tabs"
        :key="index"
        @click="() => onClickTab(index)"
        :class="selected === index ? 'selected-tab' : ''"
      >
        {{ tab }}
      </view>
    </view>
    <view :style="{ width: '95%' }">
      <BowTarget :scores="scores" :showE="false" />
    </view>
    <view class="score-row" v-for="(player, index) in players" :key="index">
      <Avatar
        :src="player.avatar"
        :borderColor="data.bluePlayers[player.playerId] ? 1 : 2"
      />
      <view
        v-if="selected > 0"
        v-for="(score, index) in data.roundsData[selected][player.playerId]"
        :key="index"
        class="score-item"
        :style="{ width: '13vw', height: '13vw' }"
      >
        {{ score.ringScore }}
      </view>
    </view>
  </view>
</template>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.8);
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 10;
}
.container > view:first-child {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  color: #fff;
  position: relative;
  font-size: 20px;
}
.container > view:first-child > view:last-child {
  position: absolute;
  right: 5px;
  top: 32px;
}
.container > view:first-child > view:last-child > image {
  width: 40px;
}
.container > view:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: calc(100% - 20px);
  color: #fff9;
  padding: 0 10px;
  overflow-x: auto;
}
.container > view:nth-child(2)::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
.container > view:nth-child(2) > 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 {
  margin: 10px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
.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: 24px;
  margin-left: 10px;
}
</style>
