<template>
  <a-spin :spinning="spinning" style="width: 100%; height: 100%; position: fixed" />
  <div v-if="!spinning" ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { defineComponent, PropType, ref, Ref, reactive, watchEffect, watch } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { toEchartsData } from '/@/utils/ventutil';
  import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  import EchartsUtil from '/@/utils/echartsUtil';

  export default defineComponent({
    name: 'BarAndLine',
    props: {
      chartsColumns: {
        type: Array,
        default: () => [],
      },
      chartsColumnsType: {
        type: String,
        required: true,
      },
      chartsType: {
        type: String,
        default: 'listMonitor',
      },
      dataSource: {
        type: Array,
        default: () => [],
      },
      option: {
        type: Object,
        default: () => ({}),
      },
      xAxisPropType: {
        type: String,
        required: true,
      },
      width: {
        type: String as PropType<string>,
        default: '100%',
      },
      height: {
        type: String as PropType<string>,
        default: 'calc(100vh - 78px)',
      },
    },
    setup(props) {
      const spinning = ref<boolean>(true);
      const chartRef = ref<HTMLDivElement | null>(null);
      const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
      const chartData = getTableHeaderColumns(props.chartsColumnsType) || [];
      let chartsColumns = props.chartsColumns.length > 0 ? props.chartsColumns : chartData;

      const option = reactive({
        name: '',
        color: ['#7B68EE', '#0000CD', '#6495ED', '#00BFFF', '#AFEEEE', '#008080', '#00FA9A', '#2E8B57', '#FAFAD2', '#DAA520'],
        tooltip: null,
        grid: null,
        toolbox: {
          feature: {
            saveAsImage: {
              iconStyle: {
                normal: {
                  borderColor: '#ffffff',
                },
              },
              show: true,
            },
          },
        },
        dataZoom: null,
        legend: null,
        timeline: null,
        xAxis: null,
        yAxis: null,
        series: null,
      });
      let optionUtil;

      watchEffect(() => {
        props.dataSource && props.dataSource.length > 0 && option.series && initCharts();
      });

      watch([() => props.chartsType, () => props.chartsColumns], ([newChartsType, newChartsColumns]) => {
        chartsColumns = newChartsColumns;
        optionUtil.initChartOption(props.chartsType, chartsColumns);
      });

      function initChartsOption() {
        optionUtil = new EchartsUtil(option);
        optionUtil.initChartOption(props.chartsType, chartsColumns);
      }
      initChartsOption();

      function initCharts() {
        //轴数据
        if (option.series && option.series.length === chartsColumns.length) {
          let xAxisData = props.dataSource.map((item) => item[props.xAxisPropType]);
          [...chartsColumns].forEach((propType: any, index) => {
            option.series[index].data = props.dataSource.map((item) => item[propType.dataIndex] || 0);
          });
          option.xAxis[0].data = xAxisData;
          setOptions(option, false);
        }
      }
      setTimeout(() => {
        spinning.value = false;
      }, 1000);
      return { chartRef, spinning };
    },
  });
</script>
<style>
  .spin-state {
    margin-top: 16px;
  }
</style>
