<template>
  <FlexCol :padding="[0,0,50,0]">
    <SimplePageContentLoader :loader="loader">
      <template v-if="loader.content.value">
        <FlexCol :gap="20">
          <swiper 
            v-if="loader.content.value.images.length > 0"
            circular 
            :indicator-dots="true"
            :autoplay="true"
            :interval="3000"
            :duration="1000"
            :style="{ height: '500rpx' }"
          >
            <swiper-item v-for="(item, key) in loader.content.value.images" :key="key">
              <view class="item">
                <Image 
                  :src="item" 
                  width="100%"
                  :height="500"
                  radius="radius.md"
                  mode="aspectFill" 
                  @click="onPreviewImage(key)"
                />
              </view>
            </swiper-item>
          </swiper>
          <video 
            v-else-if="loader.content.value.video"
            :src="loader.content.value.video"
            :style="{ width: '100%', height: '500rpx' }"
          />
          <Image
            v-else-if="loader.content.value.image"
            :src="loader.content.value.image"
            width="100%"
            mode="widthFix"
          />
          <FlexCol :padding="30" :gap="20">
            <H3 class="size-ll color-title-text">{{ loader.content.value.title }}</H3>
            <FlexRow :gap="20">
              <Text v-if="loader.content.value.from" color="text.second" :size="28">来源：{{ loader.content.value.from }}</Text>
              <Text v-if="loader.content.value.keywords" color="text.second" :size="28">{{ loader.content.value.keywords.join(' ') }}</Text>
              <Text color="text.second" :size="28">{{ DataDateUtils.formatDate(loader.content.value.publishAt, 'YYYY-MM-dd') }}</Text>
            </FlexRow>
          </FlexCol>
          <FlexCol :padding="[0,30,30,30]">
            <ImageBlock3
              :src="loader.content.value.image"
              :imageWidth="100"
              :imageHeight="100"
              radius="radius.md"
              :title="loader.content.value.villageName"
              :desc="`${loader.content.value.villageProvince}${loader.content.value.villageCity}`"
            />
          </FlexCol>
          <FlexCol :padding="30">
            <Parse
              v-if="loader.content.value.content"
              :content="loader.content.value.content"
            />
            <Text v-if="emptyContent">暂无简介</Text>
          </FlexCol>
          
          <!-- 推荐 -->
          <FlexCol v-if="recommendListLoader.content.value?.length" :padding="30">
            <H5>相关推荐</H5>
            <Box2LineImageRightShadow
              titleColor="title-text"
              v-for="item in recommendListLoader.content.value"
              :key="item.id"
              :image="item.image || AppCofig.defaultImage"
              :title="item.title"
              :desc="item.desc"
              :badge="item.villageName"
              :wideImage="true"
              @click="goDetails(item.id)"
            />
          </FlexCol>
        </FlexCol>
      </template>
    </SimplePageContentLoader>
  </FlexCol>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { useSimpleDataLoader } from "@/components/composeabe/loader/SimpleDataLoader";
import { useSwiperImagePreview } from "@/common/composeabe/SwiperImagePreview";
import { useLoadQuerys } from "@/components/composeabe/LoadQuerys";
import { navTo } from "@/components/utils/PageAction";
import { onShareTimeline, onShareAppMessage } from "@dcloudio/uni-app";
import { DataDateUtils } from "@imengyu/js-request-transform";
import SimplePageContentLoader from "@/components/loader/SimplePageContentLoader.vue";
import Parse from "@/components/display/parse/Parse.vue";
import Box2LineImageRightShadow from "@/common/components/parts/Box2LineImageRightShadow.vue";
import AppCofig from "@/common/config/AppCofig";
import VillageInfoApi from "@/api/inhert/VillageInfoApi";
import FlexRow from "@/components/layout/FlexRow.vue";
import FlexCol from "@/components/layout/FlexCol.vue";
import Icon from "@/components/basic/Icon.vue";
import Image from "@/components/basic/Image.vue";
import Text from "@/components/basic/Text.vue";
import H5 from "@/components/typography/H5.vue";
import H3 from "@/components/typography/H3.vue";
import ImageBlock3 from "@/components/display/block/ImageBlock3.vue";

const { onPreviewImage } = useSwiperImagePreview(() => loader.content.value?.images || []);
const emptyContent = computed(() => (loader.content.value?.content || '').trim() === '');

const loader = useSimpleDataLoader(async () => {
  const res = await VillageInfoApi.getInfoForDiscover(querys.value.id);
  uni.setNavigationBarTitle({ title: res.title });
  return res;
});
const recommendListLoader = useSimpleDataLoader(async () => {
  if (!querys.value.keywords)
    return []
  return (await VillageInfoApi.getListForDiscover(1, 5, querys.value.keywords))
    .list
    .filter((p) => p.id !== querys.value.id);
});

const { querys } = useLoadQuerys({ 
  id: 0,
  keywords: '',
}, (t) => loader.load(false, t));

function goDetails(id: number) {
  navTo('details', { 
    id,
    keywords: querys.value.keywords,
  });
}

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>
