<template>
  <div class="flex">
    <span class="textStyle" :class="{ toRight0: slideState }">任务统计</span>
    <div class="dataCard" @click="goToDSNTaskRecordPage">
      <div class="titleStyle">
        <span>DSN爬取任务</span>
      </div>
      <div class="items">
        <div class="wordStyle">
          <span>DSN爬虫任务数:</span>
          <span class="total-num" v-if="dsnTotalTaskNumber != null">{{ dsnTotalTaskNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>任务执行成功统计:</span>
          <span class="total-num" v-if="dsnTaskSuccessNumber != null">{{ dsnTaskSuccessNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>任务执行失败统计:</span>
          <span class="total-num" v-if="dsnTaskFailNumber != null">{{ dsnTaskFailNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>错误率: {{ dsnErrorRate }} </span>
          <span class="total-num" v-if="dsnErrorRate != null">{{ dsnErrorRate }}</span>
          <span class="loading" v-else></span>
        </div>
      </div>
    </div>
    <div class="dataCard" @click="goToITUTaskRecordPage">
      <div class="titleStyle">
        <span>ITU爬取任务</span>
      </div>
      <div class="items">
        <div class="wordStyle">
          <span>ITU爬虫任务数:</span>
          <span class="total-num" v-if="ituTotalTaskNumber != null">{{ ituTotalTaskNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>任务执行成功统计:</span>
          <span class="total-num" v-if="ituTaskSuccessNumber != null">{{ ituTaskSuccessNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>任务执行失败统计:</span>
          <span class="total-num" v-if="ituTaskFailNumber != null">{{ ituTaskFailNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>任务执行失败统计:</span>
          <span class="total-num" v-if="ituTaskFailNumber != null">{{ ituTaskFailNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>错误率:</span>
          <span class="total-num" v-if="ituErrorRate != null">{{ ituErrorRate }}</span>
          <span class="loading" v-else></span>
        </div>
      </div>
    </div>
    <div class="dataCard" @click="goToSTTaskRecordPage">
      <div class="titleStyle">
        <span>ST爬取任务</span>
      </div>
      <div class="items">
        <div class="wordStyle">
          <span>ST爬虫任务数:</span>
          <span class="total-num" v-if="stTotalTaskNumber != null">{{ stTotalTaskNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>任务执行成功统计:</span>
          <span class="total-num" v-if="stTaskSuccessNumber != null">{{ stTaskSuccessNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>采集速度:</span>
          <span class="total-num" v-if="stTaskFailNumber != null">{{ stTaskFailNumber }}</span>
          <span class="loading" v-else></span>
        </div>
        <div class="wordStyle">
          <span>错误率:</span>
          <span class="total-num" v-if="stErrorRate != null">{{ stErrorRate }}</span>
          <span class="loading" v-else></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRouter } from "vue-router";
import { getAllSpiderTaskStatistics } from "@/api/staticData";
import { onMounted, ref } from "vue";
import { getSpiderTaskList } from "@/api/spiderTask";
import { useSlideStateStore } from "@/store/slideState";
import { storeToRefs } from "pinia";

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

const router = useRouter();

const dsnTotalTaskNumber = ref(null);
const dsnTaskSuccessNumber = ref(null);
const dsnTaskFailNumber = ref(null);
const dsnErrorRate = ref(null);
const ituTotalTaskNumber = ref(null);
const ituTaskSuccessNumber = ref(null);
const ituTaskFailNumber = ref(null);
const ituErrorRate = ref(null);
const stTotalTaskNumber = ref(null);
const stTaskSuccessNumber = ref(null);
const stTaskFailNumber = ref(null);
const stErrorRate = ref(null);

const goToDSNTaskRecordPage = () => {
  router.push({
    path: "/osTaskInformation/list",
    query: {
      spiderType: "dsn_now",
      jump: "yes",
      page: "statusMonitor",
    },
  });
};
const goToITUTaskRecordPage = () => {
  router.push({
    path: "/osTaskInformation/list",
    query: {
      spiderType: "itu_space_explorer",
      jump: "yes",
      page: "statusMonitor",
    },
  });
};
const goToSTTaskRecordPage = () => {
  router.push({
    path: "/osTaskInformation/list",
    query: {
      spiderType: "api_spider",
      jump: "yes",
      page: "statusMonitor",
    },
  });
};

// 获取任务统计数据的方法
const getData = async () => {
  const res = await getAllSpiderTaskStatistics({});
  const dsnTask = await getSpiderTaskList({ spiders: "dsn_now" });
  const ituTask = await getSpiderTaskList({ spiders: "itu_space_explorer" });
  const stTask = await getSpiderTaskList({ spiders: "api_spider" });
  dsnTotalTaskNumber.value = dsnTask.data.length;
  dsnTaskSuccessNumber.value = res.data.dsn_now.successCount;
  dsnTaskFailNumber.value = res.data.dsn_now.failCount;
  dsnErrorRate.value = res.data.dsn_now.errorRate;
  ituTotalTaskNumber.value = ituTask.data.length;
  ituTaskSuccessNumber.value = res.data.itu_space_explorer.successCount;
  ituTaskFailNumber.value = res.data.itu_space_explorer.failCount;
  ituErrorRate.value = res.data.itu_space_explorer.errorRate;
  stTotalTaskNumber.value = stTask.data.length;
  stTaskSuccessNumber.value = res.data.space_track.successCount;
  stTaskFailNumber.value = res.data.space_track.failCount;
  stErrorRate.value = res.data.space_track.errorRate;
};
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: 500px;
  height: 100%;
  border-radius: 5px;
  cursor: pointer;
}

.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(-25px);
  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;
}

.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%;
  }
}

@keyframes visible {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}
</style>
