<template>
  <div class="wrap">
    <div class="banner">
      <img width="100%" height="280" :src="info.columnBanner" alt="">
      <p class="text">{{ info.columnName }}</p>
    </div>
    <div class="column-wrap">
      <div class="article">
        <div class="left">
          <!-- 文章：侧边栏+搜索+新闻列表 -->
          <div v-if="info.listStyleId === 10" class="forms">
            <div class="item">
              <span class="label">所属分类：</span>
              <el-select v-model="form.classificationId" @change="filter">
                <el-option label="不限" value=""></el-option>
                <el-option
                  v-for="item in classifications"
                  :key="item.id"
                  :label="item.classificationName"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
            <div class="item">
              <span class="label">主题：</span>
              <el-select v-model="lableId" multiple @change="filter">
                <el-option
                  v-for="item in labels"
                  :key="item.id"
                  :label="item.labelName"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
            <div class="search">
              <input type="text" placeholder="请输入搜索内容" v-model="form.title">
              <i class="icon">
                <img src="@/assets/images/search-white.png" alt="">
              </i>
            </div>
          </div>
          <!-- 会议活动：筛选+会议活动列表 -->
          <div v-if="info.listStyleId === 15" class="forms">
            <div class="item">
              <span class="label">类型：</span>
              <el-select v-model="form.classificationId">
                <el-option label="不限" value=""></el-option>
                <el-option
                  v-for="item in classifications"
                  :key="item.id"
                  :label="item.labelName"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
          </div> 
          <div class="contents">
            <ul v-if="columns.length > 2 && !isFilter && showNav" class="columns">
              <li v-if="all.columnName" class="all" :class="{active: curColumn ===  0 }" @click="toArticle(0, 1)">{{ all.columnName }}</li>
              <template v-for="(item, i) in columns">
                <li v-if="i" :key="i" :class="{active: i === curColumn}" @click="toArticle(i)">{{ item.columnName }}</li>
              </template>
            </ul>
            <div style="width: 1113px">
              <ul class="articles">
                <li v-for="(item, i) in articles" :key="i" @click="toArtice(item)">
                  <div class="texts">
                    <p v-if="info.listStyleId === 10 && item.classificationName" class="type">{{ item.classificationName }}</p>
                    <h6>{{ item.title }}</h6>
                    <template v-if="info.listStyleId === 11">
                      <span class="meta">{{ item.updateTime }}</span>
                      <div class="des" v-html="item.mainBody"></div>
                    </template>
                    <span v-if="info.listStyleId === 10" class="meta">{{ item.updateTime }} {{ item.labelName && ' | ' + item.labelName }}</span>
                    <template v-if="info.listStyleId === 15 || info.listStyleId === 16">
                      <div v-if="item.keynoteSpeaker" class="meta m-b-10"><i class="el-icon-user"></i> {{ item.keynoteSpeaker }}</div>
                      <div v-if="item.activityStartTime" class="meta m-b-10"><i class="el-icon-alarm-clock"></i> {{ item.activityStartTime + '~' + item.activityEndTime }}</div>
                      <div v-if="item.offlineLocation" class="meta"><i class="el-icon-location-outline"></i> {{ item.offlineLocation }}</div>
                    </template>
                  </div>
                  <img class="pic" :src="item.titleImg" alt="">
                </li>
              </ul>
              <div v-if="articles.length" class="pagination">
                <el-pagination background @current-change="currentChange" :current-page="page" layout="total, prev, pager, next" :total="total"></el-pagination>
              </div>
            </div>
          </div>
        </div>
        <div class="right">
          <el-tree class="column" ref="column" :data="infoColumns" highlight-current :expand-on-click-node="false" :props="defaultProps" node-key="id" ></el-tree>

          <p class="l-title">热点内容</p>
          <ul class="list">
            <li>
              <p class="text">喜报 | 深圳粒子研究院荣获全国科研…</p>
              <span class="date">2022-08-09</span>
            </li>
            <li>
              <p class="text">喜报 | 深圳粒子研究院荣获全国科研…</p>
              <span class="date">2022-08-09</span>
            </li>
            <li>
              <p class="text">喜报 | 深圳粒子研究院荣获全国科研…</p>
              <span class="date">2022-08-09</span>
            </li>
          </ul>

          <p class="l-title">最新资讯</p>
          <ul class="list">
            <li v-for="(item, i) in news" :key="i">
              <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'
export default {
  data() {
    return {
      id: this.$route.query.id,
      site: this.$store.state.content.site,
      columnId: '',
      info: {},
      searchTimer: null,
      lableId: [],
      form: {
        classificationId: '',
        title: ''
      },
      classifications: [],
      labels: [],
      columns: [],
      all: null,
      infoColumns: [],
      curColumn: 0,
      showNav: false,
      showNavIds: [10, 11, 12, 16], // 显示侧导航的模板id
      isFilter: false,
      page: 1,
      pageSize: 10,
      total: 0,
      articles: [],
      defaultProps: {
        value: 'id',
        label: 'columnName'
      },
      news: []
    }
  },
  watch: {
    '$route'() {
      this.id = this.$route.query.id
      this.init()
    },
    'form.title': function(val) {
        clearTimeout(this.searchTimer)
        this.searchTimer = setTimeout(() => {
          this.filter()
        }, 500)
      }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getLeftColumn()
      this.getInfo()
      this.getColumn()
      this.getNews()
      this.getLabel()
    },
    // 获取栏目详情
    getInfo() {
      this.$post(`${this.api.findColumn}?id=${this.id}`).then(({ data }) => {
        this.info = data
        this.showNav = this.showNavIds.includes(data.listStyleId)
        this.getClassification()
      }).catch(res => {})
    },
    // 获取所属分类
    getClassification() {
      this.$post(`${this.api.getTheFullArticleByColumn}?id=${this.id}`).then(({ data }) => {
        this.classifications = data
      }).catch(err => {})
    },
    // 获取标签
    getLabel() {
      this.$post(`${this.api.queryLabel}?siteId=${this.$route.query.siteId || this.site}`).then(({ data }) => {
        this.labels = data
      }).catch(err => {})
    },
    // 左边栏目
    getLeftColumn() {
      this.$post(`${this.api.getsTheSubColumn}?id=${this.id}`).then(({ data }) => {
        if (data.length > 1 && !data.find(e => e.listStyleId !== data[0].listStyleId)) {
          this.all = data[0]
          this.getArticle(data.map(e => e.id))
        } else {
          this.all = null
          this.getArticle([data[data.length > 1 ? 1 : 0].id])
          data = data.slice(1)
        }
        this.curColumn = 0
        this.columns = data
        console.log("🚀 ~ file: index.vue ~ line 212 ~ this.$post ~ columns", columns)
      }).catch(err => {})
    },
    // 右边资讯
    getColumn() {
      this.$post(this.api.listWithTreeMenuVisible, {
        siteId: this.$route.query.siteId || this.$store.state.content.site,
        columnName: '',
        templateId: '',
        typeId	: '',
        isSort: 1
      }).then(({ data }) => {
        this.infoColumns = data
      }).catch(err => {})
    },
    // 最新资讯
    getNews() {
      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 => {})
    },
    // 点击栏目回调
    toArticle(i, all) {
      this.curColumn = i
      this.page = 1
      this.getArticle(all ? this.columns.map(e => e.id) : [this.columns[i].id])
    },
    // 查询文章列表
    getArticle(columnIds) {
      this.$post(this.api.newlyPublishedArticles, {
        siteId: this.$route.query.siteId || this.site,
        columnIds,
        pageNum: this.page,
        pageSize: this.pageSize,
        labelId: this.lableId.join(),
        ...this.form
      }).then(({ data }) => {
        const list = data.records
        list.map(e => {
          e.mainBody = e.mainBody.replace(/<img.*?>/g , '') // 去掉正文里的图片
        })
        this.articles = list
        this.total = +data.total
      }).catch(res => {})
    },
    // 筛选
    filter() {
      this.page = 1
      this.isFilter = !!(this.form.classificationId || this.form.title || this.lableId.length)
      const id = (this.isFilter || this.all) ?
        this.columns.map(e => e.id) :
        [this.columns[0].id]
      this.curColumn = 0
      this.getArticle(id)
    },
    currentChange(val) {
      this.page = val
      // this.getData()
    },
    // 跳转文章页面
    toArtice(item) {
      this.$router.push(`/article?id=${item.id}&site=${this.$route.query.siteId || this.site}`)
    }
  }
};
</script>

<style lang="scss" scoped>
.banner {
  position: relative;
  height: 280px;
  font-size: 48px;
  color: #fff;
  .text {
    position: absolute;
    top: 123px;
    left: 243px;
  }
}
.column-wrap {
  padding: 40px 0;
  background: #F9FAFA url(../../assets/images/column-bg.png) no-repeat;
}
.article {
  display: flex;
  justify-content: space-between;
  width: 1666px;
  margin: 0 auto;
  .left {
    width: 1295px;
  }
  .forms {
    display: flex;
    align-items: center;
    padding: 30px;
    margin-bottom: 20px;
    background-color: #fff;
    .item {
      display: inline-flex;
      align-items: center;
      margin-right: 30px;
    }
    .label {
      font-size: 16px;
      color: #333;
    }
    /deep/.el-input__inner {
      width: 238px;
      height: 48px;
      line-height: 48px;
      border: 0;
      background: #F7F7F7;
    }
    .search {
      display: inline-flex;
      input {
        width: 443px;
        height: 48px;
        padding: 0 10px;
        font-size: 14px;
        color: #333;
        background: #F7F7F7;
        border: 0;
        border-top-left-radius: 6px;
        border-bottom-left-radius: 6px;
        &:focus {
          outline: none;
        }
      }
    }
    .icon {
      display: inline-flex;
      justify-content: center;
      align-items: center;
      width: 62px;
      height: 48px;
      background: #1583FF;
      border-radius: 0px 6px 6px 0px;
      cursor: pointer;
    }
  }
  .contents {
    display: flex;
  }
  .columns {
    width: 174px;
    margin-right: 12px;
    overflow: auto;
    li {
      display: flex;
      align-items: center;
      height: 48px;
      padding-left: 40px;
      margin-bottom: 1px;
      font-size: 16px;
      color: #666;
      background-color: #fff;
      border-right: 2px solid transparent;
      cursor: pointer;
      &:before {
        content: '';
        width: 2px;
        height: 2px;
        margin-right: 5px;
        background-color: #666;
        border-radius: 50%;
      }
      &:hover {
        color: #0c84eb;
      }
      &.active {
        border-right-color: #083A93;
      }
      &.all {
        padding-left: 24px;
      }
    }
  }
  .articles {
    li {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      background-color: #fff;
      cursor: pointer;
    }
    .texts {
      max-width: 620px;
      padding-left: 30px;
    }
    .type {
      margin-bottom: 15px;
      font-size: 18px;
      color: #333;
    }
    h6 {
      margin-bottom: 15px;
      font-size: 24px;
      color: #000;
      line-height: 38px;
      @include mul-ellipsis(2);
    }
    .meta {
      font-size: 16px;
      color: #666;
    }
    .des {
      margin-top: 15px;
      @include mul-ellipsis(2);
    }
    img {
      width: 417px;
      height: 244px;
    }
  }
  .right {
    width: 324px;
  }
  .column {
    width: 100%;
    margin-bottom: 25px;
  }
  /deep/.el-tree-node__content {
    height: 60px;
    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;
    }
  }
}
</style>