<template>
  <el-card shadow="none" style="padding: 10px 14px;">
    <div class="trend-container">
      <div class="card-title">
        访问页面分布<el-tooltip class="box-item" effect="light" placement="right-start">
          <template #content>
            <div style="width: 300px;">
              您可以查看用户在指定时段（1天、7天、30天）内访问页面数的分布情况，并可以进行版本、渠道和分群的交叉筛选。如果您在Android应用中使用了Fragment页面统计功能，这里的页面包括您指定统计的Activity和Fragment。<br />
              <span style="color: rgba(22, 122, 240, 1);">访问页面：</span>用户一次启动内访问的页面数。若用户在一次访问中先后访问了页面A、页面B、页面A，则用户本次启动的访问页面数为3
            </div>
          </template>
          <span style=" vertical-align: middle; margin: 0 0 0 8px;">
            <Svg name="export"></Svg>
          </span>
        </el-tooltip>
      </div>
      <FormEcharts v-if="type === 'date'" @dateChange="handleDateChange" :averageValue="averageValue" />
      <div class="chart-container">
        <BarChart :data="usageTimeData" :is-multi-series="true" :series-names="seriesNames" title="多系列对比柱状图"
          style="height: 300px;" />
      </div>
      <el-divider style="margin: 30px 0; background: rgba(230, 230, 230, 1);" />
      <ExportToCSV :data="formatData" :columns="columns" :fileName="'访问页面'" :hideTable="false" />
    </div>
  </el-card>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import BarChart from '/@/views/count/components/BarChart.vue';
import ExportToCSV from '/@/views/count/components/ExportToCSV.vue';
import FormEcharts from '../components/FormEcharts.vue';
import Svg from '/@/components/Svg.vue';

const props = defineProps({
  type: {
    type: String,
    default: 'date'
  },
  form: {
    type: Object,
    default: () => ({})
  }
})

const averageValue = ref({
  time: '平均访问页面数',
  value: '1.23'
})

// 多系列数据对比
const usageTimeData = ref([
  {
    name: '1-2',
    values: []
  },
  {
    name: '3-5',
    values: []
  },
  {
    name: '6-10',
    values: []
  },
  {
    name: '11-20',
    values: []
  }
])

const seriesNames = ref<string[]>([]);

const handleDateChange = (val: string[]) => {
  console.log(val);
  // 强制触发响应式更新
  seriesNames.value = [...val];

  // 创建新的数据对象以确保响应式更新
  const newData = usageTimeData.value.map((item: any) => ({
    ...item,
    values: val.map((date: string) => {
      const value = Math.floor(Math.random() * 100);
      return {
        value: value,
        percentage: `${value}%`
      }
    })
  }));

  console.log(newData);

  usageTimeData.value = newData;
}


// 定义表格列配置
const columns = [
  { prop: 'time', label: '访问页面' },
  { prop: 'count', label: '启动次数' },
  { prop: 'percentage', label: '启动次数占比' }
];

const formatData = computed(() => {
  console.log(`formatData: `);
  console.log(usageTimeData.value);
  return usageTimeData.value.map((item: any) => {
    return {
      time: item.name,
      count: item.values[0]?.value || 0,
      percentage: `${item.values[0]?.value || 0}%`
    }
  })
})

defineExpose({
  handleDateChange
})
</script>
<style scoped lang="scss">
@import '/@/views/count/styles/common.scss';

svg {
  vertical-align: middle;
  margin: 0 0 0 12px;
}

.chart-container {
  margin: 0 auto;
  max-width: 1360px;
}
</style>