<template>
  <div id="factoryStoreChart" style="width: 100%; height: 100%"></div>
</template>
<script>
import { debounce } from "@/utils/debounce";
import * as echarts from "echarts";
export default {
  name: "Store",
  props: {
    stock: {
      type: Object,
      required: true,
    },
  },
  computed: {
    isOpen() {
      return this.$store.getters.sidebar.opened;
    },
  },
  data() {
    return {
      isFullscreen: false,
      actualOptions: null,
      chart: "",
      options: {
        grid: {
          left: "8%",
          right: "1%",
          bottom: "8%",
          top: "15%",
        },
        tooltip: {},
        xAxis: {
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "#4561AE",
            },
          },
          axisLabel: {
            color: "rgba(255, 255, 255, 0.7)",
            fontSize: 12,
          },
          data: [],
        },
        yAxis: {
          name: "单位/片",
          nameTextStyle: {
            color: "rgba(255, 255, 255, 0.7)",
            fontSize: 12,
            align: "right",
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "rgba(255, 255, 255, 0.7)",
            fontSize: 12,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#4561AE",
            },
          },
          splitLine: {
            lineStyle: {
              color: "#4561AE",
            },
          },
        },
        series: [
          {
            name: "", // this.series[0].name,
            type: "bar",
            barWidth: 12,
            itemStyle: {
              borderRadius: [10, 10, 0, 0],
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#12f7f1", // 0% 处的颜色
                  },
                  {
                    offset: 0.35,
                    color: "#12f7f177", // 100% 处的颜色
                  },
                  {
                    offset: 0.75,
                    color: "#12f7f133", // 100% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "transparent", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
            data: [], // this.series[0].data,
          },
        ],
      },
    };
  },
  watch: {
    stock(val) {
      this.initChart();
    },
    isOpen(val) {
      this.canvasReset();
    },
  },
  mounted() {
    // if (screenfull.isEnabled) {
    //   screenfull.on("change", () => {
    //     this.isFullscreen = screenfull.isFullscreen;
    //   });
    // }
    this.canvasReset();
    window.addEventListener("resize", this.canvasReset);
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
  destroyed() {
    window.removeEventListener("resize", this.canvasReset);
  },
  methods: {
    canvasReset() {
      debounce(() => {
        this.initChart();
      }, 500)();
    },
    initChart() {
      let xAxis = Object.keys(this.stock) || [];
      let data = [];
      if (xAxis.length > 0) {
        data = xAxis.map((item) => {
          return this.stock[item].total;
        });
      }
      if (this.chart) {
        this.chart.dispose();
      }
      this.chart = echarts.init(document.getElementById("factoryStoreChart"));
      const actualOptions = JSON.parse(JSON.stringify(this.options));
      actualOptions.xAxis.data = xAxis;
      actualOptions.series[0].data = data;
      this.actualOptions = actualOptions;
      this.chart.setOption(actualOptions);
    },
  },
};
</script>
<style></style>
