<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: "normal-line-chart",
  componentName: "normal-line-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    // 数据
    list: {
      type: Array,
      default: () => [
        {
          title: "日发电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1",
              value: 1,
            },
            {
              text: "2",
              value: 2,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "4",
              value: 3,
            },
            {
              text: "5",
              value: 3,
            },
            {
              text: "6",
              value: 3,
            },
            {
              text: "7",
              value: 3,
            },
            {
              text: "8",
              value: 3,
            },
            {
              text: "9",
              value: 3,
            },
            {
              text: "10",
              value: 3,
            },
            {
              text: "11",
              value: 3,
            },
            {
              text: "12",
              value: 3,
            },
            {
              text: "13",
              value: 3,
            },
            {
              text: "14",
              value: 3,
            },
            {
              text: "15",
              value: 3,
            },
            {
              text: "16",
              value: 3,
            },
          ],
        },
        {
          title: "上网电量",
          yAxisIndex: 0,
          value: [
            {
              text: "1",
              value: 1,
            },
            {
              text: "2",
              value: 2,
            },
            {
              text: "3",
              value: 1,
            },
            {
              text: "4",
              value: 3,
            },
          ],
        },
      ],
    },
    // 单位
    units: {
      type: Array,
      default: () => ["(MW)"],
    },
    showLegend: {
      type: Boolean,
      default: false,
    },
    smooth: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
    };
  },
  computed: {
    legend() {
      return this.list.map((t) => {
        return t.title;
      });
    },
    xdata() {
      return this.list[0].value.map((t) => {
        return t.text;
      });
    },
    yAxis() {
      let result = [];
      this.units.forEach((value, index) => {
        result.push({
          type: "value",
          name: value,
          axisLabel: {
            formatter: "{value}",
            fontSize: util.vh(14),
          },
          //分格线
          splitLine: {
            lineStyle: {
              color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000" + 55,
              type: "dashed",
            },
          },
        });
      });

      return result;
    },
    series() {
      let result = [];

      this.list.forEach((value, index) => {
        result.push({
          name: value.title,
          type: "line",
          smooth: this.smooth,
          zlevel: index,
          lineStyle: {
            normal: {
              color: this.color[index],
              width: 1,
            },
          },
          yAxisIndex: value.yAxisIndex,
          data: value.value.map((t) => {
            return t.value;
          }),
        });
      });

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

      let option = {
        color: this.color,
        tooltip: {
          trigger: "axis",
          backgroundColor: this.$store.state.themeName === "dark" ? "rgba(0,0,0,0.4)" : "rgba(255,255,255,0.5)",
          borderColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
          textStyle: {
            color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
            fontSize: util.vh(16),
          },
        },
        legend: {
          show: this.showLegend,
          data: this.legend,
          right: 56,
          icon: "circle",
          itemWidth: 6,
          inactiveColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
          textStyle: {
            color: this.$store.state.themeName === "dark" ? partten.getColor("grayl") : "#000",
            fontSize: 12,
          },
        },
        grid: {
          top: 32,
          left: 40,
          right: 40,
          bottom: 80,
        },
        dataZoom: [
          {
            type: "inside",
            start: 0,
            end: 100,
          },
          {
            start: 0,
            end: 100,
            height: 40,
            bottom: 8,
            backgroundColor: "transparent",
            // handleIcon: "path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
            handleStyle: {
              color: partten.getColor("yellow"),
            },
            moveHandleSize: 0,
            dataBackground: {
              lineStyle: {
                color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
              },
              areaStyle: {
                color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
              },
            },
            selectedDataBackground: {
              lineStyle: {
                color: partten.getColor("yellow"),
              },
              areaStyle: {
                color: partten.getColor("yellow"),
              },
            },
            fillerColor: "transparent",
            textStyle: {
              color: this.$store.state.themeName === "dark" ? partten.getColor("grayl") : "#000",
            },
            borderColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
            brushSelect: false,
          },
        ],
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            axisLabel: {
              formatter: "{value}",
              fontSize: util.vh(14),
              textStyle: {
                color: this.$store.state.themeName === "dark" ? partten.getColor("grayl") : "#000",
              },
            },
            data: this.xdata,
          },
        ],
        yAxis: this.yAxis,
        series: this.series,
      };

      chart.clear();
      chart.setOption(option);

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

      window.addEventListener("resize", this.resize);
    },
  },
  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);
  },
  watch: {
      "$store.state.themeName"() {
        this.initChart();
      },
    },
};
</script>

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