<template>
  <div id="ar_company_bar"/>
</template>

<script>
import * as echarts from "echarts";
import {arCompanyBar} from "../../../api/apiList";
import {convert_num_unit} from "../../../utils/numUtil";

export default {
  name: "ArCompanyBar",
  data() {
    return {
      arCompanyBar: null,
      barChart: {
        tooltip: {
          trigger: 'axis',
          formatter: (params) => params[0].name + "<br>" + params[0].marker + "\t\t" + this.convert_num_unit(params[0]),
        },
        title: {
          show: true,
          text: "已开票未收款（结算单位）",
          left: "center",
          textStyle: {color: "white"}
        },
        textStyle: {
          color: "#FFFFFF",
          fontSize: "12"
        },
        grid: {
          containLabel: true,
          top: "10%",
          bottom: '10%'
        },
        xAxis: {
          data: [
            'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司',
            'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司',
            'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司',
            'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司', 'XXXXXX股份有限公司',
          ],
          axisLabel: {
            color: "#FFFFFF",
            fontSize: "12",
            interval: 0,
            rotate: 20
          },
          axisTick: {
            show: false
          },
        },
        yAxis: {
          type: "value",
          splitLine: {
            lineStyle: {
              color: "#1E2C58"
            }
          },
          axisLabel: {
            show: false,
            color: "#FFFFFFAA",
            fontSize: "16"
          }
        },
        series: {
          type: 'bar',
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          barWidth: "40%",
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(255, 255, 255, 0.1)'
          },
          itemStyle: {
            borderRadius: 10,
          },
          color: {
            type: 'linear',
            x: 0, y: 0, x2: 0, y2: 1,
            colorStops: [
              {offset: 0, color: '#00CCD2'},
              {offset: 1, color: '#00A2FF'}
            ]
          },
          label: {
            show: true,
            position: 'top',
            color: "#FFFFFF",
            formatter: this.convert_num_unit
          },
          emphasis: {
            focus: 'self',
            blurScope: 'coordinateSystem'
          }
        }
      },
    }
  },
  methods: {
    convert_num_unit,
    // 绘制图表
    drawArCompanyBar() {
      if (!this.arCompanyBar) {
        this.arCompanyBar = echarts.init(document.getElementById('ar_company_bar'))
        // 监听柱条点击事件，跳转子页面
        let that = this
        this.arCompanyBar.on('click', function (params) {
          that.$router.push({path: '/ar_bar/company/monthly', query: {company: params.name}})
        });
        // 监听窗口大小变化，重绘图表
        window.addEventListener('resize', this.resizeChart);
      }
      this.arCompanyBar.setOption(this.barChart);
    },
    // 重绘图表
    resizeChart() {
      if (this.arCompanyBar) {
        this.arCompanyBar.resize();
      }
    },
  },
  mounted() {
    // 获取图表数据
    arCompanyBar().then(res => {
      let data = res.data
      let xAxisData = []
      let seriesData = []
      for (let item of data) {
        xAxisData.push(item.settle_company)
        seriesData.push(item.unregister_amount)
      }
      this.barChart.xAxis.data = xAxisData
      this.barChart.series.data = seriesData
      this.drawArCompanyBar()
    }).catch(err => {
        console.log(err);
      }
    );
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeChart);
  }
}
</script>
