<template>
  <div class="course-item custom-class" :style="isOpen ? 'padding-bottom:0;' : ''">
    <div class="course-item-top" @click="toggleChild">
      <div class="course-arrow" :class="isOpen ? 'course-arrow__up' : 'course-arrow__down'"></div>
      <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>
            <div
              class="course-item__text course-item__text__freevideo"
            >{{ data.free_video_num }}个免费视频</div>
          </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" v-show="isOpen">
      <div class="course-child" v-for="item in data.child" :key="item.id" @click="onClick(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 {
      isOpen: false
    }
  },
  filters: {
    progressText(value) {
      value = parseInt(value)
      if (value === 0) {
        return '未开始'
      }
      if (value === 100) {
        return '已学完'
      }
      return `已学${value}%`
    }
  },
  methods: {
    toggleChild() {
      this.isOpen = !this.isOpen
    },
    onClick(data) {
      this.$emit('on-click', data)
    }
  }
}
</script>

<style lang="scss" scoped>
.course-item {
  padding: 0.3rem 0;
  border-bottom: 1px solid #ccc;
}
.course-arrow {
  width: 0.6rem;
  height: 2rem;
}
.course-arrow__up {
  background: url('https://zws-imgs-pub.ezijing.com/transport/weapp/icon_arrow_up.png') no-repeat left center;
  background-size: 0.3rem 0.3rem;
}
.course-arrow__down {
  background: url('https://zws-imgs-pub.ezijing.com/transport/weapp/icon_arrow_down.png') no-repeat left center;
  background-size: 0.3rem 0.3rem;
}
.course-item-top {
  display: flex;
}
.course-item-bottom {
  padding-left: 0.6rem;
}
.course-item-pic {
  width: 3.15rem;
  height: 2rem;
  margin-right: 0.15rem;
  overflow: hidden;
}
.course-item-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
}
.course-item__title {
  font-size: 0.26rem;
  color: #666;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.course-item__text {
  display: inline-block;
  height: 0.48rem;
  padding: 0 0.2rem;
  font-size: 0.24rem;
  line-height: 0.48rem;
  text-align: center;
  box-sizing: border-box;
}
.course-item__text + .course-item__text {
  margin-left: 0.1rem;
}

.course-item__text__course {
  color: #fff;
  background-color: #75b0ff;
}
.course-item__text__video {
  min-width: 1.8rem;
  color: #fff;
  background-color: #5ad0b2;
}
.course-item__text__freevideo {
  min-width: 1.8rem;
  margin-bottom: 0.1rem;
  color: #fff;
  background-color: #d05a5a;
}
.course-child {
  display: flex;
  align-items: center;
  padding: 15px 15px 15px 0;
  border-bottom: 1px solid #eee;
  background: url('https://zws-imgs-pub.ezijing.com/transport/weapp/icon_arrow.png') no-repeat right center;
  background-size: 5px 10px;
  .name {
    flex: 1;
    font-size: 13px;
    color: #666;
  }
  .progress {
    margin-left: 20px;
    font-size: 12px;
    color: #999;
  }
}
.course-child:last-child {
  border-bottom: 0;
}
</style>
