<template>
  <div class="p-4">
    <GrowCard :loading="loading" :list="growCardList" class="enter-y" />
    <sceneEchart
      title="场景趋势分析"
      class="!my-4 enter-y"
      @change="Search"
      :echartData="echartData"
    />
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue';
import { sceneTotal, sceneTrend } from '/@/api/statistics/index';
import { GrowCardItem } from '../data';
import GrowCard from '../components/GrowCard.vue';
import sceneEchart from '../components/sceneEchart.vue';
const loading = ref(true);
const growCardList = ref<GrowCardItem[]>([]);
const echartData = reactive({
  xdata: [],
  kjList:[],
  kkList:[],
  ssList:[],
  ssobjList:[],
});
const SearchData = reactive({
  startTime: '',
  endTime: '',
  dataType: 0,
  type: 2,
});
onMounted(() => {
  getData();
  getList();
});
async function getList() {
  let downlist = [],
    xdata = [];
  const {kjList,kkList,ssList,ssobjList} = await sceneTrend(SearchData);
  kjList.map((ele) => {
    xdata.push(ele.groupKey);
    downlist.push(ele.count);
  });
  echartData.xdata = xdata;
  echartData.kjList = downlist
  echartData.kkList = kkList.map(ele => ele.count)
  echartData.ssList = ssList &&ssList.map(ele => ele.count)|| []
  echartData.ssobjList = ssobjList.map(ele => ele.count)|| []
}
function Search(val) {
  const { startTime, endTime, dataType, type } = val;
  console.log('params', val);
  SearchData.startTime = startTime;
  SearchData.endTime = endTime;
  SearchData.dataType = type;
  SearchData.type = dataType;
  getList();
}
async function getData() {
  try {
    loading.value = true;
    const { totalSceneCount = 0, preMonthAddCount = 0, todayAddCount = 0 } = await sceneTotal();
    let list: GrowCardItem[] = [
      {
        title: '累计场景数量',
        icon: 'fxemoji:notchedrightsemi3dot',
        value: totalSceneCount,
        unit: '个',
        color: 'blue',
        action: '年',
      },
      {
        title: '上月新增场景数量',
        icon: 'download-count|svg',
        value: preMonthAddCount,
        unit: '个',
        color: 'orange',
        action: '月',
      },
      {
        title: '今日新增场景数量',
        icon: 'transaction|svg',
        value: todayAddCount,
        unit: '个',
        color: 'blue',
        action: '日',
      },
    ];
    loading.value = false;
    growCardList.value = list;
  } catch (error) {
    loading.value = false;
  }
}
</script>

