<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Object,
      default: () => ({ names: [], counts: [], title: '机构学员' })
    },
    theme: {
      type: String,
      default: 'dark'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    theme() {
      this.initChart()
    },
    chartData: {
      deep: true,
      handler() {
        this.initChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (this.chart) {
        this.chart.dispose() // Dispose old instance to apply new theme completely if needed, or just setOption
      }
      this.chart = echarts.init(this.$el, 'macarons')

      const names = this.chartData.names || []
      const counts = this.chartData.counts || []
      const title = this.chartData.title || '机构学员'
      
      const isDark = this.theme === 'dark';
      const textColor = isDark ? '#fff' : '#333';
      const axisLineColor = isDark ? '#fff' : '#333';
      const splitLineColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)';

      this.chart.setOption({
        title: {
          text: title,
          textStyle: {
            color: textColor
          },
          left: 'center'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 40,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: names,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
            color: axisLineColor
          },
          axisLine: {
            lineStyle: {
              color: axisLineColor
            }
          }
        }],
        yAxis: [{
          type: 'value',
          minInterval: 1, // Ensure integer ticks
          axisTick: {
            show: false
          },
          axisLabel: {
            color: axisLineColor
          },
          axisLine: {
            lineStyle: {
              color: axisLineColor
            }
          },
          splitLine: {
            lineStyle: {
              color: splitLineColor
            }
          }
        }],
        series: [{
          name: title,
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: counts,
          animationDuration: 6000,
          itemStyle: {
            color: function(params) {
              const colorList = [
                ['#4facfe', '#00f2fe'],
                ['#43e97b', '#38f9d7'],
                ['#fa709a', '#fee140'],
                ['#a18cd1', '#fbc2eb'],
                ['#ff9a9e', '#fecfef'],
                ['#667eea', '#764ba2'],
                ['#f093fb', '#f5576c'],
                ['#8ec5fc', '#e0c3fc']
              ];
              const index = params.dataIndex % colorList.length;
              const color = colorList[index];
              return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: color[0] },
                { offset: 1, color: color[1] }
              ]);
            },
            borderRadius: [4, 4, 0, 0] // Rounded top
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowColor: 'rgba(0,0,0,0.3)'
            }
          }
        }]
      })
    }
  }
}
</script>
