<script setup>
import { ref, onMounted, computed } from "vue";
import IconButton from "@/components/IconButton.vue";
import SButton from "@/components/SButton.vue";
import ScreenHint from "@/components/ScreenHint.vue";
import BowData from "@/components/BowData.vue";
import UserUpgrade from "@/components/UserUpgrade.vue";
import { directionAdjusts } from "@/constants";
import useStore from "@/store";
import { storeToRefs } from "pinia";
const store = useStore();
const { user } = storeToRefs(store);
const props = defineProps({
  onClose: {
    type: Function,
    default: () => {},
  },
  total: {
    type: Number,
    default: 0,
  },
  rowCount: {
    type: Number,
    default: 0,
  },
  result: {
    type: Object,
    default: () => ({}),
  },
  tipSrc: {
    type: String,
    default: "",
  },
});
const showPanel = ref(true);
const showComment = ref(false);
const showBowData = ref(false);
const showUpgrade = ref(false);
const totalRing = ref(0);
const closePanel = () => {
  showPanel.value = false;
  setTimeout(() => {
    props.onClose();
  }, 300);
};
function onClickShare() {
  uni.$emit("share-image");
}
onMounted(() => {
  if (props.result.lvl > user.value.lvl) {
    showUpgrade.value = true;
  }
  totalRing.value = (props.result.arrows || []).reduce(
    (last, next) => last + next.ring,
    0
  );
});

const validArrows = computed(() => {
  return (props.result.arrows || []).filter(
    (arrow) => arrow.x !== -30 && arrow.y !== -30
  ).length;
});

const getRing = (arrow) => {
  if (arrow && arrow.x !== -30 && arrow.y !== -30) return arrow.ring;
  return "-";
};
</script>

<template>
  <view class="container">
    <view :class="['container-header', showPanel ? 'scale-in' : 'scale-out']">
      <image :src="tipSrc" mode="widthFix" />
      <image src="../static/finish-frame.png" mode="widthFix" />
      <text
        >完成<text class="gold-text">{{ validArrows }}</text
        >箭，获得<text class="gold-text">{{ validArrows }}</text
        >点经验</text
      >
    </view>
    <view
      class="container-content"
      :style="{ transform: `translateY(${showPanel ? '0%' : '100%'})` }"
    >
      <view>
        <text>本局成绩（共{{ totalRing }}环）：</text>
        <button @click="() => (showBowData = true)">
          <text>查看靶纸</text>
          <image
            src="../static/enter-arrow-blue.png"
            mode="widthFix"
            :style="{ width: '20px' }"
          />
        </button>
      </view>
      <view :style="{ gridTemplateColumns: `repeat(${rowCount}, 1fr)` }">
        <view v-for="(_, index) in new Array(total).fill(0)" :key="index">
          {{ getRing(result.arrows[index])
          }}<text v-if="getRing(result.arrows[index]) !== '-'">环</text>
        </view>
      </view>
      <view>
        <block v-if="validArrows === total">
          <IconButton
            name="分享"
            src="../static/share.png"
            :onClick="onClickShare"
          />
          <IconButton
            name="教练点评"
            src="../static/review.png"
            :onClick="() => (showComment = true)"
          />
        </block>
        <SButton
          :width="validArrows === total ? '70vw' : 'calc(100vw - 20px)'"
          :rounded="30"
          :onClick="closePanel"
          >{{ validArrows === total ? "完成" : "返回" }}</SButton
        >
      </view>
    </view>
    <ScreenHint
      :show="showComment"
      :onClose="() => (showComment = false)"
      mode="tall"
    >
      <view class="coach-comment">
        <text>
          您本次练习取得了<text :style="{ color: '#fed847' }">{{
            totalRing
          }}</text
          >环的成绩，所有箭支上靶后的平均点间距为<text
            :style="{ color: '#fed847' }"
            >{{ Number(result.average_distance.toFixed(2)) }}</text
          >，{{
            result.spreadEvaluation === "Dispersed"
              ? "还需要持续改进哦~"
              : "成绩优秀。"
          }}
        </text>
        <view>
          <image
            src="https://static.shelingxingqiu.com/attachment/2025-11-26/deihtj15xjwcz3c1tx.png"
            mode="widthFix"
          />
          <text :style="{ marginTop: '12px' }"
            >针对您本次的练习，{{
              result.spreadEvaluation === "Dispersed"
                ? "我们建议您充分练习推弓、靠位以及撒放动作一致性。"
                : totalRing >= 100
                ? "我们建议您继续保持即可。"
                : `我们建议您将设备的瞄准器${
                    directionAdjusts[result.adjustmentHint]
                  }调整。`
            }}</text
          >
        </view>
      </view>
    </ScreenHint>
    <BowData
      :total="result.completed_arrows"
      :arrows="result.arrows"
      :show="showBowData"
      :onClose="() => (showBowData = false)"
    />
    <UserUpgrade
      :show="showUpgrade"
      :onClose="() => (showUpgrade = false)"
      :lvl="result.lvl"
    />
  </view>
</template>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.8);
  z-index: 5;
}
.container-header {
  margin-top: 20vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  opacity: 0;
}
.container-header > text:last-child {
  color: #fff;
  text-align: center;
  margin-top: -85px;
}
.container-content {
  width: calc(100vw - 20px);
  background-color: #fff;
  position: absolute;
  bottom: 0;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 15px 10px;
  transition: all 0.3s ease;
  transform: translateY(0);
}
.container-content > view:first-child {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.container-content > view:first-child > text:first-child {
  color: #000;
}
.container-content > view:first-child > button {
  display: flex;
  align-items: center;
  color: #287fff;
  font-size: 14px;
}
.container-content > view:nth-child(2) {
  display: grid;
  row-gap: 10px;
  column-gap: 5px;
  justify-content: flex-start;
  margin-top: 10px;
  margin-bottom: 15px;
}
.container-content > view:nth-child(2) > view {
  background: linear-gradient(#fbfbfb, #f5f5f5);
  border: 1px solid #e5e5e5;
  border-radius: 5px;
  text-align: center;
  line-height: 27px;
  color: #333333;
  font-size: 28rpx;
}
.container-content > view:nth-child(2) > view > text {
  font-size: 20rpx;
  color: #666666;
  margin-left: 5rpx;
}
.container-content > view:nth-child(3) {
  width: 100%;
  display: flex;
  justify-content: space-around;
}
.gold-text {
  color: #fed847;
  font-size: 20px;
  margin: 0 2px;
}
.coach-comment {
  display: flex;
  flex-direction: column;
  font-size: 14px;
}
.coach-comment > view {
  display: flex;
}
.coach-comment > view > image {
  width: 420rpx;
  height: 420rpx;
  margin-right: 20rpx;
}
</style>
