<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 { getGameAPI } from "@/apis";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const scores = ref([]);
const currentUser = ref({});
const data = ref({});
const onSelect = (userId) => {
  const user = data.value.players.find((p) => p.playerId === userId);
  currentUser.value = user;
  if (user && user.arrowHistory) {
    scores.value = user.arrowHistory;
  }
};
onLoad(async (options) => {
  if (options.battleId) {
    const result = await getGameAPI(options.battleId);
    data.value = result;
    if (result.players && result.players[0]) {
      onSelect(result.players[0].playerId);
    }
  }
});
</script>

<template>
  <Container title="靶纸">
    <view class="container">
      <image src="../static/battle-header-melee.png" mode="widthFix" />
      <view class="players" v-if="data.players">
        <view
          v-for="(player, index) in data.players"
          :key="index"
          :style="{
            width: `${Math.max(100 / data.players.length, 18)}vw`,
            color: player.playerId === currentUser.playerId ? '#000' : '#fff9',
          }"
          @click="() => onSelect(player.playerId)"
        >
          <image
            v-if="player.playerId === currentUser.playerId"
            src="../static/player-bg2.png"
            :style="{
              width: `${Math.max(100 / data.players.length, 18)}vw`,
            }"
            class="player-bg"
          />
          <Avatar :src="player.avatar" :rankLvl="player.rankLvl" :size="40" />
          <text>{{ player.name }}</text>
        </view>
      </view>
      <view :style="{ marginTop: '10px' }">
        <BowTarget :scores="scores" />
      </view>
      <view class="score-text"
        ><text :style="{ color: '#fed847' }">{{ scores.length }}</text
        >支箭，共<text :style="{ color: '#fed847' }">{{
          scores.reduce((last, next) => last + next.ring, 0)
        }}</text
        >环</view
      >
      <view class="score-row">
        <view
          v-for="(score, index) in scores"
          :key="index"
          class="score-item"
          :style="{ width: '13vw', height: '13vw' }"
        >
          {{ score.ring }}
        </view>
      </view>
    </view>
  </Container>
</template>

<style scoped>
.container {
  width: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
}
.container > image:first-child {
  position: absolute;
  width: 100%;
  z-index: 1;
}
.players {
  display: flex;
  width: 100%;
  overflow-x: auto;
  margin-top: 25px;
}
.players::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
.players > view {
  background-color: #fff3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  flex: 0 0 auto;
  position: relative;
  height: 68px;
  margin-bottom: 10px;
  padding-top: 10px;
}
.player-bg {
  position: absolute;
  width: 100%;
  height: 85px;
  top: 0;
}
.players > view > text {
  margin: 5px 0;
  width: 80%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  position: relative;
}
.score-text {
  width: 100%;
  color: #fff;
  text-align: center;
  font-size: 16px;
  margin-bottom: 20px;
}
.score-row {
  margin: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
.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: 3px;
}
</style>
