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

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

export default {
  name: "ArMonthlyCompanyBar",
  data() {
    return {
      arMonthlyCompanyBar: 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: "XXXX-XX 已开票未收款（结算单位）",
          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股份有限公司'],
          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: [25002, 57873, 11987, 71168, 41098, 25002, 57873, 11987, 71168, 41098],
          barWidth: "60%",
          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
          },
        }
      },
    }
  },
  methods: {
    convert_num_unit,
    // 绘制图表
    drawArMonthlyCompanyBar() {
      if (!this.arMonthlyCompanyBar) {
        this.arMonthlyCompanyBar = echarts.init(document.getElementById('ar_monthly_company_bar'))

        // 监听窗口大小变化，重绘图表
        window.addEventListener('resize', this.resizeChart);
      }
      this.arMonthlyCompanyBar.setOption(this.barChart);
    },
    resizeChart() {
      if (this.arMonthlyCompanyBar) {
        this.arMonthlyCompanyBar.resize();
      }
    },
  },
  mounted() {
    // 调整图表标题
    let month = this.$route.query.month
    if (!month) {
      this.$router.push("/ar_bar/monthly")
    }
    this.barChart.title.text = month + " 已开票未收款（结算单位）"
    // 获取图表数据
    arMonthlyCompanyBar(month).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.drawArMonthlyCompanyBar()
    }).catch(err => {
      console.log(err);
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeChart);
  }
}
</script>
