<template>
  <view class="d-flex flex-column bg-base pb-45">
    <SimplePageContentLoader :loader="loader">
      <template v-if="loader.content.value">
        <view class="d-flex flex-col">
          <swiper 
            v-if="loader.content.value.images.length > 0"
            circular 
            :indicator-dots="true"
            :autoplay="true"
            :interval="3000"
            :duration="1000"
            class="height-500"
          >
            <swiper-item v-for="(item, key) in loader.content.value.images" :key="key">
              <view class="item">
                <image 
                  :src="item" 
                  class="w-100 height-500 radius-base"
                  mode="aspectFill" 
                  @click="onPreviewImage(key)"
                />
              </view>
            </swiper-item>
          </swiper>
          <image 
            v-else-if="loader.content.value.image"
            class="w-100 radius-base"
            :src="loader.content.value.image"
            mode="widthFix"
          />
          <view class="d-flex flex-col p-3">
            <view class="size-ll color-title-text">{{ loader.content.value.title }}</view>
            <view class="d-flex flex-row mt-2">
              <text v-if="loader.content.value.from" class="size-s color-text-content-second mr-2 ">来源：{{ loader.content.value.from }}</text>
              <text class="size-s color-text-content-second">{{ DataDateUtils.formatDate(loader.content.value.publishAt, 'YYYY-MM-dd') }}</text>
            </view>
          </view>
          <view class="p-3 radius-ll bg-light mt-3">
            <Parse
              v-if="loader.content.value.content"
              :content="loader.content.value.content"
              :tagStyle="commonParserStyle"
            />
            <text v-if="emptyContent">暂无简介</text>
          </view>
          
          <!-- 推荐 -->
          <view v-if="recommendListLoader.content.value?.length" class="d-flex flex-col p-3">
            <text class="size-base text-bold mb-3">相关推荐</text>
            <Box2LineImageRightShadow
              class="w-100"
              titleColor="title-text"
              v-for="item in recommendListLoader.content.value"
              :key="item.id"
              :image="item.thumbnail || item.image || AppCofig.defaultImage"
              :title="item.title"
              :desc="item.desc"
              :badge="item.badge"
              :wideImage="true"
              @click="goDetails(item.id)"
            />
          </view>
        </view>
      </template>
    </SimplePageContentLoader>
  </view>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { useSimpleDataLoader } from "@/common/composeabe/SimpleDataLoader";
import { useSimplePageContentLoader } from "@/common/composeabe/SimplePageContentLoader";
import { useSwiperImagePreview } from "@/common/composeabe/SwiperImagePreview";
import { useLoadQuerys } from "@/common/composeabe/LoadQuerys";
import { navTo } from "@/components/utils/PageAction";
import { onShareTimeline, onShareAppMessage } from "@dcloudio/uni-app";
import { DataDateUtils } from "@imengyu/js-request-transform";
import type { GetContentDetailItem } from "@/api/CommonContent";
import commonParserStyle from "@/common/style/commonParserStyle";
import SimplePageContentLoader from "@/common/components/SimplePageContentLoader.vue";
import Parse from "@/components/display/parse/Parse.vue";
import CommonContent, { GetContentListParams } from "@/api/CommonContent";
import Box2LineImageRightShadow from "@/common/components/parts/Box2LineImageRightShadow.vue";
import AppCofig from "@/common/config/AppCofig";

const loader = useSimplePageContentLoader<
  GetContentDetailItem, 
  { id: number }
>(async (params) => {
  if (!params)
    throw new Error("!params");
  const res = await CommonContent.getContentDetail(params.id);
  //console.log(res);
  uni.setNavigationBarTitle({ title: res.title });
  return res;
});

const { onPreviewImage } = useSwiperImagePreview(() => loader.content.value?.images || [])

const emptyContent = computed(() => (loader.content.value?.content || '').trim() === '')

const recommendListLoader = useSimpleDataLoader(async () => {
  if (!querys.value.modelId)
    return []
  return (await CommonContent.getContentList(new GetContentListParams()
    .setModelId(querys.value.modelId)
    .setMainBodyColumnId(querys.value.mainBodyColumnId)
  , 1, 10)).list.filter((p) => p.id !== querys.value.id);
});


function goDetails(id: number) {
  navTo('/pages/article/details', { 
    id, 
    mainBodyColumnId: querys.value.mainBodyColumnId, 
    modelId: querys.value.modelId 
  });
}

const { querys } = useLoadQuerys({ 
  id: 0,
  mainBodyColumnId: 0,
  modelId: 0,
}, (t) => loader.loadData(t));

function getPageShareData() {
  if (!loader.content.value)
    return { title: '文章详情', imageUrl: '' }
  return {
    title: loader.content.value.title,
    imageUrl: loader.content.value.images[0],
  }
}
onShareTimeline(() => {
  return getPageShareData(); 
})
onShareAppMessage(() => {
  return getPageShareData();
})
</script>
