<template>
  <!-- 数据统计 -->
  <SimplePageContentLoader :loader="statsLoader">
    <view class="d-flex flex-col mt-2">
      <template v-for="value in statsLoader.content.value" :key="value.type">
        <StatsText
          :title="value.title" 
          :data="value.datas" 
          :type="value.type" 
        />
        <HorizontalScrollText v-if="value.showTotalText == '1'" :text="statsText1" color="text.second" :fontSize="25" innerClass="mt-2" />
        <HorizontalScrollText v-else-if="value.showTotalText == '2'" :text="statsText2" color="text.second" :fontSize="25" innerClass="mt-2" />
        <view class="border-top-light-primary pt-2 mt-3"></view>
      </template>
    </view>
  </SimplePageContentLoader>
</template>

<script setup lang="ts">
import { useSimpleDataLoader } from '@/common/composeabe/SimpleDataLoader';
import { navTo } from '@/components/utils/PageAction';
import { ref, watch } from 'vue';
import { navCommonList } from '../article/common/CommonContent';
import type { StatsTextItem } from '../parts/StatsText.vue';
import type { IHomeCommonCategoryBlockStatsProps } from '../article/data/CommonCategoryDefine';
import SimplePageContentLoader from '@/common/components/SimplePageContentLoader.vue';
import StatsText from '../parts/StatsText.vue';
import HorizontalScrollText from '@/components/typography/HorizontalScrollText.vue';
import IndexContent from '@/api/introduction/IndexContent';

const props = defineProps<IHomeCommonCategoryBlockStatsProps>()

const statsText1 = ref('');
const statsText2 = ref('');
const statsLoader = useSimpleDataLoader(async () => {
  const data = (await IndexContent.getStats());

  let sumInheritor = 0;
  let sumProject = 0;
  const topLevelProject = data.ichData.find((p: any) => p.level_text == '人类非遗')?.total || 0;
  const secondLevelProject = data.ichData.find((p: any) => p.level_text == '国家级')?.total || 0;
  const thirdLevelProject = data.ichData.find((p: any) => p.level_text == '省级')?.total || 0;
  const forthLevelProject = data.ichData.find((p: any) => p.level_text == '市级')?.total || 0;

  const topLevelInheritor = data.inheritorData.find((p: any) => p.level_text == '国家级')?.total || 0;
  const secondLevelInheritor = data.inheritorData.find((p: any) => p.level_text == '省级')?.total || 0;
  const thirdLevelInheritor = data.inheritorData.find((p: any) => p.level_text == '市级')?.total || 0;

  const projects = (data.ichData as any[]).filter((p: any) => [ '人类非遗', '国家级', '省级', '市级' ].includes(p.level_text)).map((item: any) => {
    if (item.level_text != '人类非遗')
      sumProject += item.total;
    return {
      title: item.level_text,
      value: item.total,
      titleSuffix: '项',
      type: 'forth',
      onClick: () => navTo('/pages/article/data/list', { pageConfigName: 'intangible', tab: 0, level: item.level }),
    } as StatsTextItem
  });
  const inheritors = data.inheritorData.filter((p: any) => [ '国家级', '省级', '市级' ].includes(p.title)).map((item: any) => {
    sumInheritor += item.total;
    return {
      title: item.title,
      value: item.total,
      titleSuffix: '人',
      type: 'normal',
      onClick: () => navTo('/pages/inhert/inheritor/list', { level: item.level }),
    }
  });

  statsText1.value = `目前厦门市非遗项目市级以上共有 ${sumProject} 项，其中：国家级 ${secondLevelProject} 项（含 ${topLevelProject} 项为人类非遗）、省级 ${thirdLevelProject} 项、市级 ${forthLevelProject} 项。`;
  statsText2.value = `目前厦门市非遗传承人市级以上共有 ${sumInheritor} 人，其中：国家级 ${topLevelInheritor} 人、省级 ${secondLevelInheritor} 人、市级 ${thirdLevelInheritor} 人。`;

  const dataMap  = {
    projects,
    inheritors,
    ichCenter: data.ichCenter.map((item: any) => {
      return {
        title: item.title,
        value: item.total,
        titleSuffix: '处',
        type: 'normal',
        onClick: () => navTo('/pages/article/data/list', { pageConfigName: 'seminar', tab: 0, region: item.id }),
      }
    }),
    historyData: data.historyData.map((item: any) => {
      return {
        title: item.title,
        value: item.total,
        titleSuffix: '处',
        onClick: () => {
          switch (item.title) {
            case '世界文化遗产':
              navTo('/pages/article/data/details', { pageConfigName: 'artifact-details', id: 7518 });
              break;
            case '传统村落':
              navTo('/pages/inhert/village/list');
              break;
            case '重点区域':
              navCommonList({
                title: '重点区域',
                modelId: 17,
                mainBodyColumnId: 283
              });
              break;
          }
        },
      }
    }),
    minnanCr: data.minnanCr.map((item: any) => {
      return {
        title: item.title,
        value: item.total,
        titleSuffix: '处',
        onClick: () => navTo('/pages/article/data/list', {
          pageConfigName: 'artifact',
          level: item.level
        }),
      }
    }),
  } as Record<string, StatsTextItem[]>;

  
  return props.statsNameConfig.map((item: any) => {
    return {
      ...item,
      datas: dataMap[item.name],
    }
  });
});

watch(() => props.statsNameConfig, (newVal) => {
  statsLoader.loadData(undefined, true);
}, { deep: true });

</script>