<template>
  <div class="member-position-distribution">
    <div class="chart-section">
      <div class="sub-title">职业资格等级分布</div>
      <div class="chart-container" ref="qualificationChart"></div>
    </div>
    <div class="chart-section">
      <div class="sub-title">开机年限分布</div>
      <div class="chart-container" ref="experienceChart"></div>
    </div>
    <div class="chart-section">
      <div class="sub-title">岗位资质分布</div>
      <div class="chart-container" ref="positionChart"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'MemberPositionDistribution',
  props: {
    chartsData: {
      type: Object,
      default: () => ({
        qualification: { labels: ['初级', '中级', '高级', '技师'], data: [50, 120, 80, 68] },
        experience: { labels: ['1-3年', '3-5年', '5-10年', '10年以上'], data: [60, 80, 100, 78] },
        position: { labels: ['开机员', '开包员', '引导员', '指挥员'], data: [100, 80, 60, 78] }
      })
    }
  },
  data() {
    return {
      charts: {}
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initCharts()
    })
  },
  watch: {
    chartsData: {
      deep: true,
      handler() {
        this.$nextTick(() => {
          this.disposeCharts()
          this.initCharts()
        })
      }
    }
  },
  methods: {
    disposeCharts() {
      Object.values(this.charts).forEach(chart => {
        if (chart) chart.dispose()
      })
      this.charts = {}
    },
    initCharts() {
      this.initQualificationChart()
      this.initExperienceChart()
      this.initPositionChart()
    },
    initQualificationChart() {
      if (!this.$refs.qualificationChart) return
      this.charts.qualification = echarts.init(this.$refs.qualificationChart)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        grid: {
          left: '10%',
          right: '5%',
          bottom: '14%',
          top: '10%'
        },
        xAxis: {
          type: 'category',
          data: this.chartsData.qualification.labels,
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.05)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10
          }
        },
        series: [{
          type: 'bar',
          data: this.chartsData.qualification.data,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#A78BFA'
              }, {
                offset: 1, color: '#6366F1'
              }]
            }
          },
          barWidth: '60%'
        }]
      }
      this.charts.qualification.setOption(option)
    },
    initExperienceChart() {
      if (!this.$refs.experienceChart) return
      this.charts.experience = echarts.init(this.$refs.experienceChart)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        grid: {
          left: '10%',
          right: '5%',
          bottom: '14%',
          top: '10%'
        },
        xAxis: {
          type: 'category',
          data: this.chartsData.experience.labels,
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.05)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10
          }
        },
        series: [{
          type: 'bar',
          data: this.chartsData.experience.data,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#34D399'
              }, {
                offset: 1, color: '#059669'
              }]
            }
          },
          barWidth: '60%'
        }]
      }
      this.charts.experience.setOption(option)
    },
    initPositionChart() {
      if (!this.$refs.positionChart) return
      this.charts.position = echarts.init(this.$refs.positionChart)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        grid: {
          left: '10%',
          right: '5%',
          bottom: '20%',
          top: '10%'
        },
        xAxis: {
          type: 'category',
          data: this.chartsData.position.labels,
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10,
            rotate: 30,
            margin: 15
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.05)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10
          }
        },
        dataZoom: [{
          type: 'slider',
          show: true,
          xAxisIndex: 0,
          height: 20,
          bottom: 0,
          borderColor: 'rgba(255, 255, 255, 0.1)',
          backgroundColor: 'rgba(45, 42, 85, 0.8)',
          fillerColor: 'rgba(167, 139, 250, 0.3)',
          handleStyle: {
            color: '#A78BFA'
          },
          labelStyle: {
            color: 'rgba(255, 255, 255, 0.5)'
          }
        }],
        series: [{
          type: 'bar',
          data: this.chartsData.position.data,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#FBBF24'
              }, {
                offset: 1, color: '#F59E0B'
              }]
            }
          },
          barWidth: '60%'
        }]
      }
      this.charts.position.setOption(option)
    }
  },
  beforeDestroy() {
    Object.values(this.charts).forEach(chart => {
      if (chart) chart.dispose()
    })
  }
}
</script>

<style lang="scss" scoped>
.member-position-distribution {
  .chart-section {
    margin-bottom: 32rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
  
  .sub-title {
    font-size: 24rpx;
    color: rgba(255, 255, 255, 0.5);
    margin-bottom: 16rpx;
  }
  
  .chart-container {
    height: 600rpx;
  }
}
</style>
