<script lang="ts" setup>

import VChart from "vue-echarts";
import {useMainLayoutStore} from "~/strores/UseMainLayoutStore";
import {useDataStore} from "~/strores/DataStore";
import dayjs from "dayjs";

type diskInfo = {
  name: string,
  value: strings
}
const id = toRef(useRoute().params.id);
const lsDisk = ref<diskInfo[]>([])
const mainLayoutStore = useMainLayoutStore()
const dataStore = useDataStore()
const route = useRoute()
onMounted(() => {
  $fetch('/Api/Server/GetServerDiskInfo', {
    method: 'GET',
    params: {
      ServerId: mainLayoutStore.SelectServer.id,
      DiskId: id.value
    },
    headers: {
      'Content-Type': 'application/json',
      'Authorization': "Bearer " + useCookie('token').value
    },
    baseURL: useRuntimeConfig().public.baseUrl
  }).then(res => {
    lsDisk.value = res as diskInfo[]
  })
})
const values = ref<(number | null)[]>([...Array.from({length: 61}, (_, index) => null)])
const rValues = ref<(number | null)[]>([...Array.from({length: 61}, (_, index) => null)])
const wValues = ref<(number | null)[]>([...Array.from({length: 61}, (_, index) => null)])
const option = computed(() => {
  return {
    animation: false,
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: Array.from({length: values.value.length}, (_, index) => dayjs().add(-index, 's').toString()),
      splitLine: {
        show: true,
        lineStyle: {
          color: ['rgba(38,153,100,0.6)'],
          width: 2,
          type: 'solid'
        }
      }
    },
    grid: {
      left: '0',
      right: '0',
      bottom: '0',
      top: '0',
      show: true,
    },
    yAxis: {
      type: 'value',
      min: 0,
      max: 100,
      splitNumber: 10,
      splitLine: {
        show: true,
        lineStyle: {
          color: ['rgba(38,153,100,0.6)'],
          width: 2,
          type: 'solid'
        }
      }
    },
    series: [
      {
        data: values.value,
        color: ['#269964'],
        type: 'line',
        areaStyle: {}
      }
    ],
  }
})
const RWOption = computed(() => {
  return {
    animation: false,
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: Array.from({length: rValues.value.length}, (_, index) => dayjs().add(-index, 's').toString()),
      splitLine: {
        show: true,
        lineStyle: {
          color: ['rgba(38,153,100,0.6)'],
          width: 2,
          type: 'solid'
        }
      }
    },
    grid: {
      left: '0',
      right: '0',
      bottom: '0',
      top: '0',
      show: true,
    },
    yAxis: {
      type: 'value',
      splitNumber: 10,
      min: 0,
      splitLine: {
        show: true,
        lineStyle: {
          color: ['rgba(38,153,100,0.6)'],
          width: 2,
          type: 'solid'
        }
      }
    },
    series: [
      {
        data: rValues.value,
        color: ['#269964'],
        type: 'line',
        areaStyle: {}
      },
      {
        data: wValues.value,
        color: ['#269964'],
        type: 'line',
        lineStyle: {
          type: 'dashed',
          width: 2, // 线宽
          dashArray: [5, 10] // 虚线段长度和间隔
        }
      }
    ],
  }
})
let Interval: NodeJS.Timeout
onMounted(() => {
  Interval = setInterval(() => {
    let data: number = Number(dataStore.data[`diskUtil-${id.value}`])
    //随机抖动-5,+5
    const jitter = Math.random() * 2 - 1
    data = Number((data + jitter).toFixed(2))
    values.value.push(data)
    values.value.shift()
    data = Number(dataStore.data[`diskReadKB-${id.value}`])
    data = Number((data + jitter).toFixed(2))
    rValues.value.push(data)
    rValues.value.shift()
    data = Number(dataStore.data[`diskWriteKB-${id.value}`])
    data = Number((data + jitter).toFixed(2))
    wValues.value.push(data)
    wValues.value.shift()
  }, 1000)
})
onBeforeUnmount(() => {
  clearInterval(Interval)
})
</script>

<template>
  <div class="disk-layout">
    <div class="left-chart">
      <div class="disk-title">
        <h1>驱动器 {{ $route.params.id }}</h1>
        <h2>{{ lsDisk.find(x => x.name === 'Device_Model' || x.name === 'Model_Number')?.value }}</h2>
      </div>
      <div class="disk-chart-top">
        <p>活动时间</p>
        <p>100%</p>
      </div>
      <div class="disk-chart">
        <v-chart
            :option="option"
            autoresize
            class="chart"/>
      </div>
      <div class="disk-chat-bottom">
        <p>1分钟</p>
        <p>0</p>
      </div>
      <div class="disk-chart-top">
        <p>吞吐量</p>
        <p></p>
      </div>
      <div class="disk-chart">
        <v-chart
            :option="RWOption"
            autoresize
            class="chart"/>
      </div>
      <div class="disk-chat-bottom">
        <p>1分钟</p>
        <p>0</p>
      </div>
    </div>
    <div class="right-info">
      <div class="info-title-box">
        <div class="info-title">
          <p>使用率</p>
          <h2>{{ dataStore.data[`diskUtil-${id}`] }}%</h2>
        </div>
        <div class="info-title">
          <p>平均响应时间</p>
          <h2>{{ dataStore.data[`diskAwait-${id}`] }} ms</h2>
        </div>
        <div class="infos">
          <div class="info2">
            <div></div>
            <p>读取速度</p>
            <h2>{{ dataStore.data[`diskReadKB-${id}`] }} KiB/s</h2>
          </div>
          <div class="info2">
            <div></div>
            <p>写入速度</p>
            <h2>{{ dataStore.data[`diskWriteKB-${id}`] }} KiB/s</h2>
          </div>
        </div>
      </div>
      <div class="other-info-box">
        <div v-for="value in lsDisk" class="other-info">
          <p>{{ value.name }}</p>
          <p>{{ value.value }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import "base";

.disk-layout {
  padding: $padding*1.5;
  display: grid;
  grid-template-columns: 1fr 300px;
  grid-template-rows: 1fr;
  gap: $gap*2;
}

.left-chart {
  display: flex;
  flex-direction: column;
  gap: $gap;

  .disk-title {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .dark-mode & {
      h1, h2 {
        color: $dark-text-color;
      }
    }

    h1, h2 {
      color: $light-text-color;
    }

    h1 {
      font-size: 32px;
    }
  }
}

.disk-chart-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.disk-chart {
  flex: 1;
  border: 4px solid #269964;
  border-radius: $radius*2;
  height: min-content;
  max-height: 32rem;
  min-height: 24rem;
  overflow: hidden;
}

.disk-chat-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-title-box {
  display: flex;
  flex-wrap: wrap;
  gap: $gap*2;

  .info-title {
    display: flex;
    flex-direction: column;
    gap: $gap*.5;
  }

  .info-title:nth-of-type(2) {
    width: 60%;
  }
}

.other-info-box {
  display: flex;
  flex-direction: column;
  gap: $gap*.5;

  .other-info {
    display: flex;
    gap: $gap;

    p:first-of-type {
      font-weight: 500;
    }

    p {
      flex: 1;
      //文本换行
      word-wrap: break-word;
    }
  }
}

.right-info {
  display: flex;
  flex-direction: column;
  gap: $gap*2;
}

.infos {
  display: flex;
  flex-direction: column;
  gap: $gap*2;

  .info2 {
    display: grid;
    grid-template-columns: 4px 1fr;
    grid-template-rows: 1fr 1fr;
    grid-column-gap: $gap;

    & > div {
      height: 100%;
      border: 3px solid #269964;
      grid-row: 1/3;
    }
  }

  .info2:last-of-type {
    & > div {
      height: 100%;
      border: 3px dashed #269964;
      grid-row: 1/3;
    }
  }
}
</style>