<template>
  <BaseCard title="北京走势图">
    <div ref="chartRef" class="chart"></div>
  </BaseCard>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'
import BaseCard from './BaseCard.vue'

const chartRef = ref(null)
let chartInstance = null

// 模拟数据
const months = ['一月', '二月', '三月', '四月', '五月', '六月']
const data = [20, 40, 60, 80, 60, 100]

const initChart = () => {
  if (!chartRef.value) return

  chartInstance = echarts.init(chartRef.value)
  
  const option = {
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
      borderColor: '#409EFF',
      textStyle: {
        color: '#fff',
        fontSize: 12
      },
      formatter: function(params) {
        return `${params[0].name}<br/>转牛数量: ${params[0].value}头`
      }
    },
    grid: {
      left: '8%',
      right: '8%',
      bottom: '15%',
      top: '10%'
    },
    xAxis: {
      type: 'category',
      data: months,
      axisLine: {
        lineStyle: {
          color: '#4a5568'
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#a0a8b8',
        fontSize: 11
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          color: '#2d3748',
          type: 'dashed'
        }
      },
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#a0a8b8',
        fontSize: 11
      }
    },
    series: [{
      name: '转牛数量',
      type: 'bar',
      data: data,
      barWidth: '60%',
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: '#409EFF' },
          { offset: 1, color: '#1976D2' }
        ]),
        borderRadius: [4, 4, 0, 0]
      },
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowColor: 'rgba(64, 158, 255, 0.8)'
        }
      }
    }]
  }

  chartInstance.setOption(option)

  // 添加动态效果
  let currentIndex = 0
  const timer = setInterval(() => {
    chartInstance.dispatchAction({
      type: 'showTip',
      seriesIndex: 0,
      dataIndex: currentIndex
    })
    currentIndex = (currentIndex + 1) % data.length
  }, 3000)

  chartInstance._autoTimer = timer
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', () => {
    chartInstance?.resize()
  })
})

onUnmounted(() => {
  if (chartInstance) {
    if (chartInstance._autoTimer) {
      clearInterval(chartInstance._autoTimer)
    }
    chartInstance.dispose()
  }
})
</script>

<style scoped>
.chart {
  width: 100%;
  height: 100%;
}
</style> 