<template>
  <div v-show="dataSeries.length" ref="chartRef" :style="{ height, width }"></div>
  <div class="empty-box" v-show="!dataSeries.length"
    ><Empty :image="Empty.PRESENTED_IMAGE_SIMPLE"
  /></div>
</template>
<script lang="ts">
  import { defineComponent, PropType, ref, Ref, onMounted, toRefs } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { Empty } from 'ant-design-vue';
  import { seriesDataT } from './props';

  export default defineComponent({
    components: { Empty },
    props: {
      width: {
        type: String as PropType<string>,
        default: '100%',
      },
      height: {
        type: String as PropType<string>,
        default: '200px',
      },
      legendData: {
        type: Array,
        default: () => [],
      },
      seriesData: {
        type: Array,
        default: () => [],
      },
      isCircle: {
        type: Boolean,
        default: true,
      },
    },
    setup(props) {
      const { legendData, seriesData } = toRefs(props);
      const dataSeries: Ref<seriesDataT[]> = ref([]);
      const legendDatas: Ref<seriesDataT[]> = ref([]);
      dataSeries.value = seriesData.value as unknown as seriesDataT[];
      legendDatas.value = legendData.value as unknown as seriesDataT[];

      const chartRef = ref<HTMLDivElement | null>(null);
      const { setOptions, resize } = useECharts(chartRef as Ref<HTMLDivElement>);
      const labelLine = {
        normal: {
          show: true,
          length2: 1,
        },
      } as any;
      onMounted(() => {
        setOptions({
          backgroundColor: '#ffffff',
          tooltip: {
            trigger: 'item',
            formatter: '{b} {d}%',
          },
          series: [
            {
              avoidLabelOverlap: false,
              type: 'pie',
              radius: props.isCircle ? '60%' : ['40%', '60%'],
              data: dataSeries.value,
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)',
                },
              },
              labelLine,
            },
          ],
        });
        //自适应
        window.addEventListener('resize', () => resize());
      });
      return { chartRef, Empty, dataSeries };
    },
  });
</script>

<style>
  .empty-box {
    display: flex;
    align-items: center;
    margin: 0 120px;
  }
</style>
