<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts" setup>
  import { ref, Ref, withDefaults, onMounted, watch } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { getTrendData } from '/@/api/dashboard/index';
  interface Props {
    width?: string;
    height?: string;
    tenantTrendList?: Array<[number, string]>;
  }
  const props = withDefaults(defineProps<Props>(), {
    width: '100%',
    height: '320px',
    tenantTrendList: () => [],
  });

  watch(
    () => props.tenantTrendList,
    (newValue) => {
      setOptions({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              width: 1,
              color: '#019680',
            },
          },
        },
        xAxis: {
          type: 'time',
          splitLine: {
            show: true,
            lineStyle: {
              width: 1,
              type: 'solid',
              color: 'rgba(226,226,226,0.5)',
            },
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          splitArea: {
            show: true,
            areaStyle: {
              color: ['rgba(255,255,255,0.2)', 'rgba(226,226,226,0.2)'],
            },
          },
        },
        grid: {
          left: '1%',
          right: '2%',
          bottom: 0,
          containLabel: true,
        },
        series: [
          {
            smooth: true,
            name: '当前趋势',
            type: 'line',
            data: newValue,
            areaStyle: {},
            itemStyle: {
              color: '#5ab1ef',
            },
          },
        ],
        animationDuration: 1500,
      });
    }
  );

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  onMounted(async () => {
    const endTs = Date.now();
    const res = await getTrendData({
      startTs: endTs - 2592000000,
      endTs,
      interval: 86400000,
      trend: 'TENANT_TREND',
    });
    const transferResult = res.map((item) => [item.ts, item.value]);
    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
        formatter(params) {
          var relVal = params[0].name;
          for (var i = 0, l = params.length; i < l; i++) {
              console.log('tooltip数据值',params[i].value)
              //遍历出来的值一般是字符串，需要转换成数字，再进项tiFixed四舍五入
              relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + Number(params[i].value)
          }
          return relVal;
        },
      },
      xAxis: {
        type: 'time',
        splitLine: {
          show: true,
          lineStyle: {
            width: 1,
            type: 'solid',
            color: 'rgba(226,226,226,0.5)',
          },
        },
        axisTick: {
          show: false,
        },
      },
      yAxis: {
        splitArea: {
          show: true,
          areaStyle: {
            color: ['rgba(255,255,255,0.2)', 'rgba(226,226,226,0.2)'],
          },
        },
      },
      grid: {
        left: '1%',
        right: '2%',
        bottom: 0,
        containLabel: true,
      },
      series: [
        {
          smooth: true,
          name: '当前趋势',
          type: 'line',
          data: transferResult,
          areaStyle: {},
          itemStyle: {
            color: '#5ab1ef',
          },
        },
      ],
      animationDuration: 1500,
    });
  });
</script>
