<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-line-chart",
  componentName: "multiple-line-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    // 数据
    list: {
      type: Array,
      default: () => [
        {
          title: "日发电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1",
              value: 1,
            },
            {
              text: "2",
              value: 2,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "4",
              value: 4,
            },
            {
              text: "5",
              value: 6,
            },
            {
              text: "6",
              value: 2,
            },
            {
              text: "7",
              value: 3,
            },
            {
              text: "8",
              value: 8,
            },
            {
              text: "9",
              value: 3,
            },
            {
              text: "10",
              value: 2,
            },
            {
              text: "11",
              value: 5,
            },
            {
              text: "12",
              value: 8,
            },
            {
              text: "13",
              value: 3,
            },
            {
              text: "14",
              value: 9,
            },
            {
              text: "15",
              value: 3,
            },
            {
              text: "16",
              value: 2,
            },
            {
              text: "17",
              value: 1,
            },
            {
              text: "18",
              value: 3,
            },
            {
              text: "19",
              value: 4,
            },
            {
              text: "20",
              value: 9,
            },
            {
              text: "21",
              value: 2,
            },
            {
              text: "22",
              value: 1,
            },
            {
              text: "23",
              value: 2,
            },
            {
              text: "24",
              value: 3,
            },
            {
              text: "25",
              value: 3,
            },
            {
              text: "26",
              value: 4,
            },
            {
              text: "27",
              value: 9,
            },
            {
              text: "28",
              value: 7,
            },
            {
              text: "29",
              value: 4,
            },
            {
              text: "30",
              value: 1,
            },
            {
              text: "31",
              value: 2,
            },
          ],
        },
        {
          title: "上网电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1",
              value: 1,
            },
            {
              text: "2",
              value: 2,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "4",
              value: 3,
            },
          ],
        },
        {
          title: "购网电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1",
              value: 1,
            },
          ],
        },
        {
          title: "风速",
          yAxisIndex: 1,
          value: [
            {
              text: "1",
              value: 100,
            },
            {
              text: "2",
              value: 200,
            },
            {
              text: "1",
              value: 100,
            },
            {
              text: "2",
              value: 400,
            },
          ],
        },
      ],
    },
    // 单位
    units: {
      type: Array,
      default: () => ["(MW)", "(风速)"],
    },
    showLegend: {
      type: Boolean,
      default: false,
    },
    hoverType: {
      type: String,
      default: "item",
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      // color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b", "#1a93cf", "#c531c7", "#bd3338", "#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b", "#1a93cf", "#c531c7", "#bd3338"],
      color: ["#05bb4c", "#f8de5b", "#4b55ae", "#fa8c16", "#1DA0D7", "#DD5044"],
    };
  },
  computed: {
    colorValue() {
      return partten.getColor(this.color);
    },
    datas() {
      return this.list.map((t) => {
        return t.value;
      });
    },
    legend() {
      return this.list.map((t) => {
        return t.title;
      });
    },
    xdata() {
      return this.list[0].value.map((t, tIndex) => {
        let xunit = [tIndex];
        return xunit;
      });
    },
    series() {
      let result = [];

      this.list.forEach((value, index) => {
        result.push({
          name: value.title,
          type: "line",
          smooth: true,
          showSymbol: false,
          zlevel: index,
          lineStyle: {
            normal: {
              // color: index != 0 ? this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000" : this.color[index],
              color: this.color[index],
              width: 1,
            },
            // emphasis: {
            //   color: this.color[index],
            // },
          },
          // areaStyle: {
          //   normal: {
          //     color:
          //       index == 0
          //         ? new echarts.graphic.LinearGradient(
          //             0,
          //             0,
          //             0,
          //             1,
          //             [
          //               {
          //                 offset: 0,
          //                 color: this.hexToRgba(this.color[index], 0.3),
          //               },
          //               {
          //                 offset: 1,
          //                 color: this.hexToRgba(this.color[index], 0.1),
          //               },
          //             ],
          //             false
          //           )
          //         : "transparent",
          //     shadowColor: this.hexToRgba(this.color[index], 0.1),
          //     shadowBlur: 10,
          //   },
          //   emphasis: {
          //     color: new echarts.graphic.LinearGradient(
          //       0,
          //       0,
          //       0,
          //       1,
          //       [
          //         {
          //           offset: 0,
          //           color: this.hexToRgba(this.color[index], 0.3),
          //         },
          //         {
          //           offset: 1,
          //           color: this.hexToRgba(this.color[index], 0.1),
          //         },
          //       ],
          //       false
          //     ),
          //     shadowColor: this.hexToRgba(this.color[index], 0.1),
          //     shadowBlur: 10,
          //   },
          // },
          tooltip: {
            show: true,
            position: [10, 10],
          },
          yAxisIndex: value.yAxisIndex,
          data: value.value.map((t) => {
            return t.value;
          }),
        });
      });

      return result;
    },
    yAxis() {
      let result = [];
      this.units.forEach((value, index) => {
        result.push({
          type: "value",
          name: value,
          axisLabel: {
            formatter: "{value}",
            fontSize: util.vh(14),
          },
          axisLine: {
            show: false,
          },
          //分格线
          splitLine: {
            show: index == 0,
            lineStyle: {
              color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
              type: "dashed",
            },
          },
        });
      });
      return result;
    },
  },
  methods: {
    hexToRgba(hex, opacity) {
      let rgbaColor = "";
      let reg = /^#[\da-f]{6}$/i;
      if (reg.test(hex)) {
        rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt(
          "0x" + hex.slice(3, 5)
        )},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
      }
      return rgbaColor;
    },
    resize() {},
    initChart() {
      let that = this;
      let chart = echarts.init(this.$el);

      let option = {
        color: this.color,
        tooltip: {
          trigger: this.hoverType,
          axisPointer:
            this.hoverType != "item"
              ? {
                  type: "cross",
                }
              : {},
          backgroundColor: this.$store.state.themeName === "dark" ? "rgba(0,0,0,0.4)" : "rgba(255,255,255,0.5)",
          borderColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
          textStyle: {
            fontSize: util.vh(16),
            color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
          },
        },
        legend: {
          show: this.showLegend,
          data: this.legend,
          right: 56,
          icon: "circle",
          itemWidth: 6,
          inactiveColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
          textStyle: {
            color: this.$store.state.themeName === "dark" ? partten.getColor("grayl") : "#000",
            fontSize: 12,
          },
        },
        grid: {
          top: 32,
          left: 40,
          right: 40,
          bottom: 24,
        },
        xAxis: [
          {
            name: "(m/s)",
            type: "category",
            boundaryGap: false,
            axisLabel: {
              formatter: "{value}",
              fontSize: util.vh(14),
              textStyle: {
                color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
              },
            },
            axisLine: {
              show: false,
            },
            data: this.xdata,
          },
        ],
        yAxis: this.yAxis,
        series: this.series,
      };
      chart.clear();
      chart && option && chart.setOption(option);
      this.resize = function () {
        chart.resize();
      };
      window.addEventListener("resize", this.resize);
      chart.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();
    });
  },
  updated() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  unmounted() {
    window.removeEventListener("resize", this.resize);
  },
};
</script>

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