<template>
  <div v-show="loaded" class="wrap">
    <div v-if="form.articleTemplate !== 23" class="banner" :style="{backgroundImage: form.bannerImg ? ('url(' + form.bannerImg + ')') : ''}">
      {{ form.columnName }}
    </div>
    <div class="content">
      <Breadcrumb ref="breadcrumb" :data.sync="routes"/>
      <div class="article">
        <div class="left">
          <h2>{{ form.title }}</h2>
          <div class="meta">{{ form.source && form.source + ' | ' }} {{ form.author && form.author + ' | ' }} {{ form.totalBrowsing }} 浏览 | {{ form.createTime }}</div>
          <div class="meta">{{ form.edit && '编辑：' + form.edit }} {{ form.audit && ' | 审核：' + form.audit }} {{ form.label && ' | 标签：' + form.label }}</div>
          <p class="brief">{{ form.summary }}</p>
          <div class="des" v-html="form.mainBody"></div>
          <div v-if="form.fileList && form.fileList.length" class="annex">
            <h6>附件下载</h6>
            <ul class="files">
              <li v-for="(file, i) in form.fileList" :key="i">
                <span class="name">{{ file.fileName }}</span>
                <span class="download" @click="download(file)">下载</span>
              </li>
            </ul>
          </div>
        </div>
        <div class="right">
          <el-tree class="column" ref="column" :data="columns" highlight-current :expand-on-click-node="false" :props="defaultProps" node-key="id" @node-click="columnTo"></el-tree>

          <p class="l-title">热点内容</p>
          <ul class="list">
            <li v-for="(item, i) in hots" :key="i" :title="item.title" @click="toArtice(item)">
              <p class="text">{{ item.title }}</p>
              <span class="date">{{ item.createTime }}</span>
            </li>
          </ul>

          <p class="l-title">最新资讯</p>
          <ul class="list">
            <li v-for="(item, i) in news" :key="i" :title="item.title" @click="toArtice(item)">
              <p class="text">{{ item.title }}</p>
              <span class="date">{{ item.createTime }}</span>
            </li>
          </ul>
        </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'
export default {
  mixins: [mixins],
  data() {
    return {
      id: this.$route.query.articleId,
      loaded: false,
      routes: {},
      columnId: '',
      form: {},
      columnBanner: '',
      columns: [],
      defaultProps: {
        value: 'id',
        label: 'columnName'
      },
      news: [],
      hots: []
    }
  },
  components: {
    Breadcrumb
  },
  watch: {
    '$route'() {
      this.id = this.$route.query.id
      this.getInfo()
    }
  },
  mounted() {
    this.getColumn()
    this.getArticle()
  },
  methods: {
    // 获取文章详情
    getInfo() {
      this.$post(`${this.api.findArticle}?id=${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
          }
        ]
        if (data.isDisable) {
          this.$router.back()
        } else {
          if (data.articleTemplate === 25) { // 会议活动
            this.$router.replace(`/article/activity?articleId=${this.id}&id=${id}&columnName=${columnName || ''}&path=${path || ''}`)
          } else {
            this.form = data
            this.columnId = data.columnId
            data.bannerImg || this.getBanner(this.columns)
            if (this.columnBanner) this.form.bannerImg = this.columnBanner
            this.loaded = true
          }
        }
      }).catch(err => {})

      // 浏览量+1
      this.$post(`${this.api.articlePreview}?contentId=${this.id}`).then(({ data }) => {}).catch(err => {})
    },
    // 获取banner 规则：当前文章有上传的，用上传的，没有上传的栏目有图片，读取上级栏目的（上级没有读上上级，以此类推）都没有的读取默认的
    getBanner(data) {
      this.columnBanner = ''
      for (const e of data) {
        if (e.columnBanner) this.columnBanner = e.columnBanner
        if (e.id == this.columnId) {
          break
        } else {
          this.getBanner(e.children)
        }
      }
    },
    // 资讯
    getColumn() {
      this.$post(this.api.listWithTree, {
        siteId: this.$route.query.siteId || this.$store.state.content.site,
        columnName: '',
        templateId: '',
        typeId	: '',
        isSort: 1
      }).then(({ data }) => {
        this.columns = data
        this.getInfo()
      }).catch(err => {})
      
      this.$post(`${this.api.hotContent}?siteId=${this.$route.query.siteId || this.$store.state.content.site}`).then(({ data }) => {
        this.hots = data
      }).catch(err => {})
    },
    // 最新资讯
    getArticle() {
      this.$post(this.api.newlyPublishedArticles, {
        pageNum: 1,
        pageSize: 5,
        siteId: this.$route.query.siteId || this.$store.state.content.site
      }).then(({ data }) => {
        this.news = data.records
      }).catch(res => {})
    },
    // 下载附件
    download(e) {
      Util.downloadFile(e.fileName, e.filePath)
    },
  }
};
</script>

<style lang="scss" scoped>
@import "./editor.css";
.content {
  width: 1400px;
}
.banner {
  height: 280px;
  padding: 123px 0 0 243px;
  font-size: 48px;
  color: #fff;
  background: url(../../assets/images/intro-bg.png) 0 0/100% 100% no-repeat;
}
.article {
  display: flex;
  justify-content: space-between;
  .left {
    width: 66%;
  }
  h2 {
    font-size: 36px;
    color: #1C1C1C;
  }
  .meta {
    margin: 20px 0;
    font-size: 14px;
    color: #9B9B9B;
  }
  .brief {
    padding-bottom: 30px;
    margin-bottom: 30px;
    font-size: 24px;
    line-height: 40px;
    color: #606060;
    border-bottom: 1px solid #D8D8D8;
  }
  /deep/.des {
    p {
      margin: 1em 0;
    }
    img {
      max-width: 100%;
    }
  }
  .annex {
    margin-top: 30px;
    h6 {
      padding-left: 8px;
      margin-bottom: 20px;
      font-size: 16px;
      line-height: 1;
      color: #333;
      border-left: 2px solid #1583FF;
    }
    li {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
    }
    .name {
      margin-right: 8px;
      font-size: 14px;
    }
    .download {
      color: #1583FF;
      cursor: pointer;
    }
  }
  .right {
    width: 20%;
  }
  .column {
    width: 100%;
    margin-bottom: 25px;
  }
  /deep/.el-tree-node__content {
    height: 44px;
    background-color: #E5EDF8;
    border-bottom: 2px solid #fff;
  }
  /deep/.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
    color: #fff;
    background-color: #0f5698;
  }
  .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: 1420px) {
  .content {
    width: 98%;
  }
}
</style>