<template>
  <div class="course-live">
    <template v-if="meetings.length">
      <div class="course-live-item" v-for="item in meetings" :key="item.id" @click="handleClick(item)">
        <div class="name"><i class="el-icon-video-play"></i>{{ item.topic }}</div>
        <div class="aside">{{ item.start_time }} {{ calcTimeText(item) }}</div>
      </div>
    </template>
    <div class="empty-data" v-else>暂无相关直播</div>
  </div>
</template>

<script>
export default {
  name: 'CourseLive',
  props: { course: { type: Object, default: () => ({}) } },
  data() {
    return {}
  },
  computed: {
    meetings() {
      return this.course.meetings || []
    }
  },
  methods: {
    handleClick(live) {
      // 直播
      const hasRecordUrl = live.enable_record && live.record_url
      if ([3, 5].includes(live.live_status) && !hasRecordUrl) {
        this.$message.error(this.$t('viewerMain.liveEnd'))
        return
      }
      window.open(live.record_url || live.join_url, '_blank')
    },
    // 计算日期
    calcTimeText(data) {
      let { start_time: liveTime, live_status: liveStatus } = data
      const map = {
        1: this.$t('live.notStarted'),
        2: this.$t('live.liveStreaming'),
        3: this.$t('live.liveEnd'),
        4: this.$t('live.start'),
        5: this.$t('live.liveEnd')
      }
      let result = map[liveStatus] || liveTime

      if (liveStatus === 1 && liveTime) {
        liveTime = liveTime.replace(/-/g, '/')
        const time = (new Date(liveTime).getTime() - new Date().getTime()) / 1000 || 0
        if (time <= 5 * 60) {
          result = this.$t('live.start')
        } else if (time <= 1 * 60 * 60) {
          result = this.$t('live.startInMinutes', {
            minutes: parseInt(time / 60)
          })
        } else if (time <= 24 * 60 * 60) {
          result = this.$t('live.startInHours', {
            h: parseInt(time / (60 * 60)),
            min: parseInt((time / 60) % 60)
          })
        } else {
          result = this.$t('live.startInDay', {
            day: parseInt(time / (24 * 60 * 60))
          })
        }
      }
      if (liveStatus === 3 && data.enable_record && data.record_url) {
        result = this.$t('live.watchReplay')
      }
      return result
    }
  }
}
</script>

<style lang="scss" scoped>
.course-live {
  padding: 0.2rem;
  background-color: #fff;
}
.course-live-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  padding: 5px;
  i {
    margin-right: 0.05rem;
    font-size: 0.2rem;
    vertical-align: middle;
  }
  .name {
    line-height: 0.3rem;
  }
  .aside {
    color: #666;
  }
  &:hover {
    color: #c01540;
    background-color: #f3f3f3;
  }
}
.course-live-item + .course-live-item {
  margin-top: 10px;
}
.empty-data {
  font-size: 0.24rem;
  color: #112c42;
  line-height: 1rem;
  text-align: center;
}
</style>
