<template>
  <div class="news-detail-wrap">
    <template v-if="newDetail.title">
      <h1 class="box-title" id="title">{{newDetail.title}}</h1>
      <div class="box-cen">
        <span class="box-autor" id="autor">HKT</span>
        <span class="box-time" id="time">{{newDetail?.publishTime?.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:{code:newsNext.id}}" class="box-prev-page-t">Previous</NuxtLink>
          <NuxtLink :to="{path:'/news/detail',query:{code: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:{code:newsPerv.id}}" class="box-prev-page-t">Next</NuxtLink>
          <NuxtLink :to="{path:'/news/detail',query:{code:newsPerv.id}}" class="box-prev-page-b">{{newsPerv.title}}</NuxtLink>
        </div>
      </div>
    </template>
  </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, getpreNext } = useAll();
const route = useRoute();
const nuxtApp = useNuxtApp();
const isClient = !nuxtApp.ssrContext;
let query: any = ref({
  id: route.query.code as any,
  language: 2,
});
const newDetail: any = ref({});
const newsNext: any = ref({});
const newsPerv: any = ref({});
watch(
  () => route.query.code,
  async () => {
    window.scrollTo({
      top: 0,
      behavior: "smooth",
    });

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

    // 等待请求完成
    await getNewsDetailFun();
    // 关闭加载
    toast.close();
    await getpreNextFun();
  }
);

const user = useUserStore();
async function getNewsDetailFun() {
  let res: any = await getNewsDetail(route.query.code);
  if (res.httpCode == 200) {
    newDetail.value = res.data;
    user.value.adminTitle = newDetail.value.title
  }
}
async function getpreNextFun() {
  let res: any = await getpreNext(route.query.code);
  if (res.httpCode == 200) {
    newsNext.value = res.data[0];
    if (res.data.length > 1) {
      newsPerv.value = res.data[1];
    } else {
      newsPerv.value = {};
    }
  }
}
onMounted(async () => {
  // 显示加载
  const toast = showLoadingToast({
    message: "Loading...",
    forbidClick: true,
    duration: 0,
  });

  // 等待请求完成
  await getNewsDetailFun();
  // 关闭加载
  toast.close();
  await getpreNextFun();
});
</script>
<style scoped lang="scss">
.news-detail-wrap {
  min-height: 100vh;
  background: #fff;
  padding: 12px 12px 20px;
  .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>
