<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { basicProps } from './props';
  // import { dateUtil } from '/@/utils/dateUtil';
</script>
<script lang="ts" setup>
  import { ref, Ref, watch } from 'vue';
  // import type { dataItemType } from './props';
  import { useECharts } from '/@/hooks/web/useECharts';
  const props = defineProps({
    ...basicProps,
  });

  const viewStaticsData = ref<number[]>([]);
  const shareStaticsData = ref<number[]>([]);
  const yixStringData = ref<string[]>([]);
  const maxSize = ref(0);
  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  function handlesetOptions() {
    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      legend: {
        orient: 'horizontal',
        bottom: 0,
      },
      grid: { left: '2%', right: '2%', top: '2%', bottom: '10%', containLabel: true },
      xAxis: {
        type: 'category',
        // data: [...new Array(30)].map((_item, index) => `${index + 1}日`),
        data: yixStringData.value,
      },
      yAxis: {
        type: 'value',
        max: maxSize.value,
        splitNumber: 4,
      },
      series: [
        {
          data: viewStaticsData.value,
          type: 'bar',
          itemStyle: { color: '#38a0ff' },
          barMaxWidth: 80,
          name: '用户浏览量',
        },
        {
          data: shareStaticsData.value,
          type: 'bar',
          itemStyle: { color: '#4cca73' },
          barMaxWidth: 80,
          name: '用户分享数',
        },
      ],
    });
  }
  // props.viewStatics,
  watch(
    () => [props.viewStatics, props.shareStatics],
    ([data1, data2]) => {
      console.log('viewStatics-data', data1);
      viewStaticsData.value = data1.reduce<number[]>(
        (prev: number[], current) => prev.concat(Number(current.amount)),
        [],
      );

      yixStringData.value = data1.reduce<string[]>(
        (prev: string[], current) => prev.concat(current.date),
        [],
      );
      shareStaticsData.value = data2.reduce<number[]>(
        (prev: number[], current) => prev.concat(Number(current.amount)),
        [],
      );

      const maxNumber = Math.max(...viewStaticsData.value.concat(shareStaticsData.value));
      const pow = Math.pow(10, maxNumber.toString().length - 1);
      maxSize.value = maxNumber > 10 ? Math.floor(maxNumber / 10) * 10 + pow * 2 : 10;
      console.log('maxSize', maxSize.value);
      handlesetOptions();
    },
    {
      immediate: true,
      deep: true,
    },
  );
</script>
