<template>
  <div class="wrap">
    <div class="content">
      <Breadcrumb ref="breadcrumb"
                  :data.sync="routes" />
      <div class="article">
        <div class="left">
          <h2>{{ form.title }}</h2>
          <template v-if="form.author">
            <p class="name">{{ $t('column.author') }}</p>
            <p class="val">{{ form.author }}</p>
          </template>
          <template v-if="form.quote">
            <p class="name">{{ $t('column.quote') }}</p>
            <p class="val">{{ form.quote }}</p>
          </template>
          <p class="name">{{ $t('column.introduce') }}</p>
          <div class="flex">
            <div class="des"
                 v-html="form.mainBody"></div>
            <!-- <img v-if="form.titleImg" width="222" height="222" :src="form.titleImg" alt=""> -->
          </div>
        </div>
        <div class="right">
          <img class="pic"
               src="@/assets/images/publish3.png"
               alt="">
          <div class="texts">
            <template v-if="form.periodicalName">
              <p class="name">{{ $t('column.publicationName') }}</p>
              <p class="val">{{ form.periodicalName }}</p>
            </template>
            <p class="name">{{ $t('column.yearOfPublication') }}</p>
            <p class="val">{{ form.publicationYear }}{{ $t('column.year') }}</p>
            <div class="flex">
              <div v-if="form.reel"
                   class="m-r-20">
                <p class="name">{{ $t('column.rollUp') }}</p>
                <p class="val">{{ form.reel }}</p>
              </div>
              <div v-if="form.documentNumber">
                <p class="name">{{ $t('column.documentNumber') }}</p>
                <p class="val">{{ form.documentNumber }}</p>
              </div>
            </div>
            <template v-if="form.publicationTypeId">
              <p class="name">{{ $t('column.publicationType') }}</p>
              <p class="val">{{ form.publicationTypeId && publicationTypes.find(e => e.id == form.publicationTypeId).name }}</p>
            </template>
            <template v-if="form.doi">
              <p class="name">DOI</p>
              <p class="val">{{ form.doi }}</p>
            </template>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Setting from '@/setting'
import Util from '@/libs/util'
import Breadcrumb from '@/components/breadcrumb'
import mixins from '@/mixins/article'
import ColumnConst from '@/const/column'
export default {
  mixins: [mixins],
  data () {
    return {
      id: this.$route.query.articleId,
      publicationTypes: ColumnConst.publicationTypes,
      routes: [],
      columnId: '',
      form: {},
      columnBanner: '',
      news: [],
      hots: []
    }
  },
  components: {
    Breadcrumb
  },
  watch: {
    '$route' () {
      this.id = this.$route.query.id
      this.getInfo()
    }
  },
  mounted () {
    this.getInfo()
    this.getArticle()
  },
  methods: {
    // 获取文章详情
    getInfo () {
      this.$post(this.api.findArticle, Util.rsa(this.id)).then(({ data }) => {
        // 设置面包屑
        const { columnName, path, id } = this.$route.query
        this.routes = [
          {
            name: columnName || data.columnName,
            path: path ? '/' + path : '',
            query: {
              id: id || data.columnId
            }
          },
          {
            name: data.title
          }
        ]
        this.form = data
        this.columnId = data.columnId
        // 如果没上传banner
        if (!data.bannerImg) {
          this.getBanner(this.columns)
          this.form.bannerImg = this.columnBanner || require('@/assets/images/article-banner.png')
        }
      }).catch(err => { })

      // 浏览量+1
      // this.$post(`${this.api.articlePreview}?contentId=${this.id}`).then(({ data }) => {}).catch(err => {})
    },
    // 获取banner 规则：当前文章有上传的，用上传的，没有上传的话，如果栏目有图片，读取栏目的（上级没有读上上级，以此类推）都没有的读取默认的
    getBanner (data) {
      for (const e of data) {
        if (e.columnBanner) this.columnBanner = e.columnBanner
        if (e.id == this.columnId) {
          break
        } else {
          this.getBanner(e.children)
        }
      }
    },
    // 热点内容和最新资讯
    getArticle () {
      this.$post(this.api.hotContent, Util.rsa(this.$route.query.siteId || this.$store.state.content.site)).then(({ data }) => {
        this.hots = Util.removeTag(data)
      }).catch(err => { })

      this.$post(this.api.newlyPublishedArticles, Util.rsa({
        pageNum: 1,
        pageSize: 5,
        siteId: this.$route.query.siteId || this.$store.state.content.site
      })).then(({ data }) => {
        this.news = Util.removeTag(data.records)
      }).catch(res => { })
    },
  }
};
</script>

<style lang="scss" scoped>
.wrap {
    background: url(../../assets/images/publish1.png) no-repeat;
}
.content {
    width: 1084px;
    padding-top: 25px;
    background: url(../../assets/images/publish2.png) bottom right/auto no-repeat;
}
.breadcrumb {
    margin-bottom: 25px;
}
.article {
    display: flex;
    justify-content: space-between;
    .left {
        width: 846px;
        padding: 30px;
        margin-right: 16px;
        background: #fff;
        .name {
            font-size: 24px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333333;
            line-height: 33px;
        }
        .val {
            margin-bottom: 38px;
            font-size: 16px;
            color: #333;
            line-height: 30px;
        }
        .des {
            width: 100%;
            // margin-right: 49px;
        }
    }
    h2 {
        padding-bottom: 27px;
        margin-bottom: 40px;
        font-size: 36px;
        font-family: PingFangSC-Medium, PingFang SC;
        color: #333;
        line-height: 50px;
        border-bottom: 1px solid #ddd;
    }
    .right {
        width: 222px;
        background: #fff;
        .texts {
            padding: 15px;
        }
        .name {
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            line-height: 20px;
            color: #666666;
        }
        .val {
            margin: 5px 0 36px;
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333333;
            line-height: 20px;
        }
    }
    .list {
        margin-bottom: 20px;
        li {
            padding: 16px 0;
            border-bottom: 1px solid #d8d8d8;
        }
        .text {
            margin-bottom: 5px;
            font-size: 14px;
            cursor: pointer;
            &:hover {
                color: $main-color;
            }
        }
        .date {
            font-size: 12px;
            color: #999;
        }
    }
}
@media (max-width: 1084px) {
    .content {
        width: 95%;
    }
    .article {
        flex-direction: column;
        .left,
        .right {
            width: 100%;
        }
        .right {
            margin: 20px 0 0;
        }
    }
}
</style>