<template>
  <div class="course-tag-message">
    <div class="course-tag-message-hd">
      <div class="course-tag-message__title">{{data.name}}</div>
      <div class="course-tag-message__more" @click="viewMore" v-if="hasMore">
        <span>更多</span>
        <van-icon name="arrow" />
      </div>
    </div>
    <div class="course-tag-message-bd" v-if="dataList && dataList.length">
      <ul class="message-tag-list" ref="content">
        <template v-for="item in dataList">
          <li class="course-tag-item" :key="item.id" @click="onClick(item)">
            <p>{{item.title}}</p>
          </li>
        </template>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CourseTagMessage',
  props: {
    courseId: { type: String },
    isTest: { type: Boolean, default: false },
    data: { type: Object }
  },
  data() {
    return {
      maxCount: 8
    }
  },
  computed: {
    hasMore() {
      return this.data.tag ? this.data.tag.length > this.maxCount : false
    },
    isWeapp() {
      return this.$store.state.isWeapp
    },
    // 最多现实8条
    dataList() {
      return this.data.tag ? this.data.tag.slice(0, this.maxCount) : []
    }
  },
  methods: {
    onClick(data) {
      if (this.isTest) {
        // 知识点测试
        const path = `/exam/answer?type=2&tag_id=${data.id}&papersUrl=zy/v2/examination/course-papers`
        if (this.isWeapp) {
          const src = encodeURIComponent(`${window.location.origin}${path}`)
          wx.miniProgram.navigateTo({ url: `/pages/web/index?src=${src}` })
        } else {
          this.$router.push({ path })
        }
      } else {
        this.$emit('change', data)
      }
    },
    viewMore() {
      const path = `/course/learn/${this.courseId}/tag/${this.data.id}?is_test=${this.isTest ? '1' : '0'}`
      if (this.isWeapp) {
        const src = encodeURIComponent(`${window.location.origin}${path}`)
        wx.miniProgram.navigateTo({
          url: `/pages/web/index?src=${src}`
        })
      } else {
        this.$router.push({ path })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.course-tag-message {
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
}
.course-tag-message-hd {
  display: flex;
}
.course-tag-message__title {
  flex: 1;
  font-size: 15px;
  font-weight: bold;
  color: #222;
}
.course-tag-message__more {
  display: flex;
  align-items: center;
  margin-left: 20px;
  font-size: 13px;
  color: #f47885;
}
.message-tag-list {
  margin-top: 20px;
}
.course-tag-item {
  display: inline-block;
  max-width: 100%;
  margin: 0 10px 10px 0;
  padding: 0 15px;
  background: #f47885;
  border-radius: 12px;
  cursor: pointer;
  p {
    font-size: 13px;
    color: #fff;
    line-height: 24px;
  }
}
.more {
  padding-top: 10px;
  border-top: 1px solid #eee;
  font-size: 13px;
  color: #222;
  text-align: center;
  cursor: pointer;
}
</style>
