<template>
  <CommonListPage 
    :title="querys.title || undefined"
    :load="loadData"
    :itemType="querys.itemType as any || undefined"
    :detailsPage="querys.detailsPage || undefined"
    :detailsParams="{
      mainBodyId: querys.mainBodyId || undefined,
      mainBodyColumnId: querys.mainBodyColumnId || undefined,
      modelId: querys.modelId || undefined,
    }"
    :intitalSearch="querys.search"
  />
</template>

<script setup lang="ts">
import { useLoadQuerys, stringDotNumbersToNumbers } from '@/components/composeabe/LoadQuerys';
import CommonListPage from './CommonListPage.vue';
import CommonContent, { GetContentListParams } from '@/api/CommonContent';
import { redirectTo } from '@/components/utils/PageAction';

const { querys } = useLoadQuerys({
  villageId: 0,
  mainBodyId: 0,
  mainBodyColumnId: '',
  modelId: 0,
  itemType: '',
  detailsPage: '',
  title: '',
  region: '',
  search: '',
  latitude: 0,
  longitude: 0,
  isNearby: false,
  autoJumpFirst: false,
});

async function loadData(
  page: number, 
  pageSize: number,
  searchText: string,
  dropDownValues: number[]
) {
  const mainBodyColumnId = stringDotNumbersToNumbers(querys.value.mainBodyColumnId);

  const res = await CommonContent.getContentList(new GetContentListParams().setSelfValues({
    villageId: querys.value.villageId || undefined,
    mainBodyId: querys.value.mainBodyId || undefined,
    mainBodyColumnId: mainBodyColumnId || undefined,
    modelId: querys.value.modelId || undefined,
    keywords: searchText,
    region: querys.value.region || undefined,
    latitude: querys.value.latitude || undefined,
    longitude: querys.value.longitude || undefined,
  }), page, pageSize, undefined, querys.value.isNearby);

  for (const element of res.list || []) {
    if (!element.desc && element.from)
      element.desc = `来源：${element.from}`;
  }

  if (querys.value.autoJumpFirst) {
    redirectTo('/pages/article/details', {
      id: res.list[0].id,
    })
  }

  return res;
}
</script>