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

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

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

async function loadSafeCount() {
  loading.value = true

  try {
    const res = await IotStatApi.getSafeCount()
    safeCount.value = toNumber(res)
  } catch (error) {
    console.error('获取库存预警数量失败:', error)
    safeCount.value = 0
  } finally {
    loading.value = false
  }
}

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

<template>
  <div class="panel flex flex-col">
    <div class="panel-title panel-title--lg">
      <div class="icon-decorator">
        <span></span>
        <span></span>
      </div>
      库存预警物料数量
    </div>

    <div
      class="metric-panel flex-1 min-h-0"
      style="
        /* stylelint-disable-next-line custom-property-empty-line-before */
        --metric-accent: #ef6b6f;
        --metric-glow: rgb(239 107 111 / 18%);
        --metric-border: rgb(239 107 111 / 14%);
        --metric-tag-bg: rgb(239 107 111 / 10%);
      "
    >
      <div class="metric-panel__orbit metric-panel__orbit--outer"></div>
      <div class="metric-panel__orbit metric-panel__orbit--inner"></div>

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

      <div class="metric-panel__body">
        <div class="metric-panel__center">
          <div class="metric-panel__headline">当前预警物料</div>
          <div class="metric-panel__value-row">
            <CountTo
              class="metric-panel__value"
              :start-val="0"
              :end-val="safeCount"
              :duration="1200"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

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

.metric-panel {
  position: relative;
  display: grid;
  padding: calc(16px * var(--kb-scale, 1)) calc(14px * var(--kb-scale, 1))
    calc(20px * var(--kb-scale, 1));
  overflow: hidden;
  grid-template-rows: auto minmax(0, 1fr);
  gap: calc(16px * var(--kb-scale, 1));
}

.metric-panel::before {
  position: absolute;
  inset: calc(54px * var(--kb-scale, 1)) calc(18px * var(--kb-scale, 1))
    calc(16px * var(--kb-scale, 1));
  pointer-events: none;
  background: radial-gradient(
    circle at center,
    rgb(255 255 255 / 20%) 0%,
    rgb(255 255 255 / 0%) 72%
  );
  content: '';
}

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

.metric-panel__orbit--outer {
  width: calc(240px * var(--kb-scale, 1));
  height: calc(240px * var(--kb-scale, 1));
}

.metric-panel__orbit--inner {
  width: calc(188px * var(--kb-scale, 1));
  height: calc(188px * var(--kb-scale, 1));
  border-color: rgb(255 255 255 / 42%);
  opacity: 0.55;
}

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

.metric-panel__body {
  display: flex;
  min-height: 0;
  align-items: center;
  justify-content: center;
}

.metric-panel__tag {
  display: inline-flex;
  padding: calc(5px * var(--kb-scale, 1)) calc(12px * var(--kb-scale, 1));
  font-family: YouSheBiaoTiHei, sans-serif;
  font-size: calc(16px * var(--kb-scale, 1));
  line-height: 1;
  letter-spacing: 2px;
  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;
  width: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.metric-panel__headline {
  font-size: calc(22px * var(--kb-scale, 1));
  font-weight: 600;
  letter-spacing: 2px;
  color: #4f678a;
  text-shadow: 0 4px 12px rgb(255 255 255 / 35%);
}

.metric-panel__value-row {
  display: flex;
  margin-top: calc(18px * var(--kb-scale, 1));
  align-items: flex-end;
  justify-content: center;
}

.metric-panel__value {
  font-family: YouSheBiaoTiHei, sans-serif;
  font-size: calc(90px * var(--kb-scale, 1));
  line-height: 0.88;
  letter-spacing: 2px;
  color: var(--metric-accent);
  text-shadow: 0 10px 18px var(--metric-glow);
}
</style>
