<template>
  <div class="unsafe-position-chart">
    <div class="chart-container" ref="positionChart"></div>
    <div class="position-info">
      <div class="info-text">X光机操作员: {{ chartsData.total }}</div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'UnsafePositionChart',
  props: {
    chartsData: {
      type: Object,
      default: () => ({
        total: 279,
        positions: {
          labels: ['X光机', '开包台', '引导岗', '验证岗', '维序岗'],
          data: [279, 0, 0, 0, 0]
        }
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  watch: {
    chartsData: {
      deep: true,
      handler() {
        this.$nextTick(() => {
          if (this.chart) this.chart.dispose()
          this.initChart()
        })
      }
    }
  },
  methods: {
    initChart() {
      if (!this.$refs.positionChart) return
      this.chart = echarts.init(this.$refs.positionChart)
      const option = {
        responsive: true,
        maintainAspectRatio: false,
        grid: {
          left: '15%',
          right: '5%',
          bottom: '15%',
          top: '10%'
        },
        xAxis: {
          type: 'category',
          data: this.chartsData.positions.labels,
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            }
          },
          axisLabel: {
            color: 'rgba(255, 255, 255, 0.5)',
            fontSize: 10,
            // rotate: 30
          }
        },
        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.positions.data,
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#F472B6'
              }, {
                offset: 1, color: '#DB2777'
              }]
            }
          },
          barWidth: '60%'
        }]
      }
      this.chart.setOption(option)
    }
  },
  beforeDestroy() {
    if (this.chart) this.chart.dispose()
  }
}
</script>

<style lang="scss" scoped>
.unsafe-position-chart {
  .chart-container {
    height: 240rpx;
  }
  
  .position-info {
    margin-top: 16rpx;
    text-align: center;
    
    .info-text {
      font-size: 24rpx;
      color: rgba(255, 255, 255, 0.5);
    }
  }
}
</style>
