<template>
  <div class="course-item">
    <div class="course-item-top">
      <img class="course-item-pic" :src="data.photo" v-if="data.photo" />
      <div class="course-item-content">
        <div class="course-item__title">{{ data.title }}</div>
        <div class="course-item__tools">
          <div class="course-item__text course-item__text__freevideo">{{ data.free_video_num }}个免费视频</div>
          <div class="course-item__text course-item__text__video">{{ data.video_num }}节视频课</div>
          <div class="course-item__text course-item__text__course">含{{ data.course_num }}节课</div>
        </div>
      </div>
    </div>
    <div class="course-item-bottom">
      <div class="course-child" v-for="item in data.child" :key="item.id" @click="handleClick(item)">
        <div class="name">{{ item.course_name }}</div>
        <div class="progress" v-if="showProgress">{{ item.video_progress | progressText }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CourseListItem',
  props: {
    data: { type: Object, required: true },
    showProgress: { type: Boolean, default: false }
  },
  data() {
    return {}
  },
  filters: {
    progressText(value) {
      value = parseInt(value)
      if (value === 0) {
        return '未开始'
      }
      if (value === 100) {
        return '已学完'
      }
      return `已学${value}%`
    }
  },
  methods: {
    handleClick(data) {
      this.$router.push({ name: 'courseLearnItem', params: { id: data.id } })
    }
  }
}
</script>

<style lang="scss" scoped>
.course-item {
  background: #ffffff;
  border-radius: 8px;
  padding: 30px;
  margin-bottom: 20px;
}
.course-item-top {
  display: flex;
}
.course-item-pic {
  width: 160px;
  height: 90px;
  margin-right: 20px;
  border-radius: 2px;
  overflow: hidden;
}
.course-item-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
}
.course-item__title {
  font-size: 18px;
  color: #222;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.course-item__text {
  display: inline-block;
  font-size: 14px;
}
.course-item__text + .course-item__text {
  margin-left: 0.1rem;
}

.course-item__text__course {
  color: #88bbff;
}
.course-item__text__video {
  color: #5ad0b2;
}
.course-item__text__freevideo {
  color: #d05a5a;
}
.course-child {
  display: flex;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
  cursor: pointer;
  .name {
    flex: 1;
    font-size: 18px;
  }
  .progress {
    margin-left: 20px;
    font-size: 12px;
    color: #999;
  }
}
</style>
