<template>
  <!-- 档案总数 -->
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { archivesStatistics } from "@/api/dashboard";

const animationDuration = 4200;
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
      // default: (window.innerHeight-260)/2+"px"
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      chart: null,
      chartOption:{
        tooltip:{
          trigger:'axis',
          axisPointer:{
            type:'shadow'
          }
        },
        xAxis: {
          type: 'category',
          data: [],
          axisLine: {
            show: false, //不显示坐标轴轴线
            lineStyle: {
              color: "#a0a0a0",
            },
          },
          axisLabel: {
            interval: 0, // 坐标轴刻度标签的显示间隔
            rotate: 40, // 标签倾斜的角度
            textStyle: {
              fontSize: 10, //刻度大小
              color: "#a0a0a0",
            },
          },
        },
        grid: {
          top: "6%",
          left: "3%",
          right: "0%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: {
          type: 'value',
          splitLine: {
            show: false, // 不显示网格线
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false, //不显示坐标轴轴线
            lineStyle: {
              color: "#a0a0a0",
            },
          },
          axisLabel: {
            textStyle: {
              fontSize: 10, //刻度大小
              color: "#a0a0a0",
            },
          },
        },
        series: [
          {
            data: [],
            type: 'bar',
            barWidth:15,
            label:{
              normal:{
                show:true,
                position:'top'
              }
            },
            itemStyle:{
              normal:{
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#fe1000'},
                  { offset: 0.5, color: '#fd8900'},
                  { offset: 1, color: '#fae100' }
                ])
              }
            }
          }
        ]
      }
    };
  },
  methods: {
    archivesStatistics() {
      archivesStatistics().then((res) => {
        if (res.code === "100") {
          console.log(res);
          let sumAll = null;
          res.body.list.forEach((ele) => {
            sumAll = sumAll + ele.value;
          });
          this.$emit("sunAll", sumAll);
          let _result = res.body.list;
          
          let _newArr = [];
          let _xAxisData = [];
          _result.forEach((item)=>{
            _newArr.push(item.value);
            _xAxisData.push(item.name);
          })
          this.chartOption.xAxis.data = _xAxisData;
          this.chartOption.series[0].data = _newArr;
          this.chart.setOption(this.chartOption);
        }
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.archivesStatistics();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
};
</script>
