<script setup>
import useStore from "@/store";
import { storeToRefs } from "pinia";
const { user } = storeToRefs(useStore());

const props = defineProps({
  player: {
    type: Object,
    default: () => ({}),
  },
  scores: {
    type: Array,
    default: () => [],
  },
});

const rowCount = new Array(6).fill(0);

const isMember = (player = {}) => player.vip === true || player.sVip === true;

const getMemberNicknameClass = (player = {}) => [
  "member-nickname",
  player.vip === true && player.sVip !== true ? "member-nickname--vip" : "",
  player.sVip === true ? "member-nickname--svip" : "",
];
</script>

<template>
  <view
    class="container"
    :style="{ borderColor: player.id === user.id ? '#FED847' : '#fff3' }"
  >
    <image
      :style="{
        opacity:
          (scores[0] || []).length + (scores[1] || []).length === 12 ? 1 : 0,
      }"
      src="../static/checked-green.png"
      mode="widthFix"
    />
    <image :src="player.avatar || '../static/user-icon.png'" mode="widthFix" />
    <view
      v-if="isMember(player)"
      :class="['player-score-name', ...getMemberNicknameClass(player)]"
    >
      <text class="member-nickname__text">{{ player.name }}</text>
      <text v-if="player.sVip === true" class="member-nickname__shine">
        {{ player.name }}
      </text>
    </view>
    <text v-else>{{ player.name }}</text>
    <view>
      <view>
        <view v-for="(_, index) in rowCount" :key="index">
          <text>{{
            scores[0] && scores[0][index] ? `${scores[0][index].ring}环` : "-"
          }}</text>
        </view>
      </view>
      <view>
        <view v-for="(_, index) in rowCount" :key="index">
          <text>{{
            scores[1] && scores[1][index] ? `${scores[1][index].ring}环` : "-"
          }}</text>
        </view>
      </view>
    </view>
    <text
      >{{
        scores
          .map((s) => s.reduce((last, next) => last + next.ring, 0))
          .reduce((last, next) => last + next, 0)
      }}环</text
    >
  </view>
</template>

<style scoped>
.container {
  width: calc(100% - 70px);
  display: flex;
  align-items: center;
  border: 1px solid #fff3;
  margin: 10px 0;
  margin-left: 35px;
  border-radius: 15px;
  padding: 10px;
  color: #fff9;
  font-size: 14px;
  position: relative;
}
.container > image:nth-child(1) {
  position: absolute;
  width: 15px;
  height: 15px;
  top: 30%;
  left: 0;
}
.container > image:nth-child(2) {
  width: 30px;
  height: 30px;
  min-height: 30px;
  border: 1px solid #fff;
  border-radius: 50%;
  margin-right: 10px;
  margin-left: -30px;
  flex: 0 0 auto;
}
.container > text:nth-child(3) {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 20%;
}
.player-score-name {
  width: 20%;
}
.player-score-name .member-nickname__text,
.player-score-name .member-nickname__shine {
  font-size: 14px;
}
.container > view:nth-child(4) {
  display: flex;
  flex-direction: column;
}
.container > view:nth-child(4) > view {
  display: flex;
  padding: 5px 0;
}
.container > view:nth-child(4) > view text {
  width: 34px;
  text-align: center;
  display: block;
}
.container > view:nth-child(4) > view:first-child {
  border-bottom: 1px solid #fff3;
}
.container > text:nth-child(5) {
  width: 40px;
  text-align: right;
  word-break: keep-all;
}
</style>
