<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';

  export default defineComponent({
    name: 'LineMulti',
    props: {
      /**
       * 图表数据，配合xAxisPropType、propTypeArr使用
       */
      chartData: {
        type: Array,
        default: () => [],
        required: true,
      },
      option: {
        type: Object,
        default: () => ({}),
      },
      /**
       * x轴数据对应的prop，将从chartData中映射prop到一个Set中。
       *
       * @example
       * ```js
       * const props = { xAxisPropType: 'name', chartData: [{ name: 'a' }, { name: 'b' }] }
       *
       * // truns out
       * option.xAxis.data = ['a', 'b'];
       * ```
       */
      xAxisPropType: {
        type: String,
        required: true,
      },
      /**
       * 提供一个Map，该Map将用于生成基于chartData的echart.series数据。
       *
       * @example
       * ```js
       * const props = { propTypeArr: new Map([['valueA', '值A']]), chartData: [{ valueA: 'a' }, { valueA: 'b' }] }
       *
       * // truns out
       * option.series = [{ name: '值A', data: ['a', 'b'] }]
       * ```
       */
      propTypeArr: {
        type: Map,
        default: () => new Map(),
        required: true,
      },
      type: {
        type: String as PropType<string>,
        default: 'line',
      },
      width: {
        type: String as PropType<string>,
        default: '100%',
      },
      height: {
        type: String as PropType<string>,
        default: 'calc(100vh - 78px)',
      },
    },
    emits: ['click'],
    setup(props, { emit }) {
      const chartRef = ref<HTMLDivElement | null>(null);
      const { setOptions, getInstance } = useECharts(chartRef as Ref<HTMLDivElement>);
      const option = reactive({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
            label: {
              show: true,
              backgroundColor: '#333',
            },
          },
        },
        legend: {
          top: 10,
        },
        grid: {
          top: 60,
        },
        xAxis: {
          type: 'category',
          data: [],
        },
        yAxis: {
          type: 'value',
        },
        series: [],
      });

      watchEffect(() => {
        props.chartData && initCharts();
      });

      function initCharts() {
        if (props.option) {
          Object.assign(option, props.option);
        }

        //图例类型
        // let typeArr = Array.from(new Set(props.chartData.map((item) => item.type)));
        //轴数据
        let xAxisData = Array.from(new Set(props.chartData.map((item) => item[props.xAxisPropType])));
        let seriesData = [];
        [...props.propTypeArr.keys()].forEach((filed) => {
          let obj = { name: props.propTypeArr.get(filed), type: props.type };
          // let chartArr = props.chartData.filter((item) => filed === item.type);
          //data数据
          obj['data'] = props.chartData.map((item) => item[filed]);
          seriesData.push(obj);
        });
        option.series = seriesData;
        option.xAxis.data = xAxisData;
        setOptions(option, false);
        getInstance()?.off('click', onClick);
        getInstance()?.on('click', onClick);
      }

      function onClick(params) {
        emit('click', params);
      }

      return { chartRef };
    },
  });
</script>
