<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: () => {},
    },
    isDouble: { type: Boolean, default: false },
    type: { type: String, default: "" },
  },
  data() {
    return {
      id: "",
      chart: null,
      chartName: [
        { name: "实际功率", code: "sjgl", color: "#4B55AE" },
        { name: "理论功率", code: "llgl", color: "#1C99FF" },
        { name: "预测功率", code: "ycgl", color: "#F8DE5B" },
        { name: "实际风速", name1: "实际光照", code: "pjfs", color: "#05BB4C" },
        { name: "预测风速", name1: "预测光照", 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) {
              if (ele.code.includes("fs")) {
                series.push({
                  name: this.type == -1 ? ele.name : ele.name1,
                  type: "line",
                  symbol: "circle",
                  showAllSymbol: true,
                  yAxisIndex: 1,
                  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],
                });
              } else {
                series.push({
                  name: this.type == -1 ? ele.name : ele.name1,
                  type: "line",
                  symbol: "circle",
                  showAllSymbol: true,
                  yAxisIndex: 0,
                  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],
                });
              }
            }
          });
        }
      }
      return series;
    },
    // lengend() {
    //     let arr=[]
    //   this.chartData.lengend.forEach((i) => {
    //     if (this.chartName.some((item) => item.code == i)) {
    //       i = item.name;
    //     }
    //   });
    // },
  },
  methods: {
    initChart() {
      let chart = echarts.init(this.$el);

      //图表配置
      var option = {
        color: this.color,

        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: this.chartName.map((item) => {
            return {
              name: item.name,
              itemStyle: {
                color: item.color,
              },
            };
          }),
          itemHeight: 10,
          textStyle: {
            fontSize: 12,
            color: "#93989A",
          },
          top: "5%",
          right: "15%",
        },
        grid: {
          top: "22%",
          left: "4%",
          right: "4%",
          bottom: "12%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: true,
          data: this.chartData?.xAxis || [],
          axisLabel: {
            textStyle: {
              color: "#93989A",
            },
            // formatter: function (params) {
            //   return params.split(" ")[0] + "\n" + params.split(" ")[1];
            // },
          },
          splitLine: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "#93989A",
            },
          },
        },
        yAxis: [
          {
            name: "MW",
            type: "value",
            nameTextStyle: {
              color: "#93989A",
            },
            axisLabel: {
              formatter: "{value}",
              textStyle: {
                color: "#93989A",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(96,103,105,0.3)",
                type: "dashed",
              },
            },
          },
          {
            name: this.type == -1 ? "m/s" : "W/m²",
            nameTextStyle: {
              color: "#93989A",
            },
            type: "value",
            axisLabel: {
              show: true,
              formatter: "{value}",
              textStyle: {
                color: "#93989A",
              },
            },
            splitLine: {
              show: false,
            },
          },
        ],
        series: this.series || [],
      };
      if (this.isDouble) {
        option.title = {
          text: this.chartData?.name,
          top: "5%",
          left: "35%",
          textStyle: {
            fontSize: 25,
            color: "rgba(255,255,255,0.8)",
            fontFamily: 'Arial, "SourceHanSans", "Microsoft YaHei", sans-serif',
          },
        };
      }
      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>
