<template>
  <div>
    <template v-for='(item, index) in discussList'>
      <div v-bind:key='index' class='item-list' @click='goDiscussDetail' :data-id='item.id' :data-sid='item.sid' :data-cid='item.cid' :data-index='index'>
        <div class='user'>
          <template v-if="item.user.url">
            <img class='img' :src='item.user.url' />
          </template>
          <template v-else>
            <img class='img' src='../../assets/images/person-default.jpg' />
          </template>
          <div class='right'>
            <div class='name'>{{item.user.name}}</div>
            <div class='time'>{{item.user.time}}</div>
          </div>
        </div>
        <div class='title'>{{item.title}}</div>
        <div :class='["text"]' v-html="item.text"></div><div :class='["ellipsis", (item.isShow ? "on" : "")]'>....</div>
        <div class='result'>{{item.askCnt}} {{ $t('pages.learn.discussion.answers') }}<div style='display: inline-block; width: 20px;'></div>{{item.TouCnt}} {{ $t('pages.learn.discussion.votes') }}</div>
        <!-- <div class='course-name'>{{item.courseName}}</div> -->
      </div>
    </template>
    <template v-if='!discussList.length'>
      <div class='no-data'>暂无相关讨论</div>
    </template>
  </div>
</template>

<script>
import api from '../api/index'

export default {
  name: Discuss,
  components: { },
  data () {
    return {
      tabs: [{
        title: this.$t('pages.learn.discussion.tap1'),
        isShow: true,
        path: '/my'
      }, {
        title: this.$t('pages.learn.discussion.tap2'),
        isShow: false,
        path: '/involved'
      }],
      discussList: [
        // {
        //   user: {
        //     url: '',
        //     name: '用户名000',
        //     time: '2018-2-12 15:28:47'
        //   },
        //   title: '这是一个一句话问题这是一个一句话问题这是一个一句话问题标题这是一个一句话问题',
        //   text: '<p>王家有三兄弟甲、乙、丙，丙幼年时送给胡某作养子，丙结婚时，胡某为其盖了新房，后因失火致使该房屋被烧毁。丙的生父母就将自己致使该房屋被烧毁。丙的生父母就将自己致使该房屋被烧毁。丙的生父母就将自己致使该房屋被烧毁。丙的生父母就将自己致使该房屋被烧毁。丙的生父母就将自己致使该房屋被烧毁。丙的生父母就将自己</p><p>发送到；发是否第三方sffdfdsfds</p>',
        //   askCnt: 20,
        //   TouCnt: 100,
        //   courseName: '在线学习课程',
        //   isShow: false
        // }
      ],
      param: {
        limit: 10, // 默认每页显示10条
        offset: 0 // 偏移量（未考虑传输时，产生新问题情况）
      }
    }
  },
  mounted () {
    this.loadAjax()
    window.addEventListener('resize', this.resizeRoot.bind(this), false)
  },
  destroyed () {
    window.removeEventListener('resize', this.resizeRoot.bind(this), false)
  },
  updated () {
    this.resizeRoot()
  },
  methods: {
    resizeRoot () {
      const elems = document.querySelectorAll('.ellipsis')
      let fs = getComputedStyle(window.document.documentElement)['font-size'].replace('px', '') // eslint-disable-line
      elems.forEach((_, i) => {
        _.previousSibling.style.height = 'auto'
        const height = _.previousSibling.offsetHeight
        const realHeight = fs * 0.18 * 1.5 * 2
        if (height > realHeight) {
          this.discussList[i].isShow = true
          _.previousSibling.style.height = realHeight + 'px'
        } else {
          this.discussList[i].isShow = false
        }
      })
    },
    loadAjax () {
      let pathUrl = ''
      for (let i = 0; i < this.tabs.length; i++) {
        if (this.tabs[i].isShow) {
          pathUrl = this.tabs[i].path
        }
      }
      const loading = this.$loading({ lock: true, text: '', spinner: '', background: 'rgba(255, 255, 255, 0.9)' })
      cAction.Discuss.getDiscussList(pathUrl, this.param).then(json => {
        this.discussList = json
      }).catch(e => { this.$message.error(e.message) }).finally(() => { loading.close() })
    },
    /**
     * 切换 - tab
     */
    tabSelect (e) {
      const index = e.currentTarget.dataset.index
      const json = this.tabs
      for (let i = 0; i < json.length; i++) {
        json[i].isShow = false
      }
      json[index].isShow = true
      this.tabs = json
      this.param.offset = 0
      this.loadAjax()
    },
    /**
     * 跳转到对应 问题详情页
     */
    goDiscussDetail (e) {
      const qid = e.currentTarget.dataset.id
      const sid = e.currentTarget.dataset.sid
      const cid = e.currentTarget.dataset.cid
      this.$router.push({ path: `/app/learn/discuss-detail/${sid}/${cid}/${qid}` })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>