<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: "double-line-chart",
  componentName: "double-line-chart",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "13.889vh",
    },
    // 传入数据
    lossPower: {
      type: Array,
      default: () => [],
    },
    // 单位
    unit: {
      type: String,
      default: "",
    },
    showLable: {
      type: Boolean,
      default: true,
    },
    showLegend: {
      type: Boolean,
      default: false,
    },
    // 颜色
    color: {
      type: Array,
      default: () => [
        "#005bd9",
        "#019f2e",
        "#db6200",
        "#a10f0f",
        "#850894",
        "#9fa0a2",
      ],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
      // color: ["#05bb4c", "#f8de5b", "#4b55ae", "#fa8c16", "#1DA0D7", "#DD5044"],
      newlist: null,
    };
  },
  watch: {
    list: {
      handler(newValue, oldValue) {
        this.newlist = newValue;
        this.$nextTick(() => {
          this.newlist = this.list;
          this.initChart();
        });
      },
      deep: true,
    },
    "$store.state.themeName"() {
      this.initChart();
    },
  },
  computed: {
    colorValue() {
      return partten.getColor(this.color);
    },
    datas() {
      return this.newlist.map((t) => {
        return t.value;
      });
    },
    legend() {
      if (this.newlist) {
        return this.newlist.map((t) => {
          return t.title;
        });
      } else {
        return [];
      }
    },
    xdata() {
      return [];
      // return this.newlist[0]?.value.map((t) => {
      //     return t.text;
      // });
    },
    series() {
      let result = [];
      this.list.forEach((value, index) => {
        result.push({
          name: value.name || value.title,
          type: "line",
          // smooth: value.smooth,
          showSymbol: false,
          zlevel: index,
          lineStyle: {
            normal: {
              color: this.color[index],
              width: 1,
            },
          },
          yAxisIndex: value.yAxisIndex || 0,
          data: value.value.map((t) => {
            return t.value;
          }),
        });
      });
      return result;
    },
    yAxis() {
      let result = [];
      result.push({
        type: "value",
        name: this.unit,
        axisLabel: {
          formatter: "{value}",
          fontSize: util.vh(14),
        },
        boundaryGap: false,
        //分格线
        splitLine: {
          show: false,
        },
      });
      return result;
    },
  },
  methods: {
    resize() {},
    initChart() {
      let that = this;
      const chart = echarts.init(this.$el);
      let option = {
        color: this.color,
        legend: {
          type: "scroll",
          orient: "vertical",
          right: '8%',
          top: "middle",
          itemWidth: 8,
          itemHeight: 8,
          textStyle: {
            color: partten.getColor("grayl"),
            fontSize: 12,
          },
          formatter(params) {
            return (
              params +
              "       " +
              that.lossPower.find((val) => val.name === params)?.value
            );
          },
        },
        radar: [
          {
            indicator: [{ text: "" }],
            center: ["35%", "50%"],
            radius: [0, 79],
            startAngle: 60,
            splitNumber: 5,
            shape: "circle",
            name: {
              formatter: "",
              textStyle: {
                color: "#0000FF",
              },
            },
            splitArea: {
              areaStyle: {
                color: "rgba(0, 0, 0, 0)",
              },
            },
            axisLine: {
              lineStyle: {
                color: "rgba(0, 0, 0, 0)",
              },
            },
            splitLine: {
              lineStyle: {
                color: "#0a389c",
                shadowColor: "#0a389c",
                shadowBlur: 10,
              },
            },
          },
        ],
        tooltip: {
          trigger: "item",
        },
        toolbox: {
          show: true,
        },
        series: [
          {
            name: "",
            type: "pie",
            radius: [0, 80],
            center: ["35%", "50%"],
            roseType: "area",
            label: {
              normal: {
                color: "#fff",
                formatter: "{d}%",
              },
            },
            labelLine: {
              length: 0,
              length2: 10,
              maxSurfaceAngle: 80,
            },
            data: this.lossPower,
          },
        ],
      };

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

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

      window.addEventListener("resize", this.resize);
    },
  },
  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>
