<template>
  <div :id="baseConfig.ids" :style="{ width: width, height: height }"></div>
</template>
<script>
import resize from "./resize";
export default {
  props: {
    data: {
      default: () => ({ name: [], data: [] }),
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    baseConfig: {
      default: function () {
        return {
          ids: "EchartsBar",
          title: "",
          unit: "人",
        };
      },
    },
  },
  mixins: [resize],
  data() {
    return {
      chart: null,
      colorConfig: [
        { code: "100001", name: "人员定位", color: "#00E1FF" }, // 人员
        { code: "100002", name: "周界入侵", color: "#00B387" }, //周界
        { code: "100003", name: "火灾监测", color: "#00FFCC" }, // 火灾
        { code: "100004", name: "激光云台", color: "#00B0B0" }, // 泄漏
        { code: "100001", name: "人员报警", color: "#FFD552" },
        { code: "100002", name: "周界入侵", color: "#02F2F9" },
        { code: "100003", name: "火灾监测", color: "#007DFF" },
      ],
      colors: [
        "#FF83DF",
        "#8023FF",
        "#0064FF",
        "#00ACFF",
        "#50E3C2",
        "#39CD1A",
        "#B8E986",
        "#F8E71C",
        "#FF9F00",
        "#EC576A",
      ],
    };
  },
  mounted() {
    this.drawChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  watch: {
    data: {
      //深度监听，可监听到对象、数组的变化
      handler() {
        this.$nextTick(() => {
          this.drawChart();
        });
      },
      deep: true,
    },
  },
  methods: {
    drawChart() {
      this.chart = this.$echarts.init(document.getElementById(this.baseConfig.ids));
      let option = {
        title: {
          show: false,
        },
        grid: {
          left: 0,
          right: 0,
          top: 0,
          bottom: 50,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          // formatter: "{b}: {c}" + this.baseConfig.unit,
        },
        color: this.colors,
        legend: {
          left: "center",
          right: 0,
          bottom: 0,
          width: "70%",
          textStyle: {
            color: "#fff",
          },
          // itemWidth: 20,
        },
        yAxis: [
          {
            type: "category",
            data: this.data.name,
            axisLabel: {
              textStyle: {
                color: "#fff",
              },
              margin: 10,
              // interval: 0,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#04bbff",
              },
            },
          },
        ],
        xAxis: {
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              color: "#fff",
            },
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "#04bbff",
            },
          },
          splitLine: {
            show: false,
          },
        },
        series: this.data.data.map((item, index) => {
          return {
            name: item.name,
            type: "bar",
            stack: "workPlan",
            // barWidth: 24,
            barMaxWidth: 16,
            itemStyle: {
              opacity: 0.8,
            },
            emphasis: {
              itemStyle: {
                opacity: 1,
              },
            },
            data: item.data,
          };
        }),
      };
      this.chart.setOption(option);
    },
  },
};
</script>
