<template>
  <Page >
    <SimplePageListContentLoader class="list d-flex flex-col mt-3 flex-fill" :loader="loader">
      <GridList 
        class="flex-fill"
        :list="loader.list.value"
        :defaultImage="AppCofig.defaultImage" 
        :playAudio="detailPageName === 'Play'"
        @itemClick="handleClick"
      />
      <SimplePageListContentPager :loader="loader" />
    </SimplePageListContentLoader>
  </Page>
</template>

<script setup lang="ts">
import { useSimplePagerDataLoader } from '@/composeable/SimplePagerDataLoader';
import { useRoute, useRouter } from 'vue-router';
import { computed, ref, watch, type PropType } from 'vue';
import CommonContent, { GetContentListItem, GetContentListParams } from '@/api/CommonContent';
import SimplePageListContentLoader from '@/components/SimplePageListContentLoader.vue';
import SimplePageListContentPager from '@/components/SimplePageListContentPager.vue';
import GridList from '@/components/small/GridList.vue';
import Header from '@/components/parts/Header.vue';
import AppCofig from '@/common/config/AppCofig';
import Page from '@/components/parts/Page.vue';

const emit = defineEmits(['itemClick']);
const route = useRoute();
const router = useRouter();
const loader = useSimplePagerDataLoader(16, async (page, size) => {
  let modelId = parseInt(route.query.modelId as string)
  let mainBodyColumnId = parseInt(route.query.mainBodyColumnId as string) as any;
  if (isNaN(mainBodyColumnId))
    mainBodyColumnId = undefined;
  return CommonContent.getContentList(new GetContentListParams()
    .setModelId(modelId)
    .setMainBodyColumnId(mainBodyColumnId as number)
  , page, size)
});
const detailPageName = computed(() => {
  let detailPageName = route.query.detailPageName as string;
  if (!detailPageName)
    detailPageName = 'ArticleDetail';
  return detailPageName;
})

function handleClick(item: GetContentListItem) {
  router.push({
    name: detailPageName.value,
    query: {
      id: item.id,
    }
  })
}
</script>

<style lang="scss" scoped>
.list {
  position: absolute;
  top: 2rem;
  left: 10%;
  right: 10%;
  bottom: 2rem;
}
</style>