<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-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: () => [
        {
          title: "机舱震动x方向",
          yAxisIndex: 0,
          value: [
            {
              text: "-0.003",
              value: -0.003,
            },
            {
              text: "-0.002",
              value: -0.002,
            },
            {
              text: "-0.003",
              value: -0.006,
            },
            {
              text: "-0.003",
              value: -0.003,
            },
            {
              text: "-0.002",
              value: -0.002,
            },
            {
              text: "-0.003",
              value: -0.006,
            },
            {
              text: "-0.003",
              value: -0.003,
            },
            {
              text: "-0.002",
              value: -0.002,
            },
            {
              text: "-0.003",
              value: -0.006,
            },
          ],
        },
        {
          title: "机舱震动y方向",
          yAxisIndex: 1,
          value: [
            {
              text: "-0.01",
              value: -0.01,
            },
            {
              text: "-0.005",
              value: -0.005,
            },
            {
              text: "-0.008",
              value: -0.008,
            },
            {
              text: "-0.01",
              value: -0.01,
            },
            {
              text: "-0.005",
              value: -0.005,
            },
            {
              text: "-0.008",
              value: -0.008,
            },
            {
              text: "-0.01",
              value: -0.01,
            },
            {
              text: "-0.005",
              value: -0.005,
            },
            {
              text: "-0.008",
              value: -0.008,
            },
          ],
        },
        {
          title: "机舱震动最大偏移值",
          yAxisIndex: 2,
          value: [
            {
              text: "1",
              value: 1,
            },
            {
              text: "0.05",
              value: 0.05,
            },
            {
              text: "0.5",
              value: 0.5,
            },
            {
              text: "1",
              value: 1,
            },
            {
              text: "0.05",
              value: 0.05,
            },
            {
              text: "0.5",
              value: 0.5,
            },
            {
              text: "1",
              value: 1,
            },
            {
              text: "0.05",
              value: 0.05,
            },
            {
              text: "0.5",
              value: 0.5,
            },
          ],
        },
        {
          title: "风速1",
          yAxisIndex: 3,
          value: [
            {
              text: "1",
              value: 1,
            },
            {
              text: "5",
              value: 5,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "1",
              value: 1,
            },
            {
              text: "5",
              value: 5,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "1",
              value: 1,
            },
            {
              text: "5",
              value: 5,
            },
            {
              text: "3",
              value: 1,
            },
          ],
        },
        {
          title: "风速2",
          yAxisIndex: 4,
          value: [
            {
              text: "1",
              value: 1,
            },
            {
              text: "2",
              value: 2,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "1",
              value: 1,
            },
            {
              text: "2",
              value: 2,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "1",
              value: 1,
            },
            {
              text: "2",
              value: 2,
            },
            {
              text: "3",
              value: 1,
            },
          ],
        },
      ],
    },
    showLegend: {
      type: Boolean,
      default: false,
    },
    default: {
      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",
        },
      ],
    },

    markLineData: {
      type: Array,
      defaylt: () => [],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: ["#1DA0D7", "#05BB4C", "#323E6F", "#EDB32F", "#DB5520"],
    };
  },
  computed: {
    legend() {
      return this.list.map((t) => {
        return t.title;
      });
    },
    xdata() {
      if (this.list.length > 1) {
        // this.list[1].value.forEach((lEle, lIndex) => {
        //   // console.log(
        //     this.list[1].value.indexOf(
        //       this.list[1].value[lIndex].text == "00:00"
        //     )
        //   );
        // });
        return this.list[1]?.value.map((t) => {
          return t.text || new Date(t.ts).formatDate("hh:mm:ss");
        });
      } else if (this.list.length == 1) {
        return this.list[0]?.value.map((t) => {
          return t.text || new Date(t.ts).formatDate("hh:mm:ss");
        });
      }
    },
    yAxis() {
      let result = [];
      let p = { left: 0, right: 0 };
      this.yAxises.forEach((item, index) => {
        result.push({
          type: "value",
          name: `${item.name}${item.unit}`,
          nameLocation: p[item.position] % 2 == 0 ? "end" : "start",
          nameGap: 10,
          min: item.min,
          max: item.max,
          position: item.position,
          offset: p[item.position] * 50,
          axisLabel: {
            formatter: "{value}",
            fontSize: 12,
          },
          //分格线
          splitLine: {
            lineStyle: {
              color:
                this.$store.state.themeName === "dark"
                  ? partten.getColor("gray")
                  : partten.getColor("black"),
              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.title,
          type: "line",
          smooth: true,
          zlevel: index,
          lineStyle: {
            normal: {
              color: this.color[index],
              width: 1,
            },
          },
          showSymbol: false,
          yAxisIndex: value.yAxisIndex,
          markLine: this.markLineData
            ? {
                name: "",
                silent: true,
                label: {
                  position: "end",
                  formatter: "",
                  color: "red",
                  fontSize: 14,
                },
                data: this.markLineData,
                lineStyle: {
                  normal: {
                    type: "dashed",
                    color: "red",
                    width: 2,
                  },
                },
              }
            : {},
          data: value.value.map((t) => {
            if (t.value || t.value === 0) {
              return t.value;
            } else if (t.doubleValue || t.doubleValue === 0) {
              return t.doubleValue;
            } else {
              return t.longValue;
            }
          }),
        });
      });

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

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

      chart.setOption(option);
      if (this.default) {
        chart.dispatchAction({
          type: "showTip",
          seriesIndex: 0, // 显示第几个series
          dataIndex: this.list[0].value.length - 600, // 显示第几个数据
        });
      }

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

      window.addEventListener("resize", this.resize);
    },
    option: function () {
      let themeName = "";
      this.$store.state.themeName === "dark"
        ? (themeName = true)
        : (themeName = false);
      return {
        color: this.color,
        tooltip: {
          trigger: "axis",
          triggerOn: this.default ? "click" : "mousemove",
          alwaysShowContent: true,
          backgroundColor: themeName
            ? "rgba(0,0,0,0.4)"
            : "rgba(255,255,255,0.5)",
          borderColor: themeName ? partten.getColor("gray") : "#000",
          textStyle: {
            color: themeName ? "#fff" : "#000",
            fontSize: 12,
          },
        },
        legend: {
          show: this.showLegend,
          data: this.legend,
          top: "top",
          icon: "circle",
          itemWidth: 6,
          inactiveColor: themeName ? partten.getColor("gray") : "#000",
          textStyle: {
            color: themeName ? partten.getColor("grayl") : "#000",
            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: themeName ? partten.getColor("gray") : "#000",
              },
            },
            data: this.xdata,
          },
        ],
        yAxis: {
          type: "value",
          axisLabel: {
            formatter: "{value}",
            fontSize: 14,
          },
          axisLine: {
            show: false,
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#606769",
              type: "dashed",
            },
          },
        },
        // 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();
    });
  },
  beforeUpdate() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  unmounted() {
    window.removeEventListener("resize", this.resize);
  },
};
</script>

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