<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";
import dayjs from "dayjs";
export default {
  name: "yfdl-multiple-bar-chart",
  componentName: "yfdl-multiple-bar-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },

    // 传入数据
    list: {
      type: Array,
      default: () => [],
    },
    tabIndex: {
      type: Number,
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      firstAnimation: true,
    };
  },
  computed: {
    xdata() {
      return [
        "一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月",
      ];
    },
    series() {
      let result = [];
      if (this.list && this.list.length > 0) {
        this.list.forEach((listItem, index) => {
          let seriesItem = {
            name: listItem?.name,
            type: "bar",
            barMinHeight: 2,
            data: listItem?.data,
            itemStyle: {
              color: listItem.color,
            },
            barWidth: "20%",
          };
          if (this.tabIndex == -2) {
            if (index == 3) {
              seriesItem = {
                yAxisIndex: 1,
                name: listItem?.name,
                type: "line",
                smooth: true,
                data: listItem?.data,
                itemStyle: {
                  color: listItem.color,
                },
              };
            }
          }
          result.push(seriesItem);
        });
      }
      return result;
    },
  },
  methods: {
    resize() {
      this.initChart();
    },
    initChart() {
      let chart = echarts.init(this.$el);
      let option = {
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(5, 187, 76,0.35)",
          borderColor: "#05bb4c",
          formatter: function (params) {
            var htmlStr = `<div style='margin-bottom:5px'>${params[0].axisValue}</div>`;
            for (var i = 0; i < params.length; i++) {
              htmlStr += `<div style='margin-bottom:2px'>`;
              var param = params[i];
              var seriesName = param.seriesName; //图例名称
              var value = param.value; //y轴值
              var seriesType = param.seriesType; //单位判断code
              var mark = param.marker; //点
              var unit = `<span style='font-size:14px'>${
                seriesType == "line" ? "kW/m²" : "万kWh"
              }</span>`;

              htmlStr += mark; //一个点
              htmlStr += `${seriesName} : ${
                value != null ? value + unit : "--"
              }`; //圆点后面显示的文本
              htmlStr += "</div>";
            }
            return htmlStr;
          },
          padding: [10, 10, 3, 10],
          textStyle: {
            color: "#fff",
            fontSize: 16,
          },
          axisPointer: {
            type: "shadow",
            shadowStyle: {
              color: "rgba(105,105,105, .05)",
              width: "1",
            },
          },
        },
        legend: {
          show: true,
          data: this.list.map((i) => i?.name),
          right: 56,
          icon: "ract",
          itemWidth: 8,
          itemHeight: 8,
          inactiveColor: partten.getColor("gray"),
          textStyle: {
            fontSize: 12,
            color: partten.getColor("grayl"),
          },
        },
        grid: {
          top: "10%",
          left: "1%",
          right: "2%",
          bottom: "5%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.xdata,
            nameLocation: "center",
            axisPointer: {
              type: "shadow",
            },
            axisLine: {
              lineStyle: {
                color: "#93989A",
              },
            },
            axisLabel: {
              textStyle: {
                fontSize: 14,
                color: "#93989A",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "单位：万千瓦时",
            nameTextStyle: {
              color: "#93989A",
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(96,103,105,0.3)",
                type: "dashed",
              },
            },
            axisLabel: {
              textStyle: {
                fontSize: 14,
                color: "#93989A",
              },
            },
          },
        ],
        series: this.series,
      };
      chart.clear();
      if (this.tabIndex == -2) {
        option.yAxis.push({
          type: "value",
          name: "单位：kW/m²",
          nameTextStyle: {
            color: "#93989A",
          },
          splitLine: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              fontSize: 14,
              color: "#93989A",
            },
          },
        });
      }
      chart.setOption(option);

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

      window.addEventListener("resize", this.resize);
    },
  },
  created() {
    this.$nextTick(() => {
      this.id = "pie-chart-" + util.newGUID();
    });
  },
  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);
  },

  watch: {
    "$store.state.themeName"() {
      this.initChart();
    },
  },
};
</script>

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