<script>

import ArticleAdd from "~/pages/manger/news/add";
import {getQueryString} from "../../../until/domTool";
import {unescapeHtml} from "../../../until/unescapeHtml";
import {timestampToTime} from "../../../until/time";
import {rCode} from "../../../map/rcodeMap_esm";
import {handle} from "~/until/handle";
import {apiMap, baseUrl} from "~/map/apiMap";

export default {
  name: "articleEdit",
  components: {ArticleAdd},
  data() {
    return {
      articleInfo: {}
    }
  },
  beforeMount() {
    // 获取路径中的id值
    this.newsId = getQueryString('id');
    if( !this.newsId ){
      this.$message.error('未获取到文章id');
      window.location.href = "/manger/news";
      return ;
    }
    console.log(this.newsId);
    this.getArticleInfo();
  },
  methods: {
    async getArticleInfo() {
      let newsId = this.newsId;
      if( !newsId ){
        this.$message.error('未获取到文章id');
        return;
      }
      this.loading = true;
      let [err,res] = await handle(this.$axios.get(`${apiMap.newsInfo.path}/${newsId}`));
      this.loading = false;
      if(err){
        this.$message.error('获取文章信息失败');
        return console.log(err);
      }
      let  result = res.data;
      if (result.code === rCode.OK){
        this.$message.success('');
        let pageData = result.data;
        pageData.htmlContent = unescapeHtml(pageData.content);
        pageData.createTime = timestampToTime(pageData.date_time);
        //
        this.articleInfo = pageData;
        console.log(this.newsInfo);
        this.$message.success('获取文章信息成功');
      }else{
        this.$message.error(`获取文章信息失败,${result.msg}`);
      }
    }
  }
}

</script>

<template>
<article-add :article="articleInfo"/>
</template>

<style scoped>

</style>
