<template>
  <div id="ar_summary_bar" @click="goArBar"/>
</template>

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

export default {
  name: "ArSummaryBar",
  data() {
    return {
      barChart: {
        textStyle: {
          color: "#FFFFFF",
          fontSize: "12"
        },
        grid: {
          containLabel: true,
          top: "10%",
          bottom: '0'
        },
        xAxis: {
          data: ['未结算未开票', '已结算未开票', '已开票未收款', '应收款', '已开票已收款'],
          axisLabel: {
            color: "#FFFFFF",
            fontSize: "12",
          },
          axisTick: {
            show: false
          }
        },
        yAxis: [{
          type: "value",
          splitLine: {
            lineStyle: {
              color: "#1E2C58"
            }
          },
          axisLabel: {
            show: false,
            color: "#FFFFFFAA",
            fontSize: "16"
          }
        }],
        series: [
          {
            type: 'bar',
            data: [50002, 57873, 11987, 71168, 41098],
            barWidth: "40%",
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(255, 255, 255, 0.1)'
            },
            itemStyle: {
              barBorderRadius: 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",
            },
          }
        ]
      },
      myChart: null
    }
  },
  methods: {
    goArBar() {
      this.$router.push({path: "/ar_bar"})
    },
    resizeChart() {
      if (this.myChart) {
        this.myChart.resize();
      }
    }
  },
  mounted() {
    // 获取图表数据
    window.axios.post("/api/query/dash_board/action/get_ar_summary_bar", {
      headers: {'content-type': 'application/json'}
    }).then(res => {
      this.barChart.series[0].data = res.data.data;
    }).catch(err => {
      console.log("获取应收账款数据失败", err);
    });
    // 设置500毫秒延迟执行，解决组件没有渲染完成就开始绘制图表的问题
    setTimeout(() => {
      this.arSummaryBar = echarts.init(document.getElementById('ar_summary_bar'))
      this.arSummaryBar.setOption(this.barChart);
      // 监听窗口大小变化，重绘图表
      window.addEventListener('resize', this.resizeChart);
    }, 500)
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeChart);
  }

}

</script>
