<template>
  <view class="video-details main bg-base pb-5">
    <SimplePageContentLoader :loader="loader">
      <template v-if="loader.content.value">
        
        <video 
          v-if="loader.content.value.audio"
          class="video"
          autoplay
          :poster="loader.content.value.image"
          :src="loader.content.value.audio"
          controls
        />
        <video
          v-else-if="loader.content.value.video"
          class="video"
          autoplay
          :poster="loader.content.value.image"
          :src="loader.content.value.video"
          controls
        />
        <view class="d-flex flex-col">
          <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 class="size-s color-text-content-second">{{ DataDateUtils.formatDate(loader.content.value.publishAt, 'YYYY-MM-dd') }}</text>
            </view>
          </view>
          <view class="content radius-l bg-light p-3">
            <Parse :content="loader.content.value.content"/>
            <text v-if="emptyContent" class="size-s color-text-content-second">暂无简介</text>
          </view>

          <!-- 推荐视频 -->
          <view v-if="recommendListLoader.content.value?.length && querys.showRecommend" 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"
              :title="item.title"
              :desc="item.desc"
              :tags="item.bottomTags"
              :wideImage="true"
              @click="goDetails(item.id)"
            />
          </view>
        </view>
        <ContentNote />
        <LikeFooter :content="loader.content.value">
          <template #left>
            <ArticleCorrect :content="loader.content.value" />
          </template>
        </LikeFooter>

      </template>
    </SimplePageContentLoader>
  </view>
</template>

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

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

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)
    .map((p) => ({
      ...p,
      bottomTags: [
        p.mainBodyColumnName,
        p.levelText, 
      ] as string[],
    }));
});

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

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

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();
})

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

<style lang="scss">
.video-details {
  padding: 0;

  .nested-content {
    padding: 15rpx 25rpx;
  }
  video {
    width: 750rpx;
  }
}
</style>
