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

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

export default {
  name: "ArMonthlyBar",
  data() {
    return {
      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: ['2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08',],
          axisLabel: {
            color: "#FFFFFF",
            fontSize: "12",
            interval: 0,
            rotate: 0
          },
          axisTick: {
            show: false
          }
        },
        yAxis: {
          type: "value",
          splitLine: {
            lineStyle: {
              color: "#1E2C58"
            }
          },
          axisLabel: {
            show: false,
            color: "#FFFFFFAA",
            fontSize: "16"
          }
        },
        series: {
          type: 'bar',
          data: [25002, 57873, 71168, 41098, 25002, 57873, 71168, 41098],
          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'
          }
        }
      },
      arMonthlyBar: null
    }
  },
  methods: {
    convert_num_unit,
    // 绘制图表
    drawArMonthlyBar() {
      if (!this.arMonthlyBar) {
        this.arMonthlyBar = echarts.init(document.getElementById('ar_monthly_bar'))
        // 监听柱条点击事件，跳转子页面
        let that = this
        this.arMonthlyBar.on('click', function (params) {
          that.$router.push({path: '/ar_bar/monthly/company', query: {month: params.name}})
        });
        // 监听窗口大小变化，重绘图表
        window.addEventListener('resize', this.resizeChart);
      }
      this.arMonthlyBar.setOption(this.barChart);
    },
    // 重绘图表
    resizeChart() {
      if (this.arMonthlyBar) {
        this.arMonthlyBar.resize();
      }
    },
  },
  mounted() {
    // 获取图表数据
    let data = [{"unregister_amount":7.69,"settle_month":"2023-01"},{"unregister_amount":9.77,"settle_month":"2023-02"},{"unregister_amount":140.14,"settle_month":"2023-03"},{"unregister_amount":10.06,"settle_month":"2023-04"},{"unregister_amount":57.04,"settle_month":"2023-05"},{"unregister_amount":129.07,"settle_month":"2023-06"},{"unregister_amount":92.39,"settle_month":"2023-07"},{"unregister_amount":325.96,"settle_month":"2023-08"},{"unregister_amount":457.98,"settle_month":"2023-09"},{"unregister_amount":1758.57,"settle_month":"2023-10"},{"unregister_amount":970.54,"settle_month":"2023-11"}];
    let xAxisData = []
    let seriesData = []
    for (let item of data) {
      xAxisData.push(item.settle_month)
      seriesData.push(item.unregister_amount)
    }
    this.barChart.xAxis.data = xAxisData
    this.barChart.series.data = seriesData
    this.drawArMonthlyBar()
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeChart);
  }

}

</script>
