<template>
  <Card :title="title" :loading="loading">
    <template #extra>
      <condition :selectTimeType="title=='管理中心用户活跃度统计'?1:0"  :type="title=='管理中心用户活跃度统计'?'0':'2'"  :typeShow="title=='管理中心用户活跃度统计'" @change="Search"  @expor="handleExport" />
    </template>
      <div ref="chartRef" :style="{ height, width }"></div>
  </Card>
</template>
<script lang="ts">
  import { basicProps } from '../components/props';
  import { Card, Select } from 'ant-design-vue';
  import type { SelectProps } from 'ant-design-vue';
</script>
<script lang="ts" setup>
  import { ref, Ref, watch, onMounted, reactive } from 'vue';
  import { volumeTrend, qjuserTrend } from '/@/api/statistics/index';
  import condition from '../components/condition.vue';
  import dayjs from 'dayjs';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { exportElsxFile, } from '/@/utils/file/download';
  const props = defineProps({
  loading: Boolean,
    ...basicProps,
  });
  const value = ref(1);
  const SearchData = reactive({
    startTime: dayjs().subtract(props.title == '云容量趋势分析'?6:1,'month').format('YYYY-MM-DD'),
    endTime: dayjs().format('YYYY-MM-DD'),
    timeType: props.title == '云容量趋势分析'?'month':'day',
    infoType: props.title == '云容量趋势分析'?'add':'all',
  });
  const options = ref<SelectProps['options']>([
        {
          value: 1,
          label: '日',
        },
        {
          value: 2,
          label: '周',
        },
        {
          value: 3,
          label: '月',
        },
      ]);
  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>);
  onMounted(() => {
    getAddUser();
  });
  function handleChange(val){
    console.log('handleChange',val)
    SearchData.value = val
    getAddUser()
  }
  function Search(val) {
    console.log('Search',val)
    let timeStr = {
      0:'day',
      1:'week',
      2:'month',
    }
    const { startTime, endTime, dataType, type } = val;
    SearchData.timeType = timeStr[dataType] || 'month'
    SearchData.startTime = startTime;
    SearchData.endTime = endTime;
    SearchData.infoType = props.title == '管理中心用户活跃度统计'?'all':type == '0'?'add':'all';
    getAddUser();
  }
  function handleExport(){
  console.log('props',props.title)
  let fields  = {
    'time':'日期',
    'num':'数量',
  }
    let data = yixStringData.value.map((ele,index) => {
      return {
        'time':ele,
        'num':viewStaticsData.value && viewStaticsData.value[index] || 0,
      }
    })
    exportElsxFile(data, fields,  props.title)
}
  async function getAddUser() {
    let xdata = [], yData=[]
    let apiSrc = props.title=='云容量趋势分析'?volumeTrend:qjuserTrend
    let res = await apiSrc(SearchData);
    const data = res.reverse()
    data.map(ele => {
      xdata.push(ele.groupKey)
      yData.push(ele.count)
    })
    yixStringData.value = xdata
    viewStaticsData.value = yData
    handlesetOptions()
  }
  function handlesetOptions() {
    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      legend: {
        orient: 'horizontal',
        bottom: 0,
      },
      xAxis: {
        type: 'category',
        data: yixStringData.value,
      },
      yAxis: {
        name:props.title=='云容量趋势分析'?'容量/G':'',
        type: 'value',
        splitNumber: 4,
      },
      series: [
        {
          data: viewStaticsData.value,
          type: 'line',
          itemStyle: { color: '#38a0ff' },
          name: props.title=='云容量趋势分析'?'容量数':'用户活跃度',
        },
      ],
    });
  }
  watch(
    () => props.loading,
    () => {
      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;
      handlesetOptions();
    },
    {
      immediate: true,
      deep: true,
    },
  );
</script>
