<template>
  <div ref="chartContainer" class="bar-chart-minute-container"></div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
import * as echarts from 'echarts'

// 定义组件属性
interface Props {
  chartData?: Array<{ name: string; value: number }>
  title?: string
  color?: string
}

const props = withDefaults(defineProps<Props>(), {
  chartData: () => [],
  title: '',
  color: '#4CAF50' // 固定使用绿色
})

// 图表容器引用和实例
const chartContainer = ref<HTMLDivElement | null>(null)
let chartInstance: echarts.ECharts | null = null

// 初始化图表
const initChart = () => {
  if (chartContainer.value) {
    chartInstance = echarts.init(chartContainer.value)
    updateChart()
  }
}

// 更新图表
const updateChart = () => {
  if (!chartInstance || !props.chartData.length) return

  const option = {
    title: {
      // text: props.title,
      textStyle: {
        color: '#fff',
        fontSize: 14
      },
      left: 'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      backgroundColor: 'rgba(0, 0, 0, 0.7)',
      borderColor: 'rgba(30, 120, 255, 0.4)',
      borderWidth: 1,
      textStyle: {
        color: '#fff'
      }
    },
    grid: {
      left: '1%',
      right: '1%',
      top: '5%',
      bottom: '1%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: props.chartData.map(item => item.name),
      axisLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.3)'
        }
      },
      axisLabel: {
        color: 'rgba(255, 255, 255, 0.7)',
        rotate: 0,
        interval: Math.ceil(props.chartData.length / 24), // 每小时显示一个标签
        formatter: (value: string) => {
          // 只显示小时:00:00的标签
          if (value.endsWith(':00:00')) {
            return value.split(':')[0] + '时'
          }
          return ''
        },
        margin: 10
      },
      axisTick: {
        interval: Math.ceil(props.chartData.length / 24)
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.3)'
        }
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.1)'
        }
      },
      axisLabel: {
        color: 'rgba(255, 255, 255, 0.7)'
      }
    },
    series: [
      {
        type: 'bar',
        data: props.chartData.map(item => item.value),
        barWidth: 1, // 设置柱子宽度为1px
        itemStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#4CAF50' // 固定使用绿色
              },
              {
                offset: 1,
                color: '#4CAF5080' // 绿色带透明度
              }
            ]
          },
          borderRadius: [1, 1, 0, 0]
        },
        emphasis: {
          itemStyle: {
            color: '#4CAF50' // 固定使用绿色高亮
          }
        }
      }
    ]
  }

  chartInstance.setOption(option)
}

// 监听数据变化
watch(
  () => props.chartData,
  () => {
    updateChart()
  },
  { deep: true }
)

// 窗口大小改变时重绘图表
const handleResize = () => {
  if (chartInstance) {
    chartInstance.resize()
  }
}

// 组件挂载时初始化图表
onMounted(() => {
  setTimeout(() => {
    initChart()
  }, 200)
  window.addEventListener('resize', handleResize)
})

// 组件销毁前清理资源
onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
  if (chartInstance) {
    chartInstance.dispose()
    chartInstance = null
  }
})
</script>

<style scoped>
.bar-chart-minute-container {
  width: 100%;
  height: 100%;
  /* min-height: 200px; */
}
</style>