<template>
  <div class="main-list" v-show="loaded">
    <template v-if="list.length">
      <ul>
        <li v-for="item in list" :key="item.id" @click="onClick(item)">
          <div class="pic">
            <img :src="item.url" />
            <div class="cover"></div>
          </div>
          <div class="text">{{item.chapter_name}}</div>
        </li>
      </ul>
    </template>
    <template v-else>
      <slot name="empty">
        <div class="empty">暂时没有内容哦，换个搜索词试试吧～</div>
      </slot>
    </template>
  </div>
</template>

<script>
import * as api from '@/api/course.js'
export default {
  name: 'VideoList',
  props: {
    params: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      loaded: false,
      list: [],
      requestParams: {}
    }
  },
  watch: {
    params: {
      immediate: true,
      handler(value) {
        this.requestParams = Object.assign({}, this.requestParams, value)
      }
    }
  },
  computed: {
    isWeapp() {
      return this.$store.state.isWeapp
    }
  },
  methods: {
    getList() {
      this.loaded = false
      api.getSearchCourseVideoList(this.requestParams).then(response => {
        this.loaded = true
        this.list = response
      })
    },
    // 刷新
    refresh() {
      this.getList()
    },
    onClick(data) {
      if (this.isWeapp) {
        wx.miniProgram.navigateTo({
          url: `/pages/course/item?id=${data.course_id}&chapter_id=${data.id}`
        })
      } else {
        window.alert('请在微信小程序中打开')
      }
    }
  },
  beforeMount() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
.main-list ul {
  margin-top: 0.58rem;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.main-list li {
  width: 3rem;
  margin-bottom: 0.4rem;
  .pic {
    position: relative;
    width: 3rem;
    height: 1.7rem;
    border-radius: 6px;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .cover {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background: rgba(1, 1, 1, 0.2) url(../../assets/images/icon_play.png)
      no-repeat center center;
    background-size: 0.56rem 0.7rem;
    z-index: 10;
  }
  .text {
    margin: 0.1rem 0;
    font-size: 0.3rem;
    color: #222;
    line-height: 0.4rem;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
}
</style>
