<template>
  <v-chart :theme="themeColor" :option="option.value" autoresize> </v-chart>
</template>

<script setup>
import { reactive, watch, ref, computed } from 'vue';
import VChart from 'vue-echarts';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { PieChart } from 'echarts/charts';
import { mergeTheme } from '../../../chart';
import { useChartDataFetch } from '../../../hooks/useChartDataFetch.hook';
import { useChartEditStore } from '@/store/modules/chart/chartEditStore';
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent, TitleComponent } from 'echarts/components';

const config = ref(null);

import('./config.js').then(module => {
  config.value = module;
});

// 计算属性
const { includes } = computed(() => config.value || { includes: [] });

const props = defineProps({
  themeSetting: {
    type: Object,
    required: true
  },
  themeColor: {
    type: Object,
    required: true
  },
  chartConfig: {
    type: Object,
    required: true
  }
});

use([DatasetComponent, CanvasRenderer, PieChart, GridComponent, TooltipComponent, LegendComponent, TitleComponent]);

const option = reactive({
  value: {}
});

const dataHandle = newData => {
  const d = parseFloat(`${newData}`) * 100;
  let config = props.chartConfig.option;
  config.title.text = d.toFixed(2) + '%';
  config.series[0].data[0].value[0] = d;
  config.series[0].data[1].value[0] = 100 - d;
  option.value = mergeTheme(props.chartConfig.option, props.themeSetting, includes);
  option.value = props.chartConfig.option;
};

// 配置时
watch(
  () => props.chartConfig.option.dataset,
  newData => {
    try {
      dataHandle(newData);
    } catch (error) {
      console.log(error);
    }
  },
  {
    immediate: true,
    deep: false
  }
);

// 预览时
useChartDataFetch(props.chartConfig, useChartEditStore, resData => {
  let d = parseFloat(`${resData}`) * 100;
  // @ts-ignore
  option.value.title.text = d.toFixed(2) + '%';
  // @ts-ignore
  option.value.series[0].data[0].value[0] = d;
  // @ts-ignore
  option.value.series[0].data[1].value[0] = 100 - d;
});
</script>
