<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 { baoguanqixian } from "@/api/dashboard";

const animationDuration = 3800;
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: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        grid: {
          top: "6%",
          left: "0%",
          right: "0%",
          bottom: "3%",
          containLabel: true,
        },
        legend: {
          show:false,
          left: 'center',
          top: 'bottom',
          data: []
        },
        series: [
          {
            name: '保管期限',
            type: 'pie',
            radius: [20, 100],
            center: ['50%', '50%'],
            roseType: 'area',
            itemStyle: {
              borderRadius: 5
            },
            data: []
          }
        ]
      },
    };
  },
  methods: {
    baoguanqixian() {
      baoguanqixian().then((res) => {
        if (res.code === "100") {
          let _seriesData = [];
          let _legend = [];
          res.body.list.forEach((ele) => {
            _legend.push(ele.name)
            _seriesData.push({value:ele.count,name:ele.name});
          });
          this.chartOption.legend.data = _legend;
          this.chartOption.series[0].data = _seriesData;
          this.chart.setOption(this.chartOption);
        }
      });
      
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.baoguanqixian();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
};
</script>
