<template>
  <div class="search-tag-message">
    <p class="tips">交小通猜你想查：</p>
    <ul class="search-tag-list">
      <template v-for="(item,index) in dataList">
        <li class="search-tag-item" :key="item.id" @click="onClick(item)">
          <span class="num">{{index+1}}:</span>
          <span class="text">{{item.title}}</span>
        </li>
      </template>
    </ul>
    <div class="more" @click="toggleMore" v-if="hasMore">
      <template v-if="!showMore">
        <span>更多</span>
        <van-icon name="arrow-down"></van-icon>
      </template>
      <template v-else>
        <span>收起</span>
        <van-icon name="arrow-up"></van-icon>
      </template>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SearchTagMessage',
  props: {
    courseId: { type: String },
    isTest: { type: Boolean, default: false },
    data: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return { showMore: false, maxCount: 7 }
  },
  computed: {
    hasMore() {
      return this.data.length > this.maxCount
    },
    dataList() {
      if (this.hasMore && !this.showMore) {
        return this.data.filter((item, index) => index < this.maxCount)
      }
      return this.data
    },
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  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)
      }
    },
    toggleMore() {
      this.showMore = !this.showMore
    }
  }
}
</script>

<style lang="scss" scoped>
.search-tag-message {
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
}
.search-tag-item {
  margin: 10px 0;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  .num {
    display: inline-block;
    min-width: 24px;
  }
  .text {
    border-bottom: 1px solid #2b7ce9;
    cursor: pointer;
  }
}
.more {
  padding-top: 10px;
  border-top: 1px solid #eee;
  font-size: 13px;
  color: #222;
  text-align: center;
  cursor: pointer;
}
</style>
