<template>
  <div class="chart" :id="id"></div>
</template>

<script>
import util from "@/helper/util.js";
import partten from "@/helper/partten.js";
import * as echarts from "echarts";

export default {
  name: "multiple-bar-chart",
  componentName: "multiple-bar-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    // 传入数据
    barData: {
      type: Array,
      default: () => [
        {
          title: "日发电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 2,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 3,
            },
            {
              text: "5日",
              value: 3,
            },
            {
              text: "6日",
              value: 3,
            },
            {
              text: "7日",
              value: 3,
            },
          ],
        },
        {
          title: "上网电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 2,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 3,
            },
            {
              text: "5日",
              value: 3,
            },
            {
              text: "6日",
              value: 3,
            },
            {
              text: "7日",
              value: 3,
            },
          ],
        },
        {
          title: "购网电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 2,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 3,
            },
            {
              text: "5日",
              value: 3,
            },
            {
              text: "6日",
              value: 3,
            },
            {
              text: "7日",
              value: 3,
            },
          ],
        },
        {
          title: "风速",
          yAxisIndex: 1,
          value: [
            {
              text: "1日",
              value: 1,
            },
            {
              text: "2日",
              value: 2,
            },
            {
              text: "3日",
              value: 1,
            },
            {
              text: "4日",
              value: 3,
            },
            {
              text: "5日",
              value: 3,
            },
            {
              text: "6日",
              value: 3,
            },
            {
              text: "7日",
              value: 3,
            },
          ],
        },
      ],
    },
    lineData: {
      type: Object,
      default: () => {
        return {
          name: "风速",
          unit: "km",
          data: [200, 800, 400, 500, 800, 700, 800, 900, 200],
        };
      },
    },
    // 单位
    units: {
      type: Array,
      default: () => ["(万KWh)", "(风速)"],
    },
    // 显示 legend
    showLegend: {
      type: Boolean,
      default: true,
    },
    // 颜色
    color: {
      type: Array,
      default: () => [
        "#05bb4c",
        "#4b55ae",
        "#fa8c16",
        "#f8de5b",
        "#1a93cf",
        "#c531c7",
        "#bd3338",
      ],
    },
    showAnimation: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      firstAnimation: true,
      newbarData: null,
    };
  },
  watch: {
    barData: {
      handler(newValue, oldValue) {
        console.warn(newValue);
        this.newbarData = newValue;
        this.initChart();
      },
      deep: true,
    },
    lineData : {
      handler(newValue, oldValue) {
        console.warn(newValue);
        this.newlineData = newValue;
        this.initChart();
      },
      deep: true,
    },
  },
  computed: {
    legend() {
      return this.newbarData.map((t) => {
        return t.title;
      });
    },
    xdata() {
      let result = [];
      if (
        this.newbarData &&
        this.newbarData.length > 0 &&
        this.newbarData[0].value.length > 0
      ) {
        result = this.newbarData[0].value.map((t) => {
          return t.text;
        });
      }
      return result;
    },
    ydata() {
      let result = [];
      this.units.forEach((value, index) => {
        let data = null;
        if (index == 0) {
          data = {
            type: "value",
            name: value,
            axisLabel: {
              formatter: "{value} ",
              fontSize: 12,
            },
            //分格线
            splitLine: {
              lineStyle: {
                color: "#5a6162",
                type: "dashed",
              },
            },
          };
        } else {
          data = {
            type: "value",
            name: value,
            axisLabel: {
              formatter: "{value}",
              fontSize: 12,
            },
            //分格线
            splitLine: {
              show: false,
            },
          };
        }

        result.push(data);
      });

      return result;
    },
    series() {
      let result = [];
      if (this.newbarData && this.newbarData.length > 0) {
        this.newbarData.forEach((value, index) => {
          result.push({
            name: value.title,
            type: "bar",
            barWidth: "10%",
            animation: this.firstAnimation && this.showAnimation,
            yAxisIndex: value.yAxisIndex,
            data: value.value.map((t) => {
              return t.value;
            }),
          });
        });
      }
      return result;
    },
  },
  methods: {
    resize() {},
    initChart() {
      let chart = echarts.init(this.$el);

      let option = {
        color: this.color,
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,0.4)",
          borderColor: partten.getColor("gray"),
          textStyle: {
            color: "#fff",
            fontSize: 12,
          },
        },
        legend: {
          show: this.showLegend,
          data: this.legend,
          right: 56,
          icon: "ract",
          itemWidth: 8,
          itemHeight: 8,
          inactiveColor: partten.getColor("gray"),
          textStyle: {
            color: partten.getColor("grayl"),
            fontSize: 12,
          },
        },
        grid: {
          top: 32,
          left: 40,
          right: this.ydata.length > 1 ? 40 : 14,
          bottom: 24,
        },
        xAxis: [
          {
            type: "category",
            data: this.xdata,
            axisPointer: {
              type: "shadow",
            },
            axisLabel: {
              fontSize: 12,
            },
          },
        ],
        yAxis: this.ydata,
        series: this.series,
      };

      // line data
      if (this.newlineData && this.newlineData.data.length > 0) {
        option.yAxis.push({
          type: "value",
          name: this.newlineData.name,
          axisLabel: {
            formatter: "{value} ",
            color: partten.getColor("gray"),
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: false,
            lineStyle: {
              type: "dashed",
              dashOffset: 10,
              color: partten.getColor("gray") + 80,
            },
          },
        });

        option.series.push({
          name: this.newlineData.name,
          type: "line",
          data: this.newlineData.data,
          smooth: true, //平滑展示
          yAxisIndex: option.yAxis.length - 1,
          lineStyle: {
            color: partten.getColor("yellow"),
          },
          itemStyle: {
            color: partten.getColor("yellow"),
          },
        });
      }

      chart.clear();
      chart.setOption(option);

      this.resize = function () {
        chart.resize();
      };

      window.addEventListener("resize", this.resize);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
    this.newbarData = this.barData;
  },
  mounted() {
    this.$nextTick(() => {
      this.$el.style.width = this.width;
      this.$el.style.height = this.height;
      this.initChart();
      this.firstAnimation = false;
    });
  },
  updated() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  unmounted() {
    window.removeEventListener("resize", this.resize);
  },
};
</script>

<style lang="less">
.chart {
  width: 100%;
  height: 100%;
  display: inline-block;
}
</style>
