<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 { right } from "@antv/x6/lib/registry/port-layout/main";

export default {
  name: "multiple-line-chart",
  componentName: "multiple-line-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    chartData: {
      type: Array,
      default: () => {},
    },
    // 单位
    units: {
      type: Array,
      default: () => ["MW", "m/s"],
    },
    // 颜色
    color: {
      type: Array,
      default: () => [
        "#323E6F",
        "#1DA0D7",
        "#02BB4C",
        "#DB5520",
        "#EDB32F",
        "#EDEB2F",
      ],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      chartName: [
        { name: "实际功率", code: "sjgl", color: "#4B55AE" },
        { name: "理论功率", code: "llgl", color: "#1C99FF" },
        { name: "预测功率", code: "ycgl", color: "#F8DE5B" },
        { name: "实际风速", code: "pjfs", color: "#05BB4C" },
        { name: "预测风速", code: "ycfs", color: "#FF8300" },
      ],
    };
  },
  computed: {
    series() {
      let series = [];
      if (this.chartData && this.chartData.xAxis.length) {
        for (let key in this.chartData) {
          this.chartName.forEach((ele) => {
            if (ele.code == key) {
              series.push({
                name: ele.name,
                type: "line",
                showAllSymbol: true,
                symbolSize: 0,
                smooth: true,
                lineStyle: {
                  normal: {
                    width: 1,
                    color: ele.color,
                  },
                  //   borderColor: "rgba(0,0,0,.4)",
                },
                itemStyle: {
                  color: ele.color,
                },
                tooltip: {
                  show: true,
                },

                data: this.chartData[key],
              });
            }
          });
        }
      }
      console.log(series);
      return series;
    },
    // lengend() {
    //     let arr=[]
    //   this.chartData.lengend.forEach((i) => {
    //     if (this.chartName.some((item) => item.code == i)) {
    //       i = item.name;
    //     }
    //   });
    // },
  },
  methods: {
    initChart() {
      //   console.log(this.chartData);
      console.log(this.series);
      let chart = echarts.init(this.$el);
      //图表所需数据区
      var charts = {
        names: ["点击量", "访客"],
        lineX: [
          "2018-11-11 17:01",
          "2018-11-11 17:02",
          "2018-11-11 17:03",
          "2018-11-11 17:04",
          "2018-11-11 17:05",
          "2018-11-11 17:06",
          "2018-11-11 17:07",
          "2018-11-11 17:08",
          "2018-11-11 17:09",
          "2018-11-11 17:10",
          "2018-11-11 17:11",
          "2018-11-11 17:12",
          "2018-11-11 17:13",
          "2018-11-11 17:14",
          "2018-11-11 17:15",
          "2018-11-11 17:16",
          "2018-11-11 17:17",
          "2018-11-11 17:18",
          "2018-11-11 17:19",
          "2018-11-11 17:20",
        ],
        useNum: [
          4.51, 3.52, 3.03, 5.34, 0.95, 2.36, 2.17, 3.28, 1.59, 1.51, 2.31,
          1.92, 4.53, 5.24, 1.65, 2.36, 5.27, 3.28, 1.29, 5.3,
        ],
        visitorNum: [
          360, 545, 80, 192, 330, 580, 192, 80, 250, 453, 352, 28, 625, 345, 65,
          325, 468, 108, 253, 98,
        ],
      };
      var color = ["rgba(23, 255, 243", "rgba(255,100,97"];

      //y轴样式  arrIndex相关目标数组的index值，lineYData目标y轴值
      function getLineYSeries(arrIndex, lineYData) {
        var data = {
          name: charts.names[arrIndex],
          type: "line",
          yAxisIndex: arrIndex, //使用相对的y轴
          color: color[arrIndex] + ")",
          smooth: true,
          symbol: "circle",
          symbolSize: 5,
          data: lineYData,
        };

        return data;
      }

      var lineY = [getLineYSeries(0), getLineYSeries(1, charts.visitorNum)];

      //图表配置
      var option = {
        color: this.color,
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: this.chartName.map((item) => item.name),
            textStyle: {
              fontSize: 12,
              color: "#93989A",
            },
          top: "5%",
          right: "15%",
        },
        grid: {
          top: "22%",
          left: "4%",
          right: "4%",
          bottom: "12%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.chartData?.xAxis || [],
          axisLabel: {
            textStyle: {
              color: "rgb(255,255,255)",
            },
            // formatter: function (params) {
            //   return params.split(" ")[0] + "\n" + params.split(" ")[1];
            // },
          },
          splitLine: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "rgb(255,255,255)",
            },
          },
        },
        yAxis: [
          {
            name: "MW",
            type: "value",
            axisLabel: {
              formatter: "{value}",
              textStyle: {
                color: "rgb(255,255,255)",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgb(255,255,255,0.4)",
                type: "dashed",
              },
            },
            axisLine: {
              lineStyle: {
                color: "rgb(255,255,255)",
              },
            },
          },
          {
            name: "m/s",
            type: "value",
            axisLabel: {
              show: true,
              formatter: "{value}",
              textStyle: {
                color: "rgb(255,255,255)",
              },
            },
            splitLine: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "rgb(255,255,255)",
              },
            },
          },
        ],
        series: this.series || [],
      };
      chart.setOption(option);
    },
  },
  emits: {
    areaClick: null,
  },
  created() {
    this.id = "line-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();
    });
  },
  watch: {
    "$store.state.themeName"() {
      this.initChart();
    },
  },
};
</script>

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