<template>
  <div class="news-detail-wrap">
    <h1 class="box-title" id="title">{{newDetail.title}}</h1>
    <div class="box-cen">
      <span class="box-autor" id="autor">HOTKING</span>
      <span class="box-time" id="time">{{newDetail?.updateTime?.substr(0,19)}}</span>
    </div>
    <div class="box-desc" id="desc">
      <p>{{newDetail.description}}</p>
    </div>
    <div id="desc" v-html="newDetail.content">
    </div>
    <div class="box-page clear">
      <div class="box-next-page page-1" id="next-page" v-if="newsNext.id">
        <NuxtLink :to="{path:'/news/detail',query:{id:newsNext.id}}" class="box-prev-page-t">上一篇</NuxtLink>
        <NuxtLink :to="{path:'/news/detail',query:{id:newsNext.id}}" class="box-prev-page-b">{{newsNext.title}}</NuxtLink>
      </div>
      <div class="box-prev-page page-1" id="prev-page" v-if="newsPerv.id">
        <NuxtLink :to="{path:'/news/detail',query:{id:newsPerv.id}}" class="box-prev-page-t">下一篇</NuxtLink>
        <NuxtLink :to="{path:'/news/detail',query:{id:newsPerv.id}}" class="box-prev-page-b">{{newsPerv.title}}</NuxtLink>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Search } from "@element-plus/icons-vue";
import { useAll } from "~/api/pubAll/service";
import { showLoadingToast, closeToast } from "vant";
const { getNewsDetail } = useAll();
const route = useRoute();
let pageTitle: any = ref("HOTKING-规格书");
useHead(() => ({
  title: pageTitle.value,
}));
const nuxtApp = useNuxtApp();
const isClient = !nuxtApp.ssrContext;
let query: any = ref({
  id: route.query.id as any,
});
const {
  data: ssrNewsDetail,
  error,
  refresh,
} = await useAsyncData<any>(`newsDetail-${route.query.id}`, () =>
  getNewsDetail({
    id: route.query.id,
  })
);
const newDetail = computed(() => ssrNewsDetail.value?.data?.news || {});
const newsNext = computed(() => ssrNewsDetail.value?.data?.next || {});
const newsPerv = computed(() => ssrNewsDetail.value?.data?.last || {});

watch(
  () => route.query.id,
  async () => {
    window.scrollTo({
      top: 0,
      behavior: "smooth",
    });

    // 显示加载
    const toast = showLoadingToast({
      message: "加载中...",
      forbidClick: true,
      duration: 0,
    });

    // 等待请求完成
    await refresh();

    // 关闭加载
    toast.close();
  }
);

</script>
<style scoped lang="scss">
.news-detail-wrap {
  min-height: 100vh;
  background: #fff;
  padding: 12px 12px 66px;
  .box-title {
    font-size: 16px;
    color: #333;
    text-align: center;
    margin-bottom: 12px;
    line-height: 24px;
  }
  .box-cen {
    font-size: 12px;
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    color: #666;
    .box-autor {
      color: $mainColor;
      margin-right: 5px;
    }
  }
  .box-desc {
    background: #f4f4f4;
    padding: 12px;
    font-size: 12px;
    line-height: 20px;
    margin-bottom: 12px;
  }
  .box-page {
    margin-top: 20px;
    font-size: 14px;
    line-height: 20px;
    margin-bottom: 20px;
    .page-1{
      background: #f7f7f7;
      padding: 12px;
    }
    .box-prev-page-t {
      margin-right: 10px;
    }
    .box-prev-page {
      margin-top: 10px;
    }
  }
}
</style>