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

<script>
import * as api from '../api'
export default {
  name: 'CourseList',
  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.getSearchCourseList(this.requestParams).then(response => {
        this.loaded = true
        this.list = response
      })
    },
    // 刷新
    refresh() {
      this.getList()
    },
    onClick(data) {
      if (this.isWeapp) {
        const url = data.free
          ? `/pages/course/free?id=${data.id}`
          : `/pages/web/index?src=${window.location.origin}/course/learn/${data.id}`
        wx.miniProgram.navigateTo({ url })
      } else {
        this.$router.push({ name: 'courseLearnItem', params: { id: data.id } })
      }
    }
  },
  beforeMount() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
.main-list li {
  padding: 0.3rem 0;
  font-size: 0.3rem;
  color: #222;
  border-bottom: 1px solid #f1f1f1;
  &:last-child {
    border-bottom: 0;
  }
}
</style>
