<template>
  <div class="chart" :id="id"></div>
</template>

<script>
import util from "/@/tools/util.js";
import partten from "/@/tools/partten.js";
import * as echarts from "echarts";
import dayjs from "dayjs";
export default {
  name: "multiple-y-line-chart-normal",
  componentName: "multiple-y-line-chart-normal",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    // 数据
    list: {
      type: Array,
      default: () => [],
    },
    showLegend: {
      type: Boolean,
      default: false,
    },
    // 轴
    yAxises: {
      type: Array,
      default: () => [
        {
          name: "机舱震动x方向",
          min: -0.01,
          max: 0,
          unit: "",
          position: "left",
        },
        {
          name: "机舱震动y方向",
          min: -0.01,
          max: 0,
          unit: "",
          position: "right",
        },
        {
          name: "机舱震动最大偏移值",
          min: 0,
          max: 1,
          unit: "",
          position: "left",
        },
        {
          name: "风速1",
          min: 0,
          max: 10,
          unit: "",
          position: "right",
        },
        {
          name: "风速2",
          min: 0,
          max: 10,
          unit: "",
          position: "left",
        },
      ],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: ["#323E6F", "#1DA0D7", "#05BB4C", "#EDB32F", "#DB5520"],
    };
  },
  computed: {
    legend() {
      return this.list.map((t) => {
        return t.name;
      });
    },
    xdata() {
       return this.list[1]?.data.map((t) => {
          return dayjs(t.ts).format("HH:mm:ss");
        });
    },
    yAxis() {
      let result = [];
      let p = { left: 0, right: 0 };
      this.yAxises.forEach((item, index) => {
        result.push({
          type: "value",
          name: item.name,
          nameLocation: p[item.position] % 2 == 0 ? "end" : "start",
          nameGap: 10,
          min: item.min,
          max: item.max,
          position: index % 2 == 0 ? "left" : "right",
          offset: p[item.position] * 50,
          axisLabel: {
            formatter: "{value}",
            fontSize: 12,
          },
          //分格线
          splitLine: {
            lineStyle: {
              color: partten.getColor("gray"),
              type: "dashed",
            },
          },
          axisLine: {
            show: true,
          },
          axisTick: {
            show: true,
          },
        });
        p[item.position]++;
      });
      return result;
    },
    series() {
      let result = [];
      this.list.forEach((value, index) => {
        result.push({
          name: value.name,
          type: "line",
          smooth: true,
          zlevel: index,
          lineStyle: {
            normal: {
              color: this.color[index],
              width: 1,
            },
          },
          showSymbol: false,
          yAxisIndex: value.yAxisIndex,
          data: value.data.map((t) => {
            return t.doubleValue.toFixed(2)
          }),
        });
      });

      return result;
    },
  },
  methods: {
    resize() {},
    initChart() {
      const chart = echarts.init(this.$el);

      let option = this.option();
      chart.clear();

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

      window.addEventListener("resize", this.resize);
    },
    option: function () {
      return {
        color: this.color,
        tooltip: {
          trigger: "axis",
          backgroundColor: "rgba(0,0,0,0.4)",
          borderColor: partten.getColor("gray"),
          textStyle: {
            color: "#fff",
            fontSize: 12,
          },
        },
        legend: {
          show: this.showLegend,
          data: this.legend,
          top: "top",
          icon: "circle",
          itemWidth: 6,
          inactiveColor: partten.getColor("gray"),
          textStyle: {
            color: partten.getColor("grayl"),
            fontSize: 12,
          },
        },
        grid: {
          top: 27,
          left: 16,
          right: 16,
          bottom: 0,
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            axisLabel: {
              interval:
                Number((this.xdata?.length / 8).toFixed(0)) > 2
                  ? Number((this.xdata.length / 8).toFixed(0))
                  : 0,
              showMinLabel: true,
              showMaxLabel: true,
              formatter: "{value}",
              fontSize: 12,
              textStyle: {
                color: partten.getColor("gray"),
              },
            },
            data: this.xdata,
          },
        ],
        yAxis: this.yAxis,
        series: this.series,
      };
    },
    reload: function () {
      const chart = echarts.getInstanceByDom(this.$el);
      chart.clear();
      let option = this.option();
      chart.setOption(option);
    },
  },
  created() {
    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" scoped>
.chart {
  width: 100%;
  height: 100%;
  display: inline-block;
}
</style>