<template>
  <div class="card">
    <div class="card-header">
      <div class="card-title">
        <span class="card-title-icon">
          <Icon icon="fa-solid:gauge-high" />
        </span>
        <span>今日开机率/稼动率</span>
      </div>
      <span class="chip">{{ currentLineName }}</span>
    </div>
    <div class="card-body">
      <div id="chart-today" class="chart"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useChart, colors } from '../utils'

const { init, instance } = useChart('chart-today')

const lineMetrics = [
  { name: '产线A', run: 88, avail: 82 },
  { name: '产线B', run: 86, avail: 80 },
  { name: '产线C', run: 90, avail: 84 },
  { name: '产线D', run: 85, avail: 79 },
  { name: '产线E', run: 87, avail: 81 },
  { name: '产线F', run: 92, avail: 86 }
]

const currentLineName = ref(lineMetrics[0].name)
let timer: ReturnType<typeof setInterval> | null = null
let currentLineIndex = 0

const gaugeOption = (run: number, avail: number) => {
  return {
    backgroundColor: 'transparent',
    tooltip: { show: true },
    series: [
      {
        type: 'gauge',
        center: ['50%', '60%'],
        startAngle: 200,
        endAngle: -20,
        min: 0,
        max: 100,
        splitNumber: 10,
        radius: '88%',
        axisLine: { lineStyle: { width: 8, color: [[1, 'rgba(30,144,255,0.18)']] } },
        axisTick: { show: false },
        splitLine: { show: false },
        axisLabel: { show: false },
        pointer: { show: false },
        progress: { show: true, width: 8, itemStyle: { color: colors.blue } },
        detail: { formatter: '{value}%', color: '#fff', fontSize: 26, offsetCenter: ['0%', '-5%'] },
        title: { show: true, color: '#a8b7d8', fontSize: 12, offsetCenter: ['0%', '-25%'] },
        data: [{ value: run, name: '开机率' }]
      },
      {
        type: 'gauge',
        center: ['50%', '60%'],
        startAngle: 200,
        endAngle: -20,
        min: 0,
        max: 100,
        radius: '68%',
        axisLine: { lineStyle: { width: 8, color: [[1, 'rgba(16,185,129,0.18)']] } },
        axisTick: { show: false },
        splitLine: { show: false },
        axisLabel: { show: false },
        pointer: { show: false },
        progress: { show: true, width: 8, itemStyle: { color: colors.green } },
        detail: { formatter: '{value}%', color: '#fff', fontSize: 20, offsetCenter: ['0%', '45%'] },
        title: { show: true, color: '#a8b7d8', fontSize: 12, offsetCenter: ['0%', '25%'] },
        data: [{ value: avail, name: '稼动率' }]
      }
    ]
  }
}

onMounted(() => {
  const chart = init()
  if (!chart) return

  // Initial render
  chart.setOption(gaugeOption(lineMetrics[0].run, lineMetrics[0].avail))

  // Start rotation
  timer = setInterval(() => {
    currentLineIndex = (currentLineIndex + 1) % lineMetrics.length
    const item = lineMetrics[currentLineIndex]
    currentLineName.value = item.name
    
    // Update chart
    const c = instance()
    if (c) {
      c.setOption(gaugeOption(item.run, item.avail))
    }
  }, 3000)
})

onUnmounted(() => {
  if (timer) clearInterval(timer)
})
</script>

<style scoped>
.card {
  position: relative;
  display: flex;
  padding: 10px;
  overflow: hidden;
  background: linear-gradient(135deg, rgb(15 23 42 / 96%), rgb(15 23 42 / 88%));
  border: 1px solid rgb(30 64 175 / 85%);
  border-radius: 10px;
  box-shadow:
    0 18px 45px rgb(15 23 42 / 95%),
    0 0 0 1px rgb(15 23 42 / 100%),
    inset 0 0 0 1px rgb(56 189 248 / 5%);
  flex-direction: column;
}

.card::before,
.card::after {
  position: absolute;
  width: 13px;
  height: 13px;
  pointer-events: none;
  border: 1px solid rgb(56 189 248 / 75%);
  border-radius: 2px;
  content: "";
  opacity: 0.6;
}

.card::before {
  top: -1px;
  left: -1px;
  border-right: none;
  border-bottom: none;
}

.card::after {
  right: -1px;
  bottom: -1px;
  border-top: none;
  border-left: none;
}

.card-header {
  display: flex;
  padding-bottom: 4px;
  margin-bottom: 8px;
  border-bottom: 1px solid rgb(41 54 95 / 90%);
  align-items: center;
  justify-content: space-between;
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
  color: #e5f0ff;
}

.card-title-icon {
  color: #22d3ee;
}

.card-body {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  gap: 8px;
}

.chip {
  display: inline-flex;
  padding: 4px 10px;
  font-size: 12px;
  color: #94a3b8;
  background: rgb(15 23 42 / 75%);
  border: 1px solid rgb(148 163 184 / 50%);
  border-radius: 999px;
  align-items: center;
  gap: 6px;
}

.chart {
  width: 100%;
  height: 100%;
  min-height: 180px;
}

@media (width <= 1600px) {
  .chart { min-height: 160px; }
}
</style>
