<script setup>
import {onMounted, ref} from 'vue'
import {useRoute, useRouter} from 'vue-router'

import {getNewsDetail} from '@/api/index'
import {ElMessage} from 'element-plus'

const route = useRoute()
const detail = route.query
const showLoad = ref(true)
const newsTitle = ref('')
const newsSubTitle = ref('')
const newsCoverImg = ref('')
const newsContent = ref('')

const getDetail = () => {
  showLoad.value = true
  let query = {
    slug: detail.slug,
  }
  getNewsDetail(query)
    .then((res) => {
      if (res.code === 0) {
        let {content, cover_image, snapshot, title} = res.data
        newsTitle.value = title
        newsSubTitle.value = snapshot
        newsCoverImg.value = cover_image
        newsContent.value = content
        showLoad.value = false
      } else {
        ElMessage.error(res.msg)
        showLoad.value = false
      }
    })
    .catch((err) => {
      showLoad.value = false
    })
}

const router = useRouter()
onMounted(() => {
  getDetail()
})

const toBack = () => {
  router.back()
}
</script>

<template>
  <div class="page flex column a-c" v-loading="showLoad">
    <div v-if="newsTitle">
      <h3 class="label">
        {{ newsTitle }}
      </h3>
      <h3 class="time" v-if="newsSubTitle">
        {{ newsSubTitle }}
      </h3>
      <div class="img" v-if="newsCoverImg">
        <img :src="newsCoverImg" alt="" />
      </div>
      <div class="artical" v-if="newsContent">
        {{ newsContent }}
      </div>
    </div>
    <div v-else>暂无内容</div>
    <div class="back" @click="toBack">返回</div>
  </div>
</template>

<style lang="scss" scoped>
.label {
  margin: 64px 0 20px 0;
  font-family: 'PingFang SC';
  font-weight: 500;
  font-size: 28px;
  color: $black;
}
.time {
  font-family: 'PingFang SC';
  font-weight: 500;
  font-size: 18px;
  margin-bottom: 40px;
}
.img {
  width: 1200px;
  height: auto;
  margin-bottom: 40px;
  img {
    width: 100%;
    height: 100%;
  }
}
.artical {
  width: 1200px;
  font-family: 'PingFang SC';
  font-weight: 400;
  font-size: 18px;
  color: $black;
  text-indent: 2em;
  white-space: pre-wrap; /* 保留换行符，同时允许自动折行 */
  word-break: break-word; /* 长单词截断 */
  margin-bottom: 300px;
}
.back {
  padding: 10px 40px;
  background: #0389ff;
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.25);
  border-radius: 16px;
  margin-bottom: 60px;
  font-size: 20px;
  color: $white;
  cursor: pointer;
}
</style>
