<template>
  <CommonListPage 
    title="非遗"
    itemType="image-large-2"
    detailsPage="/pages/inhert/intangible/details"
    :dropDownNames="dropdownNames"
    :tabs="[
      { id: 0, name: '非遗项目' },
      { id: 1, name: '非遗产品' },
      { id: 2, name: '非遗传承人', jump: () => navTo('/pages/inhert/inheritor/list') },
    ]"
    :load="loadData" 
  />
</template>

<script setup lang="ts">
import CommonContent, { GetContentListParams } from '@/api/CommonContent';
import ProductsContent from '@/api/inheritor/ProductsContent';
import ProjectsContent from '@/api/inheritor/ProjectsContent';
import { navTo } from '@/common/utils/PageAction';
import CommonListPage, { type DropDownNames } from '@/pages/article/common/CommonListPage.vue';
import { onMounted, ref } from 'vue';

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

async function loadData(
  page: number, 
  pageSize: number,
  searchText: string,
  dropDownValues: number[],
  tabSelect: number,
) {
  let api;
  switch (tabSelect) {
    case 0: api = ProjectsContent; break;
    default:
    case 1: api = ProductsContent; break;
  }
  return (await api.getContentList(new GetContentListParams().setSelfValues({
    ichType: tabSelect !== 0 || dropDownValues[0] == 0 ? undefined: dropDownValues[0],
    level: tabSelect !== 0 || dropDownValues[1] == 0 ? undefined: dropDownValues[1],
    region: tabSelect !== 0 || dropDownValues[2] == 0 ? undefined: dropDownValues[2],
    keyword: searchText,
  }), page, pageSize)).list;
}

onMounted(async () => {
  dropdownNames.value.push({ 
    options: [{
      id: 0, 
      name: '全部分类'
    }].concat((await CommonContent.getCategoryList(4)).map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    activeTab: 0,
    defaultSelectedValue: 0,
  });
  dropdownNames.value.push({ 
    options: [{
      id: 0, 
      name: '全部级别'
    }].concat((await CommonContent.getCategoryList(2)).map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    activeTab: 0,
    defaultSelectedValue: 0,
  });
  dropdownNames.value.push({ 
    options: [{
      id: 0, 
      name: '全部地区'
    }].concat((await CommonContent.getCategoryList(1)).map((item) => ({
      id: item.id,
      name: item.title,
    }))),
    activeTab: 0,
    defaultSelectedValue: 0,
  });
})
</script>
