<template>
  <a-card
    :bordered="false"
    title="销售额趋势图"
    :body-style="{ padding: '16px 6px 0 0' }"
  >
    <v-chart
      ref="visitHourChartRef"
      :option="visitHourChartOption"
      style="height: 362px"
    />
  </a-card>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { use } from 'echarts/core';
  import type { EChartsCoreOption } from 'echarts/core';
  import { CanvasRenderer } from 'echarts/renderers';
  import { LineChart } from 'echarts/charts';
  import {
    GridComponent,
    TooltipComponent,
    LegendComponent
  } from 'echarts/components';
  import VChart from 'vue-echarts';
  import { getSaleroomList, getVisitHourList } from '@/api/dashboard/analysis';
  import useEcharts from '@/utils/use-echarts';
  import dayjs from 'dayjs';

  use([
    CanvasRenderer,
    LineChart,
    GridComponent,
    TooltipComponent,
    LegendComponent
  ]);

  //
  const visitHourChartRef = ref<InstanceType<typeof VChart> | null>(null);

  useEcharts([visitHourChartRef]);

  // 最近 1 小时访问情况折线图配置
  const visitHourChartOption: EChartsCoreOption = reactive({});

  /* 获取最近 1 小时访问情况数据 */
  const getVisitHourData = () => {
    getSaleroomList()
      .then((data) => {
        Object.assign(visitHourChartOption, {
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['销售额', '订单量'],
            right: 20
          },
          xAxis: [
            {
              type: 'category',
              boundaryGap: false,
              data: data.salesAmountStatisticsList.map((d) =>
                dayjs(d.startStatisticsDate).format('YYYY-MM-DD')
              )
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '销售额',
              type: 'line',
              smooth: true,
              symbol: 'none',
              areaStyle: {
                opacity: 0.5
              },
              data: data.salesAmountStatisticsList.map((d) => d.dailySale)
            },
            {
              name: '订单量',
              type: 'line',
              smooth: true,
              symbol: 'none',
              areaStyle: {
                opacity: 0.5
              },
              data: data.orderStatisticsList.map((d) => d.dailyNewOrders)
            }
          ]
        });
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  getVisitHourData();
</script>
