<template>
  <div class="article-box clearfix" @click="toDetail">
    <div class="img fl"><img :src="baseUrl+articleInfo.coverUrl" mode="widthFix"></div>
    <div class="info-box">
      <div>
        <div class="title">{{articleInfo.title}}</div>
        <div v-if="isBrief && articleInfo.articleBrief" class="brief">{{articleInfo.articleBrief}}</div>
        <div v-if="!isBrief && articleInfo.doctor" class="label">
          <span>{{articleInfo.doctor.dept}}</span>
          <span>{{articleInfo.doctor.hospital}}</span>
        </div>
      </div>
      <div class="count-box clearfix">
        <!-- 收藏 -->
        <div class="count-item collect fr" :class="{'active': articleInfo.isCollect}" @click.stop="collect">
          <div class="text">{{articleInfo.collectNum}}人</div>
        </div>
        <!-- 浏览 -->
        <div class="count-item watch fr">
          <div class="text">{{articleInfo.playNum || 0}}人</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { baseUrl } from '@/utils/http'
import { articleCollect } from '@/utils/api'

export default {
  props: ['articleInfo', 'redirect', 'isBrief'],
  data () {
    return {
      baseUrl: baseUrl
    }
  },
  methods: {
    // 收藏
    async collect () {
      let {id, isCollect, collectNum} = this.articleInfo

      await articleCollect(id)
      this.articleInfo.collectNum = isCollect ? collectNum - 1 : collectNum + 1
      this.articleInfo.isCollect = !isCollect
    },
    // 跳转详情页
    toDetail () {
      let nextType = 'navigateTo'

      if (this.redirect) {
        nextType = 'redirectTo'
      }
      wx[nextType]({
        url: '/pages/teachClassDetail/main?id=' + this.articleInfo.id,
        events: {
          setArticleInfo: function (data) {
            console.log(data)
          }
        },
        success: res => {
          res.eventChannel.emit('setArticleInfo', this.articleInfo)
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
@import '~@/assets/less/common.less';
.article-box{
  margin: 0 @spacing @spacing;
  box-shadow:0px 4px 14px 0px rgba(0,0,0,0.1);
  border-radius:10px;
  overflow: hidden;
  .img{
    padding: 8px;
    width: 100px;
    height: 100px;
    box-sizing: border-box;
  }
  .info-box{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 8px 10px;
    height: 100px;
    overflow: hidden;
    box-sizing: border-box;
    .title{
      max-height: 44px;
      font-size: 16px;
      font-weight: 500;
      color: #333;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
    }
    .label{
      margin-top: 6px;
      span{
        display: inline-block;
        margin-right: 8px;
        padding: 0 5px;
        line-height: 16px;
        border-radius: 8px;
        font-size: 10px;
        color: @themeColor;
        background: #FFF2F5;
      }
    }
    .brief{
      height: 22px;
      line-height: 22px;
      font-size: 12px;
      color: #999999;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
    }
    .count-box{
      .count-box();
    }
  }
}
</style>
