<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 { left } from "@antv/x6/lib/registry/port-layout/line";

export default {
  name: "dsah-pie",
  componentName: "dsah-pie",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "18.519vh",
    },
    paddingWidth: {
      type: String,
      default: "30%",
    },
    // 内部饼图数据
    innerData: {
      type: Array,
      default: () => [
        {
          value: 700,
          unit: "个",
          name: "行业大类1",
        },
        {
          value: 679,
          unit: "个",
          name: "行业大类2",
        },
        {
          value: 1548,
          unit: "个",
          name: "行业大类3",
        },
      ],
    },
    // 外部饼图数据
    outerData: {
      type: Array,
      default: () => [],
    },
    // 颜色
    color: {
      type: String,
      default: "green",
    },
    showLegend: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {},
  methods: {
    initChart() {
      let that = this;
      let legend1 = this.innerData.map((v) => v.name);
      let legend2 = this.outerData.map((v) => v.name);
      let legendData = [...legend1, ...legend2];

      let option = {
        color: [
          "#e17e23",
          "#ba3237",
          "#c531c7",
          "#4b55ae",
          "#ccf0d3",
          "#05bb4c",
        ],
        tooltip: {
          trigger: "item",
          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: "#fff",
            fontSize: util.vh(16),
          },
        },
        grid: {
          top: 32,
          left: 60,
          right: 40,
          bottom: 24,
        },
        // legend: {
        //   show: this.showLegend,
        //   orient: "vertical", //纵向图例
        //   right: "16",
        //   bottom: 32,
        //   itemWidth: 15,
        //   itemHeight: 15,
        //   formatter: (name) => {
        //     if (that.innerData.length) {
        //       const item = that.innerData.filter((item) => item.name === name)[0];
        //       return `{name|${name}：}{value| ${item.value}}`;
        //     }
        //   },
        //   //icon: 'circle',
        //   itemGap: 12, //图例item间距
        //   textStyle: {
        //     color: this.$store.state.themeName === "dark"
        //       ? partten.getColor("gray")
        //       : "#000",
        //     fontSize: util.vh(14),
        //     rich: {
        //       name: {
        //         color: this.$store.state.themeName === "dark"
        //       ? partten.getColor("gray")
        //       : "#000",
        //         fontSize: 12,
        //       },
        //       value: {
        //         color: this.$store.state.themeName === "dark"
        //         ? partten.getColor("grayl")
        //         : "#000",
        //         fontSize: 12,
        //       },
        //     },
        //   },
        //   data: legend1,
        // },
        series: [
          {
            name: "",
            type: "pie",
            center: ["50%", "50%"],
            radius: [0, "35%"],
            itemStyle: {
              normal: {},
            },
            label: {
              normal: {
                show: false,
              },
            },
            labelLine: {
              normal: {
                show: false,
              },
            },
            data: this.innerData,
          },
          {
            name: "",
            type: "pie",
            center: ["50%", "50%"],
            radius: ["55%", "95%"],
            data: this.outerData,
            labelLine: {
              normal: {
                // length: 40,
                // length2: 120,
                lineStyle: {
                  color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
                  fontSize: util.vh(14),
                },
              },
            },
            itemStyle: {
              normal: {
                borderWidth:util.vh(4),
                borderColor: "#071812",
              },
            },
            label: {
              normal: {
                formatter: (params) => {
                  return "{percent|" + params.percent.toFixed(2) + "%}";
                },
                padding: [0, 0, 0, 0],
                rich: {
                  color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
                  percent: {
                    fontSize: util.vh(14),
                    color: this.$store.state.themeName === "dark" ? "#FFFFFF" : "#000000" ,
                  },
                },
              },
            },
          },
        ],
      };

      this.chart.setOption(option);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    this.$el.style.width = this.width;
    this.$el.style.height = this.height;
    this.chart = echarts.init(this.$el);
    this.initChart();
  },
  updated() {
    this.initChart();
  },
  watch: {
    "$store.state.themeName"() {
      this.initChart();
    },
  },
};
</script>

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