<template>
  <div class="kpi-container">
    <dv-border-box-10 class="kpi-card">
      <div class="chart-title">今日告警</div>
      <div class="kpi-value">6549.69<span class="monitor-unit">个</span></div>
    </dv-border-box-10>
    <dv-border-box-10 class="kpi-card online-monitor">
      <div class="chart-title">在线监控数</div>
      <div class="kpi-value">
        <dv-border-box-12 class="monitor-box">
          <div class="monitor-title">当前在线监控</div>
          <div class="monitor-value">13,365<span class="monitor-unit">台</span></div>
        </dv-border-box-12>
      </div>
    </dv-border-box-10>
    <dv-border-box-10 class="kpi-card algorithm-card">
      <div class="chart-title">算法执行</div>
      <div class="algorithm-content">
        <div class="algorithm-subtitle">累计执行次数</div>
        <div class="flip-counter">
          <div class="flip-digit" v-for="(digit, index) in digits" :key="index">
            <div class="digit-scroll" :style="{ transform: `translateY(${-digit.current * 10}%)` }">
              <span v-for="n in 10" :key="n">{{ n }}</span>
            </div>
          </div>
        </div>
      </div>
    </dv-border-box-10>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 初始显示22415
const digits = ref([
  { current: 2, target: 2 },
  { current: 2, target: 2 },
  { current: 4, target: 4 },
  { current: 1, target: 6 },
  { current: 5, target: 4 },
  { current: 5, target: 4 }
])

const animateDigits = () => {
  // 3秒后开始变化到22464
  setTimeout(() => {
    digits.value.forEach((digit, index) => {
      setTimeout(() => {
        let steps = Math.abs(digit.target - digit.current)
        if (steps > 5) steps = 10 - steps
        
        const duration = 1000
        const startTime = Date.now()
        
        const animate = () => {
          const elapsed = Date.now() - startTime
          const progress = Math.min(elapsed / duration, 1)
          
          digit.current = Math.floor(digit.current + progress * (digit.target - digit.current))
          if (digit.current > 9) digit.current = 0
          if (digit.current < 0) digit.current = 9
          
          if (progress < 1) {
            requestAnimationFrame(animate)
          } else {
            digit.current = digit.target
          }
        }
        
        animate()
      }, index * 300)
    })
  }, 1000)
}

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

<style scoped>
/* 基础样式 */
.kpi-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 80vh;
  padding: 20px 0;
  box-sizing: border-box;
}

.kpi-card {
  width: 91%;
  height: 25vh;
  margin: 10px 0;
  /* display: flex; */
  /* flex-direction: column;
  justify-content: center;
  align-items: center; */
}

.chart-title {
  font-size: 16px;
  color: #7cd0ff;
  text-align: center;
  padding-top: 15px;
  font-weight: 500;
  letter-spacing: 1px;
}

.kpi-value {
  font-size: 28px;
  font-weight: bold;
  color: #fff;
  text-align: center;
  margin-top: 10%;
}

/* 在线监控数卡片样式 */
.online-monitor {
  border-color: rgba(124, 208, 255, 0.7);
}

.online-monitor ::v-deep .border-box-title {
  color: #7cd0ff !important;
  font-size: 16px;
  font-weight: normal;
}

.monitor-box {
  width: 50%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 15px 0;
  margin: 0 auto;
  background: #094290;
}

.monitor-title {
  color: #7cd0ff;
  font-size: 16px;
  font-weight: 400;
  margin-bottom: 10px;
  letter-spacing: 1px;
}

.monitor-value {
  color: #ffffff;
  font-size: 32px;
  font-weight: bold;
  letter-spacing: 1px;
  line-height: 1.2;
}

.monitor-unit {
  font-size: 16px;
  font-weight: normal;
  margin-left: 6px;
  color: rgba(255, 255, 255, 0.8);
  position: relative;
  top: -2px;
}

/* 算法执行卡片样式 */
.algorithm-card ::v-deep .border-box-title {
  display: none !important;
}

.algorithm-content {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 15px 0;
  margin-top: 4%;
}

.algorithm-subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin-bottom: 15px;
}

/* 数字翻牌器样式 */
.flip-counter {
  display: flex;
  justify-content: center;
  gap: 10px;
  height: 60px;
}

.flip-digit {
  width: 40px;
  height: 50px;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #1a5fb4, #3584e4);
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(53, 132, 228, 0.6);
}

.digit-scroll {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  transition: transform 0.8s cubic-bezier(0.25, 1, 0.5, 1);
}

.digit-scroll span {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: bold;
  color: white;
}
</style>