<!-- 客户消息量或告警统计 -->
<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';

  interface Props {
    width?: string;
    height?: string;
    customerList?: Array<[number, string]>;
    type: 'CUSTOMER_ALARM_STATISTICAL' | 'CUSTOMER_MESSAGE_STATISTICAL';
  }
  const props = withDefaults(defineProps<Props>(), {
    width: '100%',
    height: '320px',
    customerList: () => [],
    type: '',
  });
  watch(
    () => props.customerList,
    (newValue) => {
      const transferResult = newValue.map((item) => [item.ts, item.value]);

      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: transferResult,
            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: props.type,
    });

    const transferResult = res.map((item) => [item.ts, item.value]);
    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: transferResult,
          areaStyle: {},
          itemStyle: {
            color: '#5ab1ef',
          },
        },
      ],
      animationDuration: 1500,
    });
  });
</script>
