<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}}{{item.free? '（免费）': ''}}</p>
          </li>
        </template>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'CourseTagMessage',
  props: {
    courseId: { type: String },
    isTest: { type: Boolean, default: false },
    data: { type: Object }
  },
  data() {
    return {
      maxCount: 8
    }
  },
  computed: {
    ...mapState(['isWeapp', 'isAndroid', 'isIos', 'isVip', 'isLogin']),
    hasMore() {
      return this.data.tag ? this.data.tag.length > this.maxCount : false
    },
    // 最多显示8条
    dataList() {
      let list = this.data.tag || []
      if (!this.isVip) {
        // 免费的在前
        list = [...list.filter(item => item.free), ...list.filter(item => !item.free)]
      }
      return list.slice(0, this.maxCount)
    },
    moreText() {
      if (this.isVip) {
        return '更多'
      }
      if (this.isIos) {
        return '更多请开通'
      }
      return '更多请购买'
    }
  },
  methods: {
    onClick(data) {
      // 未登录
      if (!data.free && !this.isLogin) {
        this.isWeapp ? wx.miniProgram.navigateTo({ url: '/pages/login/index' }) : this.$router.push({ name: 'login' })
        return
      }
      // 未开通
      if (!data.free && !this.isVip) {
        this.isWeapp
          ? wx.miniProgram.navigateTo({ url: `/pages/web/index?src=${window.location.origin}/pay` })
          : this.$router.push({ name: 'pay' })
        return
      }
      if (this.isTest) {
        // 知识点测试
        const path = `/exam/courseNodeExam?tag_id=${data.id}`
        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>
