<template>
  <div class="event-statistics">
    <div
        id="eventStatisticsChart"
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0,0,0,0.5)"
        style="height: 293px;"
    />
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "EventStatistics",
  data() {
    return {
      loading: true
    }
  },

  watch: {
    "$store.state.chooseArea.chooseName"(val) {
      if (val.orgId) {
        this.getData();
      }
    }
  },
  mounted() {
    if (this.$store.state.chooseArea.chooseName.orgId) {
      this.getData();
    }
  },
  methods: {
    getData() {
      this.loading = true
      let params = {
        level: this.$store.state.chooseArea.chooseName.level,
        orgId: this.$store.state.chooseArea.chooseName.orgId,
      }
      params = this.$paramsFormat(params)
      this.$http.get('/governance/satisfactionOverview/childSatisfactionGroup?' + params).then(({data: {data}}) => {
        this.initCharts(data)
      })
    },
    initCharts({orgList, series}) {
      let div = document.getElementById('eventStatisticsChart');
      this.myChart = echarts.init(div);

      let xData = orgList;
      let color = [[
        {
          offset: 0,
          color: 'rgba(152, 72, 255, 0)'
        },
        {
          offset: 1,
          color: '#A962FF'
        }
      ], [
        {
          offset: 0,
          color: 'rgba(255, 228, 102, 0)' // 0% 处的颜色
        },
        {
          offset: 1,
          color: 'rgba(255, 228, 102, 1)' // 100% 处的颜色
        }
      ], [
        {
          offset: 0,
          color: 'rgba(0, 84, 255, 0)' // 0% 处的颜色
        },
        {
          offset: 1,
          color: 'rgba(38, 244, 248, 1)' // 100% 处的颜色
        }
      ]]
      let seriesArray = series.map((item, index) => {
        return {
          name: item.name,
          type: 'bar',
          barWidth: 14,
          itemStyle: {
            opacity: 1,
            color: new echarts.graphic.LinearGradient(
                0,
                1,
                0,
                0,
                color[index],
                false
            )
          },
          data: item.data,
        }
      })
      console.log(seriesArray)
      let num = []
      series.forEach(item => {
        num.push(...item.data)
      })
      const max = Math.max(...num)

      let barArray = new Array(xData.length).fill((parseInt(max / 100) + 1) * 100)
      var option = {
        title: {
          show: false,
          text: '',
          x: 'center',
          top: '15px',
          textStyle: {
            color: '#333333',
            fontWeight: 500,
            fontSize: 18,
          },
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(13,33,71,0.5)',
          borderColor: 'rgba(143,174,252,0.6)',
          padding: 8,
          textStyle: {
            color: '#fff',
          },
          formatter: function (params) {
            console.log(params)
            var res = '<div"><p>' + params[0].name + '</p></div>'
            for (var i = 0; i < params.length; i++) {
              if (params[i].seriesName != "") {
                res += '<p>' + params[i].seriesName + ':' + params[i].data + '</p>'
              }
            }
            return res;
          },
        },
        legend: {
          // icon: 'circle',
          data: series.map(item => item.name),
          itemGap: 20,
          itemWidth: 6,
          itemHeight: 6,
          x: 'right',
          top: '3%',
          textStyle: {
            color: '#fff',
            fontSize: 14,
            // padding:[0, 10, 0, 10],
          }
        },
        grid: {
          top: '18%',
          left: '2%',
          right: '4%',
          bottom: '8%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: '#323c41'
              }
            },
            axisLabel: {
              align: 'center',
              textStyle: {
                fontSize: 12,
                color: '#A3B9DA'
              }
            },
            boundaryGap: true,
            data: xData,
          }, {
            type: 'category',
            data: xData,
            axisLine: {show: false},
            axisLabel: {
              show: false,
            },
            boundaryGap: true,

          }
        ],

        yAxis: [
          {
            type: 'value',
            name: '单位：人',
            nameLocation: 'end',
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(4,187,255,0.18)',
                type: 'dashed'
              },
            },
            nameTextStyle: {
              color: '#A3B9DA',
              textStyle: {
                fontSize: 12
              },
              align: 'center'
            },
            axisLabel: {
              show: true,
              color: '#A3B9DA',
              textStyle: {
                fontSize: 12
              }
            },
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          ...seriesArray,
          {
            name: '',
            type: 'bar',

            barWidth: 100,
            barGap: '-60%',
            data: barArray,
            itemStyle: {
              normal: {
                color: 'rgba(22,153,152, 0.1)'
              }
            },
            zlevel: -1,
            xAxisIndex: 1,
          }
        ],
      };
      this.myChart.setOption(option);
      window.addEventListener("resize", () => this.myChart.resize());
      this.loading = false
    },
  }
}
</script>

<style scoped lang="scss">
.event-statistics {
  padding: 16px 16px;
}
</style>