<template>
  <CommonListPage 
    title="非遗传承人"
    itemType="article-character"
    detailsPage="/pages/article/data/details?pageConfigName=inheritor-details"
    showTotal
    :dropDownNames="dropdownNames"
    :load="loadData" 
  />
</template>

<script setup lang="ts">
import CommonContent, { GetContentListParams } from '@/api/CommonContent';
import InheritorContent from '@/api/inheritor/InheritorContent';
import CommonListPage, { type DropDownNames } from '@/pages/article/common/CommonListPage.vue';
import { onLoad } from '@dcloudio/uni-app';
import { onMounted, ref } from 'vue';

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

async function loadData(
  page: number, 
  pageSize: number,
  searchText: string,
  dropDownValues: number[]
) {
  const res = (await InheritorContent.getContentList(new GetContentListParams().setSelfValues({
    ichType: dropDownValues[0] == 0 ? undefined: dropDownValues[0],
    level: dropDownValues[1] == 0 ? undefined: dropDownValues[1],
    region: dropDownValues[2] == 0 ? undefined: dropDownValues[2],
    keywords: searchText,
  }), page, pageSize));
  res.list.forEach((p) => {
    p.desc = p.ichName as string;
    p.titleBox = Boolean(p.deathBirth);
    p.bottomTags = [
      p.levelText, 
      p.nation,
    ];
  })
  return res;
}

onLoad(async (querys) => {
  dropdownNames.value.push({ 
    options: [{
      id: 0, 
      name: '全部类别'
    }].concat((await CommonContent.getCategoryList(4)).map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    defaultSelectedValue: 0,
  });
  const levels = await CommonContent.getCategoryList(2);
  dropdownNames.value.push({ 
    options: [{
      id: 0, 
      name: '全部级别'
    }].concat(levels.map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    defaultSelectedValue: querys?.level ?? 0,
  });
  console.log('level', querys?.level);
  
  dropdownNames.value.push({  
    options: [{
      id: 0, 
      name: '全部区域'
    }].concat((await CommonContent.getCategoryList(1)).map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    defaultSelectedValue: 0,
  });
})
</script>
