<script lang="ts" setup>
import CountTo from '@/components/count-to1.vue'
import { IotStatApi } from '@/api/pms/stat'

const mttr = ref(0)
const loading = ref(false)

function toNumber(value: unknown) {
  const num = Number(value)
  return Number.isFinite(num) ? num : 0
}

async function loadMttr() {
  loading.value = true

  try {
    const res = await IotStatApi.getMttr()
    mttr.value = toNumber(res)
  } catch (error) {
    console.error('获取 MTTR 失败:', error)
    mttr.value = 0
  } finally {
    loading.value = false
  }
}

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

<template>
  <div class="panel flex flex-col">
    <div class="panel-title h-10">
      <div class="icon-decorator">
        <span></span>
        <span></span>
      </div>
      MTTR(平均解决时间)
    </div>

    <div
      class="metric-panel flex-1 min-h-0"
      style="
        /* stylelint-disable-next-line custom-property-empty-line-before */
        --metric-accent: #21b7b1;
        --metric-glow: rgb(33 183 177 / 16%);
        --metric-border: rgb(33 183 177 / 14%);
        --metric-tag-bg: rgb(33 183 177 / 10%);
      "
    >
      <div class="metric-panel__orbit metric-panel__orbit--outer"></div>

      <div class="metric-panel__top">
        <span class="metric-panel__tag">REPAIR</span>
      </div>

      <div class="metric-panel__center">
        <div class="metric-panel__headline">平均处理时长</div>
        <div class="metric-panel__value-row translate-x-1">
          <CountTo
            class="metric-panel__value"
            :start-val="0"
            :end-val="mttr"
            :duration="1200"
            :decimals="1"
          />
          <span class="metric-panel__unit">h</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import url('@/styles/kb.scss');

.metric-panel {
  position: relative;
  display: flex;
  padding: 12px 12px 9px;
  overflow: hidden;
  flex-direction: column;
}

.metric-panel__orbit {
  position: absolute;
  left: 50%;
  border: 1px solid var(--metric-border);
  border-radius: 999px;
  transform: translateX(-50%);
}

.metric-panel__orbit--outer {
  top: 20px;
  width: 180px;
  height: 180px;
}

.metric-panel__top,
.metric-panel__center {
  position: relative;
  z-index: 1;
}

.metric-panel__tag {
  display: inline-flex;
  padding: 4px 10px;
  font-family: YouSheBiaoTiHei, sans-serif;
  font-size: 14px;
  line-height: 1;
  letter-spacing: 1px;
  color: var(--metric-accent);
  background: var(--metric-tag-bg);
  border: 1px solid rgb(255 255 255 / 72%);
  border-radius: 999px;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 78%);
}

.metric-panel__center {
  display: flex;
  margin: auto 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.metric-panel__headline {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  color: #4f678a;
}

.metric-panel__value-row {
  display: flex;
  margin-top: 10px;
  align-items: flex-end;
  justify-content: center;
}

.metric-panel__value {
  font-family: YouSheBiaoTiHei, sans-serif;
  font-size: 54px;
  line-height: 0.92;
  letter-spacing: 1px;
  color: var(--metric-accent);
  text-shadow: 0 10px 18px var(--metric-glow);
}

.metric-panel__unit {
  padding-bottom: 7px;
  margin-left: 4px;
  font-family: YouSheBiaoTiHei, sans-serif;
  font-size: 22px;
  line-height: 1;
  color: #5b789e;
}
</style>
