<template>
  <!-- 文化传承 -  -->
   <CommonListPage
    :title="'非遗传承人'"
    :prevPage="{ title: '保护传承' }"
    :dropDownNames="dropdownNames"
    :pageSize="8"
    :load="loadData"
    :loadDetail="loadDetail"
    detailsPage="/inheritor/intangible-detail"
  />
</template>

<script setup lang="ts">
import { GetContentListParams } from '@/api/CommonContent';
import InheritorContent from '@/api/inheritor/InheritorContent';
import IndexContent from '@/api/introduction/IndexContent';
import type { DropDownNames } from '@/components/content/CommonListPage.vue';
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';

async function loadDetail(id: number, item: any) {
  item = await InheritorContent.getContentDetail(id);
  item.content = item.content || item.intro as string;
  item.addItems = [
    { name: '传承项目', text: item.ichName },
    { name: '级别', text: item.levelText },
    { name: '民族', text: item.nation },
  ];
  return item;
}
async function loadData(
  page: number, 
  pageSize: number,
  selectedTag: number,
  searchText: string,
  dropDownValues: number[]
) {

  const res = await InheritorContent.getContentList(new GetContentListParams().setSelfValues({
    ichType: selectedTag == 0 ? undefined: selectedTag,
    level: dropDownValues[0] == 0 ? undefined: dropDownValues[0],
    region: dropDownValues[1] == 0 ? undefined: dropDownValues[1],
    keywords: searchText,
  }), page, pageSize);

  return { 
    page: page,
    total: res.total,
    data: res.list.map((item, index) => {
      return {
        id: item.id,
        title: item.title,
        desc: item.desc,
        image: item.image,
        addItems: [
          { name: '传承项目', text: item.ichName },
          { name: '级别', text: item.levelText },
          { name: '民族', text: item.nation },
        ],
      };
    }),
  }
}

const dropdownNames = ref<DropDownNames[]>([]);
const route = useRoute();

onMounted(async () => {
  const levels = await IndexContent.getCategoryList(2);
  dropdownNames.value.push({ 
    options: [{
      id: 0, 
      name: '全部'
    }].concat(levels.map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    defaultSelectedValue: (route.query.level ? levels.find(p => p.title == route.query.level)?.id : undefined) ?? 0,
  });
  dropdownNames.value.push({ 
    options: [{
      id: 0, 
      name: '全部'
    }].concat((await IndexContent.getCategoryList(1)).map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    defaultSelectedValue: 0,
  });
})
</script>

<style>
</style>

