<script setup>
import { ref, onMounted, computed } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import Container from "@/components/Container.vue";
import BowTargetEdit from "@/components/BowTargetEdit.vue";
import ScreenHint2 from "@/components/ScreenHint2.vue";
import SButton from "@/components/SButton.vue";
import RingBarChart from "@/components/RingBarChart.vue";

import { getPointBookDetailAPI } from "@/apis";

const selectedIndex = ref(0);
const showTip = ref(false);
const showTip2 = ref(false);
const groups = ref([]);
const data = ref({});
const targetId = ref(0);
const targetSrc = ref("");
const arrows = ref([]);

const openTip = (index) => {
  if (index === 1) showTip.value = true;
  else if (index === 2) showTip2.value = true;
};

const closeTip = () => {
  showTip.value = false;
  showTip2.value = false;
};

const onSelect = (index) => {
  selectedIndex.value = index;
  data.value = groups.value[index];
  arrows.value = groups.value[index].list.filter((item) => item.x && item.y);
};

const goBack = () => {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 2];
  uni.navigateBack({
    delta: currentPage.route === "pages/point-book" ? 1 : 2,
  });
};

const ringRates = computed(() => {
  const rates = new Array(12).fill(0);
  arrows.value.forEach((item) => {
    if (item.ring === -1) rates[11] += 1;
    else rates[item.ring] += 1;
  });
  return rates.map((r) => r / arrows.value.length);
});

onLoad(async (options) => {
  if (options.id) {
    const result = await getPointBookDetailAPI(options.id || 164);
    const config = uni.getStorageSync("point-book-config");
    config.targetOption.some((item) => {
      if (item.id === result.targetType) {
        targetId.value = item.id;
        targetSrc.value = item.icon;
      }
    });
    if (result.groups) {
      groups.value = result.groups;
      data.value = result.groups[0];
      arrows.value = result.groups[0].list;
    }
  }
});
</script>

<template>
  <Container
    :bgType="2"
    bgColor="#F5F5F5"
    :whiteBackArrow="false"
    title="分析"
    :onBack="goBack"
  >
    <view class="container">
      <view class="tab-bar">
        <view
          v-for="(_, index) in groups"
          :key="index"
          @click="onSelect(index)"
          :style="{ borderColor: selectedIndex === index ? '#FED847' : '#fff' }"
        >
          <text
            :style="{
              color: selectedIndex === index ? '#000' : '#333',
              fontSize: selectedIndex === index ? '15px' : '13px',
              letterSpacing: index !== 0 ? '2px' : '0',
            }"
            >{{ index === 0 ? "全部" : `第${index}组` }}</text
          >
          <!-- <image
            src="../static/s-triangle.png"
            mode="widthFix"
            :style="{ bottom: selectedIndex !== index ? '0' : '-5px' }"
          /> -->
        </view>
      </view>
      <view class="detail-data">
        <view>
          <view
            :style="{ display: 'flex', alignItems: 'center' }"
            @click="() => openTip(1)"
          >
            <text>落点稳定性</text>
            <image
              src="../static/s-question-mark.png"
              mode="widthFix"
              class="question-mark"
            />
          </view>
          <text>{{ Number((data.stability || 0).toFixed(2)) }}</text>
        </view>
        <view>
          <view>黄心率</view>
          <text>{{ Number((data.yellowRate * 100).toFixed(2)) }}%</text>
        </view>
        <view>
          <view>10环数</view>
          <text>{{ data.tenRings }}</text>
        </view>
        <view>
          <view>平均环数</view>
          <text>{{ Number((data.averageRing || 0).toFixed(2)) }}</text>
        </view>
        <view>
          <view>总环数</view>
          <text>{{ data.userTotalRing }}/{{ data.totalRing }}</text>
        </view>
      </view>
      <view class="title-bar">
        <view />
        <text>落点分布</text>
        <button hover-class="none" @click="() => openTip(2)">
          <image
            src="../static/s-question-mark.png"
            mode="widthFix"
            class="question-mark"
          />
        </button>
      </view>
      <BowTargetEdit
        :id="targetId"
        :src="targetSrc"
        :arrows="arrows.filter((item) => item.x && item.y)"
        :editMode="false"
      />
      <view class="title-bar">
        <view />
        <text>环值分布</text>
      </view>
      <view :style="{ padding: '0 30rpx' }">
        <RingBarChart :data="ringRates" />
      </view>
      <view class="title-bar" :style="{ marginTop: '30rpx' }">
        <view />
        <text>{{
          selectedIndex === 0 ? "每组环数" : `第${selectedIndex}组环数`
        }}</text>
      </view>
      <view class="ring-text-groups">
        <view v-for="(item, index) in groups" :key="index">
          <text v-if="selectedIndex === 0 && index !== 0">{{
            `第${index}组`
          }}</text>
          <view
            v-if="
              (selectedIndex === 0 && index !== 0) ||
              (selectedIndex !== 0 && index === selectedIndex)
            "
          >
            <text
              v-for="(arrow, index2) in item.list"
              :key="index2"
              :style="{
                color:
                  arrow.ring === 0 || arrow.ring === 10 ? '#FFA118' : '#666',
              }"
            >
              {{
                arrow.ring === 0
                  ? "X"
                  : arrow.ring === -1
                  ? "M"
                  : arrow.ring + "环"
              }}
            </text>
          </view>
        </view>
      </view>
      <view :style="{ marginBottom: '40rpx' }">
        <SButton :onClick="goBack" :rounded="50">关闭</SButton>
      </view>
      <ScreenHint2 :show="showTip || showTip2" :onClose="closeTip">
        <view class="tip-content">
          <block v-if="showTip">
            <text>落点稳定性说明</text>
            <text
              >通过计算每支箭与其他箭的平均距离衡一量射箭的稳定性，数字越小则说明射箭越稳定。该数据只能在用户标记落点的情况下生成。</text
            >
          </block>
          <block v-if="showTip2">
            <text>落点分布说明</text>
            <text>展示用户某次练习中射箭的点位</text>
          </block>
        </view>
      </ScreenHint2>
    </view>
  </Container>
</template>

<style scoped>
.container {
  width: 100%;
}
.tab-bar {
  display: flex;
  width: clac(100% - 20px);
  overflow-x: auto;
  padding: 0 10px;
  margin-top: 10px;
}
.tab-bar::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
.tab-bar > view {
  box-sizing: border-box;
  border: 2px solid #fff;
  border-radius: 10px;
  background-color: #fff;
  width: 24vw;
  height: 90rpx;
  text-align: center;
  margin: 5px;
  margin-top: 0;
  font-size: 14px;
  flex: 0 0 auto;
  position: relative;
}
.tab-bar > view > text {
  line-height: 90rpx;
  transition: all 0.2s ease;
}
.tab-bar > view > image {
  position: absolute;
  width: 14px;
  height: 4px;
  left: calc(50% - 7px);
  transition: all 0.3s ease;
}
.detail-data {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  column-gap: 3vw;
  margin: 10px 15px;
}
.detail-data > view {
  border-radius: 10px;
  background-color: #fff;
  margin-bottom: 20rpx;
  padding: 18rpx 24rpx;
}
.detail-data > view > view {
  font-size: 13px;
  color: #999;
  margin-bottom: 12rpx;
}
.detail-data > view > view > text {
  word-break: keep-all;
}
.detail-data > view > text {
  font-weight: 500;
  color: #000;
}
.question-mark {
  width: 28rpx;
  height: 28rpx;
  margin-left: 3px;
}
.title-bar {
  width: 100%;
  display: flex;
  align-items: center;
  font-size: 13px;
  color: #999;
}
.title-bar > view:first-child {
  width: 8rpx;
  height: 28rpx;
  border-radius: 10px;
  background-color: #fed847;
  margin-right: 7px;
  margin-left: 15px;
}
.title-bar > button {
  height: 34rpx;
}
.tip-content {
  width: 100%;
  padding: 25px;
  display: flex;
  flex-direction: column;
}
.tip-content > text {
  width: 100%;
}
.tip-content > text:first-child {
  text-align: center;
}
.tip-content > text:last-child {
  font-size: 13px;
  margin-top: 20px;
  opacity: 0.8;
}
.ring-text-groups {
  display: flex;
  flex-direction: column;
  padding: 20rpx;
  padding-top: 30rpx;
  font-size: 24rpx;
  color: #999999;
}
.ring-text-groups > view {
  display: flex;
  justify-content: center;
}
.ring-text-groups > view > text {
  width: 82rpx;
  text-align: center;
  font-size: 27rpx;
}
.ring-text-groups > view > view {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(6, auto);
  grid-gap: 10rpx;
  margin-bottom: 30rpx;
  margin-left: 20rpx;
}
.ring-text-groups > view > view > text {
  width: 60rpx;
  text-align: center;
  margin-bottom: 10rpx;
}
</style>
