<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: "windChartCom",
  componentName: "windChartCom",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "450px",
    },
    interval: {
      type: Number,
      default: 7,
    },
    // 单位
    units: {
      type: Array,
      default: () => ["", ""],
    },
    // 单位数据
    unitDatas: {
      type: Array,
      default: () => [
        { flag: false, min: 0, max: 0 },
        { flag: false, min: 0, max: 0 },
      ],
    },
    // 传入数据
    list: {
      type: Array,
      default: () => [
        {
          title: "绿线",
          smooth: true,
          value: [
            {
              text: "",
              value: 0,
            },
            {
              text: "0:00",
              value: 20,
            },
            {
              text: "10:00",
              value: 1,
            },
            {
              text: "11:00",
              value: 40,
            },
            {
              text: "12:00",
              value: 10,
            },
            {
              text: "13:00",
              value: 15,
            },
            {
              text: "14:00",
              value: 30,
            },
            {
              text: "15:00",
              value: 40,
            },
            {
              text: "",
              value: 10,
            },
          ],
        },
        {
          title: "黄线",
          smooth: true,
          value: [
            {
              text: "",
              value: 0,
            },
            {
              text: "0:00",
              value: 40,
            },
            {
              text: "10:00",
              value: 20,
            },
            {
              text: "11:00",
              value: 20,
            },
            {
              text: "12:00",
              value: 10,
            },
            {
              text: "13:00",
              value: 40,
            },
            {
              text: "14:00",
              value: 50,
            },
            {
              text: "15:00",
              value: 40,
            },
            {
              text: "",
              value: 10,
            },
          ],
        },
      ],
    },
    showLegend: {
      type: Boolean,
      default: false,
    },
    animate: {
      type: Boolean,
      default: true,
    },
    showXdata: {
      type: Boolean,
      default: false,
    },
    ratio: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: [
        "#1985de",
        "#ff8300",
        "#06a746",
        "#3eede7",
        "#eb0f7c",
        "#dfdc0a",
        "#de0feb",
        "#cca4e3",
      ],
      newlist: null,
      emptyData: [],
      leftOffset: 20,
      seletedData: {},
    };
  },
  watch: {
    list: {
      handler(newValue, oldValue) {
        this.newlist = newValue;
        this.$nextTick(() => {
          this.newlist = this.list;
          this.initChart();
        });
      },
      deep: true,
    },
    "$store.state.themeName"() {
      this.initChart();
    },
  },
  computed: {
    getTimeStanp() {
      // 当日0点时间
      var timeStamp = [];
      let stamp = new Date(new Date().setHours(0, 0, 0, 0)).getTime();
      for (let i = 0; i < 96; i++) {
        timeStamp.push(dayjs(stamp).format("HH:mm"));
        this.emptyData.push("0");
        stamp = parseInt(stamp) + 15 * 60 * 1000;
      }
      timeStamp.push("24:00");
      return timeStamp;
    },
    colorValue() {
      return partten.getColor(this.color);
    },
    datas() {
      return this.newlist.map((t) => {
        return t.title;
      });
    },
    legend() {
      if (this.newlist) {
        return this.newlist.map((t) => {
          return t.title;
        });
      } else {
        return [];
      }
    },
    xdata() {
      var data;
      this.newlist.forEach((value, index) => {
        if (value.value != null && value.value.length > 0) {
          data = value;
        }
      });
      return data?.value.map((t, index) => {
        // if (index === this.newlist[0]?.value.length - 1) {
        //   return "24:00";
        // } else {
        return dayjs(t.dateTime).format("MM-DD HH:mm");
        // }
      });
    },
    series() {
      let result = [];
      this.list.forEach((value, index) => {
        result.push({
          name: value.name || value.title,
          type: "line",
          smooth: value.smooth,
          symbol: "circle",
          showSymbol: true,
          zlevel: index,
          lineStyle: {
            normal: {
              color: this.color[index],
              width: 1,
            },
          },
          yAxisIndex: value.yAxisIndex,
          data: value.value.map((t) => {
            if (value.noRatio) {
              return t.value ? t.value.toFixed(2) : 0.0;
            } else {
              return t.value === "" && t.value !== 0
                ? ""
                : (t.value / this.ratio).toFixed(2);
            }
          }),
        });
      });
      return result;
    },
    yAxis() {
      let result = [];
      let i = 0;
      this.units.forEach((value, index) => {
        var option = {
          type: "value",
          // name: this.units[index],
          nameTextStyle: {
            color: this.color[index],
            fontSize: 12,
          },
          splitNumber: 11,
          // position:'left',
          axisLabel: {
            formatter: "{value}",
            textStyle: {
              color: this.color[index],
              fontSize: 12,
            },
          },
          axisLine: {
            lineStyle: {
              color: this.color[this.unitDatas[index].codex],
              width: 1,
            },
          },
          boundaryGap: false,
          //分格线
          splitLine: {
            show: false,
            lineStyle: {
              color: "#2D3338",
              width: 1, //轴线的宽度
              type: "dashed",
            },
          },
        };
        if (this.unitDatas[index].flag) {
          i++;
          option.min = this.unitDatas[index].min;
          option.max = this.unitDatas[index].max;
          if (i % 2 != 0) {
            option.position = "left";
            option.offset = (i - 1) * 35;
            if (i >= 3) {
              option.offset = (i - 2) * 35;
            }
            if (i >= 5) {
              option.offset = (i - 3) * 35;
            }
            if (i >= 7) {
              option.offset = (i - 4) * 35;
            }
          } else {
            if (i >= 4) {
              option.offset = (i - 3) * 35;
            }
            if (i >= 6) {
              option.offset = (i - 4) * 35;
            }
            if (i >= 8) {
              option.offset = (i - 5) * 35;
            }
          }

          result.push(option);
        } else if (index == 0) {
          if (!this.unitDatas[0].flag) {
            option.axisLine.lineStyle.color = "#525252";
            option.axisLabel.textStyle.color = "#828484";
          }
          if (
            this.unitDatas[index].min == 0 &&
            this.unitDatas[index].max == 0
          ) {
            result.push(option);
          } else {
            option.min = this.unitDatas[index].min;
            option.max = this.unitDatas[index].max;
            result.push(option);
          }
        }
        this.leftOffset = 35;
        if (i == 0) {
          this.leftOffset = 35;
        }
      });
      return result;
    },
  },
  methods: {
    childMethod(index) {
      this.chart.dispatchAction({
        type: "highlight",
        seriesIndex: 0,
        dataIndex: 0,
      });
      return this.chart.getModel().getComponent("yAxis", index).axis.scale
        ._extent[1];
    },
    changDateVisible(option) {
      this.seletedData = option;
      this.initChart();
    },
    resize() {},
    initChart() {
      const chart = echarts.init(this.$el, null, {
        renderer: "canvas",
        useDirtyRect: false,
      });
      let option = {
        animation: this.animate,
        color: this.color,
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,0.4)",
          borderColor: partten.getColor("gray"),
          textStyle: {
            color: "#fff",
            fontSize: 14,
          },
        },
        legend:
          {
            show: this.showLegend,
            data: this.legend,
            padding: [10, 10],
            left: "center",
            bottom: -10,
            icon: "ract",
            itemWidth: 6,
            itemHeight: 6,
            inactiveColor: partten.getColor("gray"),
            textStyle: {
              color: partten.getColor("grayl"),
              fontSize: 12,
            },
            selected: this.seletedData,
          } || {},
        grid: {
          top: 16,
          left: this.leftOffset,
          right: 40,
          bottom: 30,
          containLabel: true,
        },
        xAxis:
          [
            {
              type: "category",
              boundaryGap: false,
              axisLabel: {
                interval: this.interval,
                formatter: "{value}",
                textStyle: {
                  color: "#828484",
                  fontSize: 12,
                },
              },
              axisTick: {
                alignWithLabel: true,
              },
              data: this.showXdata ? this.getTimeStanp : this.xdata,
            },
          ] || [],
        yAxis: this.yAxis || [],
        dataZoom: [
          {
            type: "slider",
            bottom: 0,
            height: 20,
            handleSize: "90%",
            start: 0,
            end: 100,
            handleColor: "#555B66",
            borderColor: "#545454",
            backgroundColor: "rgba(0, 70, 199, 0.2)",
            fillerColor: "#343A46",
            textStyle: { color: "white" },
            labelFormatter: (value) => {
              if (value) {
                return this.xdata[value];
              }
            },
          },
        ],
        series: this.series || {},
      };

      chart.clear();
      if (Object.values(this.series).length) {
        chart.setOption(option);
      }

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

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