<template>
  <!-- 文章内容 -->
  <!-- title-->
  <div class="member container">
    <div class="header container" style="padding-left: 0 !important;">
      <div class="title">{{ article.title }}</div>
      <div class="author">{{ article.author }}</div>
      <div class="aug">{{ article.date }}</div>
    </div>
    <!-- 分割线 -->
    <LineCom/>
    <div class="right container">
      <!-- 文章区域 -->
      <div class="ContentArticle" v-html="article.content">
      </div>
      <div class="maxShare ">
        <!-- 分享 -->
        <div class="sharePadding">
          <div class="share">
            <div class="sharetext" style="display: flex;flex-direction: column">
              <span>Share Posts</span>

              <div class="shareContent" style="margin: 0 !important;">
                <div class="shareImg twitter" @click="handleShare">
                </div>
                <a :href="Linkedin" target="_blank">
                  <div class="shareImg linkedin" style="margin: 0 30px">
                  </div>
                </a>

                <a :href="telegram" target="_blank">
                  <div class="shareImg telegram">
                  </div>
                </a>
              </div>
            </div>
          </div>
        </div>
        <!-- 相关文章 -->
        <div class="relatedArticles">
          <div class="relatedTitle">Related Posts</div>
          <div class="iprelatedContent">
            <div :class="`relatedContent ${index === 0? 'relatedContentOne' : ''}` "
                 v-for="(item, index) in recommendList" :key="index" @click="toDetail(item)">
              <img :src="item.cover_image" alt="The image cannot be displayed properly"/>
              <h1>{{ item.title }}</h1>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import {nextTick, onMounted, ref, watch} from "vue";
import {useRoute} from "vue-router";
import LineCom from "../components/LineCom.vue";
import {getArticleDetail, recommendResearchApi} from "../api/research.js";
import {transformDate} from "../utils/tools.js";
import router from "../router/index.js";
import {clickNewsApi, getNewsApi} from "../api/manage.js";

const route = useRoute();
const contentId = ref(route.query.contentId);
const articleType = ref(route.query.type);

const recommendList = ref([])

let article = ref({
  title: ''
})


const Linkedin = ref('')
const telegram = ref('')
const href = ref(null)
const getData = () => {
  getArticleDetail(contentId.value).then(async ({data}) => {
    if (data) {
      article.value = {...data, date: transformDate(data.publishedtime)};
      nextTick(() => {
        document.head.innerHTML += `<meta property="twitter:url" content="${location.href}"/>
  <meta property="twitter:title" content="${article.value.title}"/>
  <meta property="twitter:description" content="${article.value.description}"/>
  <meta property="twitter:site" content="${location.href}">
  <meta name="twitter:image:src" content="${import.meta.env.VITE_IMG_DOMAIN_URL + article.value.cover_image}"/>
`

        href.value = encodeURIComponent(location.href)
        const _href = "https://www.linkedin.com/shareArticle?mini=true";

        Linkedin.value = `${_href}&title=${article.value.title}&url=${encodeURIComponent(location.href)}`
        telegram.value = `https://t.me/share/url?&title=${article.value.title}&url=${encodeURIComponent(location.href)}`
      })
    }
  })
  recommendResearchApi({research_id: contentId.value, page: 1, limit: 3}).then((res) => {
    if (res.data) {
      recommendList.value = res.data.data
    }
  })
  nextTick(() => {
    const content = document.getElementsByClassName('view-content')
    content[0].scrollTop = 0;
  })
}
  const getNewsData = () => {
  clickNewsApi({news_id: contentId.value}).then(async res => {
    if (res.data) {

      article.value = {...res.data, date: transformDate(res.data.publishedtime)};
      nextTick(() => {
        document.head.innerHTML += `<meta property="twitter:url" content="${location.href}"/>
  <meta property="twitter:title" content="${article.value.title}"/>
  <meta property="twitter:description" content="${article.value.description}"/>
  <meta property="twitter:site" content="${location.href}">
  <meta name="twitter:image:src" content="${import.meta.env.VITE_IMG_DOMAIN_URL + article.value.cover_image}"/>
`

        href.value = encodeURIComponent(location.href)
        const _href = "https://www.linkedin.com/shareArticle?mini=true";
        Linkedin.value = `${_href}&title=${article.value.title}&url=${encodeURIComponent(location.href)}`
        telegram.value = `https://t.me/share/url?&title=${article.value.title}&url=${encodeURIComponent(location.href)}`
      })
    }
  })
  getNewsApi({page: 1, limit: 3}).then((res) => {
    if (res.data) {
      recommendList.value = res.data.data
    }
  })
}

const handleShare = ()=>{
  console.log(location.href)
  window.open(`https://twitter.com/share?title=${article.value.title + encodeURIComponent('\n')}&url=${encodeURIComponent(location.href)}`)
}

const toDetail = (item)=> {
  contentId.value = item.id
  if (articleType.value === 'news') {
    clickNewsApi({news_id: item.id}).then(res=>{
      if (res.data) {
        if(res.data.type === 1) {
          router.replace({
            path:'/ContentPages',
            query:{
              contentId: item.id,
              type: 'news'
            }
          })
          getNewsData()
        } else {
          window.open(res.data.link)
        }

      }
    })

  }else {
    router.replace({
      path:'/ContentPages',
      query:{
        contentId: item.id
      }
    })
    getData()
  }
}


onMounted(() => {
  if (articleType.value === 'news') {
    getNewsData()
  }else {
    getData()
  }
})
watch(()=> route.query.contentId,(n,v)=>{
  contentId.value = route.query.contentId
  if (articleType.value === 'news') {
getNewsData()
  }else {
    getData()
  }
})

</script>
<style lang="scss" scoped>
$margin: 15px;

body {
  background: none !important;
}

.member {
  max-width: 1000px;
  margin: 0 auto;
}

.ContentArticle {
  font-family: "Raleway";
  font-style: normal;
  font-weight: 400;
  font-size: 12px;
  line-height: 18px;
  text-align: justify;
  color: rgba(0, 0, 0, 0.5);

  :deep(*) {
    // 设置一个错误的样式，消除 reset css， 确保从后端拿过来的富文本正常显示
    box-sizing: s;
    list-style: s;
    margin: s;
    padding: s;
  }
}

:deep(.ContentArticleH1) {
  font-family: "Libre Bodoni";
  font-style: normal;
  font-weight: 400;
  font-size: 18px;
  line-height: 24px;
  text-align: justify;
  word-break: break-all;
  color: #000000;
}

.title {
  font-family: "Libre Bodoni";
  font-style: normal;
  font-weight: 400;
  font-size: 18px;
  line-height: 24px;
  color: #000000;
}

.author {
  font-family: "Raleway";
  font-style: italic;
  font-weight: 700;
  font-size: 14px;
  line-height: 21px;
  color: rgba(0, 0, 0, 0.5);
}

.aug {
  font-family: "Raleway";
  font-style: normal;
  font-weight: bolder;
  font-size: 12px;
  line-height: 21px;
  color: rgba(0, 0, 0, 0.25);
}

.divider {
  margin: 0 15px;
  border: 0.1px solid rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.twitter {
  background-image: url('/images/About/twitter.png');
  background-size: 100%;
}

.twitter:hover {
  background-image: url('/images/About/twitter_a.png');
}

.linkedin {
  background-image: url(/images/About/linkedin.png);
  background-size: 100%;
}

.linkedin:hover {
  background-image: url(/images/About/linkedin_a.png);
}

.telegram {
  background-image: url(/images/About/telegram.png);
  background-size: 100%;
}

.telegram:hover {
  background-image: url(/images/About/telegram_a.png);
  background-size: 100%;
}

.relatedContent {
  h1 {
    font-family: 'Libre Bodoni';
    font-style: normal;
    font-weight: 400;
    font-size: 15px;
    line-height: 19px;
    color: #000000;
    font-size: 15px;
  }

  img {
    width: 7rem;
    height: 3.8rem;
  }
}

@media screen and (min-width: 1440PX) and (max-width: 100VW) {
  .right {
    position: relative;
    padding: 0 !important;
    display: flex;
    justify-content: space-between;
  }

  .maxShare {
    width: 346Px;
    box-sizing: border-box;
    //position: absolute;
    //top: 80px;
    //right: 0px;
    padding-top: 50px;
    flex-shrink: 0;
    margin-left: 20px;
  }

  .ArticleFour {
    margin-top: 20px;
  }

  .Contentcell {
    margin-top: 30px;
  }

  :deep(.ContentArticle) {
    font-family: "Raleway";
    font-style: normal;
    font-weight: 400;
    font-size: 16px;
    line-height: 32px;
    /* or 160% */

    text-align: justify;

    color: rgba(0, 0, 0, 0.5);

    width: 900Px;
    margin-top: 50px;
    margin-bottom: 124px;
    //padding-right: 30px;

    .ContentArticleH1 {
      margin-top: 30px;
    }
  }

  ::v-deep .ArticleTwo {
    width: 100%;
    text-align: center;

    .ArticleTwo-img {
      width: 100% !important;
      height: 100%;
      margin: 30px 0;
    }
  }

  .title {
    font-size: 28px;
    line-height: 44px;
  }

  .author {
    margin-top: 15px;
    font-size: 16px;
  }

  .aug {
    font-size: 16px;
    margin-top: 10px;
    margin-bottom: 20px;
  }

  .share {
    padding: 20px;
    background: #ffffff;

    .sharetext {
      font-family: "Cinzel";
      font-style: normal;
      font-weight: 700;
      font-size: 12px;
      color: #808080;
      text-align: left;
      line-height: 40px;
      &>span {
        margin-bottom: 10px;
      }
      //margin-top: -20px;
      //margin-left: 29px;
    }

    .shareContent {
      margin: 0 8px;
      display: flex;

      .shareHover {
        border-radius: 50%;
      }

      .shareHover:hover {
        background-color: orange;
      }

      .shareImg {
        width: 32px;
        height: 32px;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }

  .relatedArticles {
    width: 100%;
    min-height: 660px;
    background: #ffffff;
    padding: 20px;
    &:not(&:first-child) {
      margin-top: 60px;
    }
    // 相关文章标题
    .relatedTitle {
      font-family: "Cinzel";
      font-style: normal;
      font-weight: 700;
      font-size: 12px;
      line-height: 40px;
      text-align: justify;
      color: rgba(0, 0, 0, 0.5);
    }

    .relatedContent {
      width: 100%;
      margin-top: 25px;
      padding:0 !important;
      box-sizing: border-box;

      img {
        width: 5.34rem;
        height: 2.8rem;
      }

      h1 {
        font-family: "Libre Bodoni";
        font-style: normal;
        font-weight: 400;
        font-size: 15Px;
        line-height: 1.5;
        color: #000000;
      }
    }

    .relatedContentOne {
      margin-top: 24px;
    }
  }
}

@media screen and (min-width: 431PX) and (max-width: 1439PX) {
  .maxShare {
    max-width: 1439px !important;
    margin: 0 auto;
  }

  .right {
    position: relative;
  }

  ::v-deep .ContentArticle {
    margin-top: 30px !important;
  }

  .Contentcell {
    margin-top: 40px;
    margin-left: 50px;
    margin-bottom: 10px;
  }

  ::v-deep .ArticleTwo {
    margin: 32px 0;
    text-align: center;

    .ArticleTwo-img {
      width: 100% !important;
      height: 100%;
    }
  }

  .share {
    width: 100% !important;
    background: #ffffff;
    padding: 30px 0;
    margin: 48px 0;

    .sharetext {
      font-family: "Cinzel";
      font-style: normal;
      font-weight: 700;
      font-size: 16Px;
      color: rgba(0, 0, 0, 0.5);
      text-align: center;
      line-height: 40px;
    }

    .shareContent {
      max-width: 1439px;
      margin: 0 8px;
      display: flex;
      justify-content: center;
      padding: 0 80px;

      .shareHover:hover {
        background-color: orange;
      }

      .shareImg {
        width: 24px;
        height: 24px;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }

  .author {
    margin-top: 10px;
  }

  .aug {
    margin-top: 10px;
    margin-bottom: 20px;
  }

  ::v-deep .relatedArticles {
    margin-bottom: 25px;

    .relatedTitle {
      font-family: "Cinzel";
      font-style: normal;
      font-weight: 700;
      font-size: 16Px;
      line-height: 24px;
      text-align: justify;
      color: rgba(0, 0, 0, 0.5);
      margin-bottom: $margin;
    }

    .iprelatedContent {
      display: flex;
      overflow: auto;

      .relatedContent {
        margin-right: 20px;

        h1 {
          font-size: 16px;
        }
      }
    }
  }

  .divider {
    margin-bottom: 70px;
  }
}

@media screen and (min-width: 0PX) and (max-width: 431PX) {
  .header {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  .right {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  .Contentcell {
    margin-left: 40px;
    margin-top: 20px;
  }

  ::v-deep .ContentArticle {
    margin-top: 0px !important;
  }

  .ArticleOne {
    margin-top: 90px;
  }

  ::v-deep .ArticleTwo {
    margin: 32px 0;
    text-align: center;

    .ArticleTwo-img {
      width: 100% !important;
      height: 100%;

      img {
        width: 100%;
        margin: 0 auto;
      }
    }
  }

  .ArticleFour {
    margin: 16px 0;
  }

  .author {
    margin-top: 10px;
  }

  .aug {
    margin-top: 10px;
    margin-bottom: 15px;
  }

  .share {
    width: 100%;
    height: 160px;
    background: #ffffff;
    margin: 48px 0;

    .sharetext {
      font-family: "Cinzel";
      font-style: normal;
      font-weight: 700;
      font-size: 16px;
      line-height: 24px;
      color: rgba(0, 0, 0, 0.5);
      text-align: center;
      line-height: 80px;
    }

    .shareContent {
      margin: 0 8px;
      display: flex;
      justify-content: center;

      .shareHover:hover {
        background-color: orange;
      }

      .shareImg {
        width: 32px;
        height: 32px;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }

  .relatedArticles {
    margin-bottom: 25px;

    .relatedTitle {
      font-family: "Cinzel";
      font-style: normal;
      font-weight: 700;
      font-size: 16px;
      line-height: 24px;
      text-align: justify;
      color: rgba(0, 0, 0, 0.5);
      margin-bottom: $margin;
    }

    .iprelatedContent {
      display: flex;
      overflow: auto;

      .relatedContent {
        margin-right: 20px;

        h1 {
          font-size: 14px;
        }
      }
    }
  }
}
</style>
