<template>
  <div class="flex">
    <span class="textStyle" :class="{ toRight0: slideState }">数据统计</span>
    <div class="dataCard" @click="goToAllDataPage" style="cursor: pointer">
      <div class="titleStyle">
        <span>数据统计</span>
      </div>
      <div class="items">
        <div class="wordStyle">
          <span>总数据量(条):</span>
          <span class="total-num" v-if="totalDataNumber !== null">{{
            formatExactLargeNum(totalDataNumber) ?? '--'
          }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>采集的页面数量(个):</span>
          <span class="total-num" v-if="totalPageNumber !== null">
            {{ formatExactLargeNum(totalPageNumber) ?? '--' }}</span
          >
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>采集的目标数量(个):</span>
          <span class="total-num" v-if="totalTargetNumber !== null">
            {{ formatExactLargeNum(totalTargetNumber) ?? '--' }}</span
          >
          <span class="loading" v-else></span>
        </div>
      </div>
    </div>
    <div class="dataCard" @click="goToTaskRecordPage" style="cursor: pointer">
      <div class="titleStyle">
        <span>任务执行统计</span>
      </div>
      <div class="items">
        <div class="wordStyle">
          <span>任务执行成功统计(个):</span>
          <span class="total-num" v-if="successTask !== null">{{ formatExactLargeNum(successTask) ?? '--' }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>任务执行失败统计(个):</span>
          <span class="total-num" v-if="failTask !== null">{{ formatExactLargeNum(failTask) ?? '--' }}</span>
          <span class="loading" v-else></span>
        </div>
      </div>
    </div>
    <div class="dataCard">
      <div class="titleStyle">
        <span>性能统计</span>
      </div>
      <div class="items">
        <div class="wordStyle">
          <span>平均成功率:</span>
          <span class="total-num" v-if="speed !== null">
            {{ `${Number(Math.floor(speed * 100) / 100).toFixed(2)}%` }}</span
          >
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>平均错误率: </span>
          <span class="total-num" v-if="errorRate !== null">{{
            `${Number(Math.ceil(errorRate * 100) / 100).toFixed(2)}%`
          }}</span>
          <span class="loading" v-else></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from 'vue-router'
import { getStatsDataList, getSpiderTaskList, getPerformanceList } from '@/api/staticData'
import { onMounted, ref } from 'vue'
import formatExactLargeNum from '@/utils/formatExactLargeNum'
import { useSlideStateStore } from '@/store/slideState'
import { storeToRefs } from 'pinia'

const slideStateStore = useSlideStateStore()
const { slideState } = storeToRefs(slideStateStore)

const router = useRouter()
const totalDataNumber = ref(null)
const totalPageNumber = ref(null)
const totalTargetNumber = ref(null)
const successTask = ref(null)
const failTask = ref(null)
const unusualTask = ref(null)
const speed = ref(null)
const errorRate = ref(null)
const unusualRate = ref(null)

const goToAllDataPage = () => {
  router.push({
    path: '/osDataDisplay/list',
    query: {
      jump: 'yes',
      mode: 'DSN数据'
    }
  })
}

const goToTaskRecordPage = () => {
  router.push({
    path: '/osTaskRecord/list',
    query: {
      jump: 'yes',
      mode: '状态监控'
    }
  })
}

// 获取数据统计数据的方法
const getData = async () => {
  const staticData = await getStatsDataList({})
  const spiderTask = await getSpiderTaskList({})
  const performance = await getPerformanceList({})
  totalDataNumber.value = staticData.data.totalData
  totalPageNumber.value = staticData.data.ituPage
  totalTargetNumber.value = staticData.data.spaceTrackItemCount
  successTask.value = spiderTask.data.successCount
  failTask.value = spiderTask.data.failCount
  unusualTask.value = spiderTask.data.exceptionCount
  speed.value = performance.data.success
  errorRate.value = performance.data.error
  unusualRate.value = performance.data.exception
}

//sidebarStatus

onMounted(() => {
  getData()
})
</script>

<style lang="scss" scoped>
.dataCard {
  background-image: url('@/assets/picture/box2.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  // background: #c6ebfc;
  // border: 1.5px solid rgb(193, 188, 188);
  width: 350px;
  border-radius: 5px;
}

/* 标题区 */
.titleStyle {
  color: #dff6ff;
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  text-shadow: 0 0 8px rgba(100, 200, 255, 0.8);
  letter-spacing: 2px;
  margin-top: 15px;
}

.wordStyle {
  font-size: 18px;
  color: #ffffff;
  margin-top: 1%;
  display: flex;
  margin-left: 15px;
  display: flex;
  justify-content: space-between;
}

.textStyle {
  writing-mode: vertical-lr;
  font-size: 22px;
  letter-spacing: 3px;
  color: #ffffff;
  font-weight: 500;
  transform: translateX(-30px);
  transition: 0.2s;
}

.total-num {
  color: #4edaff;
  font-size: 16px;
  font-weight: 600;
  text-shadow: 0 0 10px rgba(0, 200, 255, 0.5);
  letter-spacing: 1px;
  margin-right: 30px;
  animation: visible 0.6s;
}

@keyframes visible {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

.toRight0 {
  transform: translateX(-10px);
}

.items {
  display: flex;
  flex-direction: column;
  padding-left: 15px;
  padding-top: 20px;
}

.loading {
  width: 40px;
  aspect-ratio: 2;
  margin-right: 30px;
  --_g: no-repeat radial-gradient(circle closest-side, #4edaff 90%, #0000);
  background:
    var(--_g) 0% 50%,
    var(--_g) 50% 50%,
    var(--_g) 100% 50%;
  background-size: calc(100% / 3) 50%;
  animation: l3 1s infinite linear;
}
@keyframes l3 {
  20% {
    background-position:
      0% 0%,
      50% 50%,
      100% 50%;
  }
  40% {
    background-position:
      0% 100%,
      50% 0%,
      100% 50%;
  }
  60% {
    background-position:
      0% 50%,
      50% 100%,
      100% 0%;
  }
  80% {
    background-position:
      0% 50%,
      50% 50%,
      100% 100%;
  }
}
</style>
