<template>
  <div>
    <template v-if="newLiveMsg.live">
      <div class="live-msg">
        <div class="txt">{{ $t('pages.learn.course.title2') }}</div>
        <div class="txt">
          {{ $t('pages.learn.course.msg') }} 《{{ newLiveMsg.course_name }}》 {{ $t('pages.learn.course.msg1') }}
          {{ newLiveMsg.live.start_time }}
          {{ $t('pages.learn.course.msg2') }}
        </div>
        <el-button class="in-btn" type="primary" size="small" round @click="goLive">{{
          $t('pages.learn.course.Enterlive')
        }}</el-button>
      </div>
    </template>
    <div class="con-title">{{ $t('pages.learn.course.title') }}</div>
    <div class="con-box">
      <template v-for="(item, index) in find">
        <ul v-bind:key="index" class="tabs-list">
          <template v-if="find[index].arrItem.length">
            <li class="tabs-hd">{{ item.name }}</li>
            <template v-for="(item1, index1) in item.arrItem">
              <li v-bind:key="index1">
                <div
                  :class="['tab', item.selectIndex == index1 ? 'on' : '']"
                  @click="selFindSelect"
                  :data-index="index1"
                  :data-i="index"
                  :data-key="item.key"
                  :data-val="item1.val"
                >
                  {{ item1.name }}
                </div>
              </li>
            </template>
          </template>
        </ul>
      </template>
    </div>

    <div class="switch-box">
      <el-button type="info" size="medium" @click="studyEarlyUpdate" plain
        >{{ $t('pages.learn.course.lastLearn')
        }}<i :class="['el-icon-caret-' + (filter.studyEarly == 'down' ? 'bottom' : 'top'), 'icon']"></i
      ></el-button>
      <el-button type="info" size="medium" @click="selectTimeUpdate" plain
        >{{ $t('pages.learn.course.updateTime')
        }}<i :class="['el-icon-caret-' + (filter.selectTime == 'down' ? 'bottom' : 'top'), 'icon']"></i
      ></el-button>
      <!-- <el-button class="rbtn" type="primary" size="medium" icon="el-icon-self-cc-book icon" @click="goCourseAll">{{ $t('pages.learn.course.changeCourse') }}</el-button> -->
    </div>

    <div class="con-box" v-loading="loading">
      <ul class="course-list">
        <template v-for="(item, index) in homeList">
          <li v-bind:key="index" class="item">
            <div class="left-pic">
              <template v-if="item.src">
                <img :src="item.src" alt="" @click="goCourseContent" :data-cid="item.id" :data-sid="item.sid" />
              </template>
              <template v-else>
                <div class="no-img"><i class="el-icon-self-13"></i></div>
              </template>
            </div>
            <div class="right-bd">
              <div class="title" @click="goCourseContent" :data-cid="item.id" :data-sid="item.sid">
                {{ item.title }}
              </div>
              <div class="tags">
                <template v-for="(item1, index) in item.arrTab">
                  <span v-bind:key="index">{{ item1 }}</span>
                </template>
              </div>
              <div class="time">
                {{ item.status }}&emsp;&emsp;<em>{{ item.time }}</em>
              </div>
              <div class="progress">
                {{ $t('pages.learn.course.progress') }}&emsp;<el-progress
                  :percentage="item.progress > 99.5 ? 100 : item.progress"
                  color="#b49441"
                ></el-progress>
              </div>
              <div class="right-sel">{{ item.myStatus }}</div>
              <el-button
                class="in-btn"
                type="primary"
                size="small"
                round
                @click="goCourseContent"
                :data-cid="item.id"
                :data-sid="item.sid"
                >{{ $t('pages.learn.course.showCourse') }}</el-button
              >
            </div>
          </li>
        </template>
        <template v-if="!homeList.length">
          <div class="no-data">{{ $t('pages.learn.course.noCourseStr') }}</div>
        </template>
      </ul>
    </div>
  </div>
</template>

<script>
import cAction from '@action'

export default {
  components: {},
  data() {
    return {
      filter: {
        studyEarly: 'down',
        selectTime: 'down'
      },
      find: [
        {
          name: this.$t('pages.learn.course.semeter'),
          isShow: false,
          selectIndex: 0,
          key: 'semester_id',
          arrItem: [] // 从后台请求
        },
        {
          name: this.$t('pages.learn.course.courseType'),
          isShow: false,
          selectIndex: 0,
          key: 'course_type',
          arrItem: [
            {
              val: '-1',
              name: this.$t('pages.learn.course.allLearn')
            },
            {
              val: '1',
              name: this.$t('pages.learn.course.mustLearn')
            },
            // {
            //   val: '2',
            //   name: this.$t('pages.learn.course.changeLearn')
            // },
            {
              val: '3',
              name: this.$t('pages.learn.course.repeatLearn')
            }
          ]
        }
      ],
      homeList: [], // 从后台请求
      param: {},
      timeInterval: null,
      newLiveMsg: {},
      loading: false
    }
  },
  mounted() {
    if (this.timeInterval) {
      clearInterval(this.timeInterval)
      this.timeInterval = null
    }
    // 获取最新直播
    this.getLatestLive()
    // 定时获取最新直播
    this.timeInterval = setInterval(this.getLatestLive, 10000)

    cAction.Course.getLearnFind()
      .then(data => {
        this.find[0].arrItem = data
        this.getAjaxList(true, this.$t('pages.learn.course.goCourseAllStr'))
      })
      .catch(e => {
        this.$message.error(e.message)
      })
      .finally(() => {})
  },
  destroyed() {
    if (this.timeInterval) {
      clearInterval(this.timeInterval)
      this.timeInterval = null
    }
  },
  methods: {
    goCourseAll() {
      this.$router.push({
        path: '/app/learn/course-all'
      })
    },
    goCourseContent(e) {
      const cid = e.currentTarget.dataset.cid
      const sid = e.currentTarget.dataset.sid
      this.$router.push({
        path: `/app/learn/course-detail/${sid}/${cid}`
      })
    },
    getAjaxList(bool, str) {
      this.loading = true
      cAction.Course.getCourseList(bool, this.param)
        .then(json => {
          this.homeList = json
          if (!json.length) {
            this.$message(str)
          }
        })
        .catch(e => {
          this.$message.error(e.message)
        })
        .finally(() => {
          this.loading = false
        })
    },
    /**
     * 分类选择 - 选中某一项
     */
    selFindSelect(e) {
      const _data = e.currentTarget.dataset
      const index = _data.index
      const json = this.find
      const i = _data.i
      json[i].selectIndex = index
      json[i].isShow = false
      if (_data.val === '-1') {
        delete this.param[_data.key]
      } else {
        this.param[_data.key] = _data.val
      }

      /* 调用接口 */
      this.getAjaxList(true, this.$t('pages.learn.course.noFitCourseStr'))
    },
    /**
     * 筛选 - 最近学习顺序
     */
    studyEarlyUpdate() {
      const na = this.filter.studyEarly === 'up' ? 'down' : 'up'
      this.filter.studyEarly = na
      if (na === 'down') {
        this.param.order_type = 'learn_time'
      } else if (na === 'up') {
        this.param.order_type = '-learn_time'
      }

      this.getAjaxList(true, this.$t('pages.learn.course.goCourseAllStr'))
    },
    /**
     * 筛选 - 选修时间顺序
     */
    selectTimeUpdate() {
      const na = this.filter.selectTime === 'up' ? 'down' : 'up'
      this.filter.selectTime = na
      if (na === 'down') {
        this.param.order_type = 'join_time'
      } else if (na === 'up') {
        this.param.order_type = '-join_time'
      }

      this.getAjaxList(true, this.$t('pages.learn.course.goCourseAllStr'))
    },
    /* 直接进直播 */
    goLive() {
      const live = this.newLiveMsg.live
      if (live.type === 8) {
        window.open(live.record_url || live.join_url)
      } else {
        this.$router.push({
          name: 'viewerCourseChapter',
          params: {
            sid: this.newLiveMsg.semester_id,
            cid: this.newLiveMsg.course_id,
            id: this.newLiveMsg.chapter_id
          }
        })
      }
    },
    // 获取最新直播
    getLatestLive() {
      cAction.Player.getNewLiveMsg()
        .then(json => {
          if (json.status === 200) {
            this.newLiveMsg = json.data
          }
        })
        .catch(e => {
          this.$message.error(e.message)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.live-msg {
  position: relative;
  padding: 15px;
  box-shadow: 0 1px 8px rgba(0, 0, 0, 0.1);

  .txt {
    padding-right: 100px;
  }

  .in-btn {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
  }
}

/* 列表 筛选 */
ul.tabs-list {
  float: left;
  width: 100%;
  margin: 0 0 0.15rem 0;
  padding: 0;
  font-size: 0.16rem;
  line-height: 1.5;
  border-top: 1px solid #e8e8e8;

  &:last-child {
    margin-bottom: 0;
  }

  .tabs-hd {
    display: inline-block;
    color: #fff;
    padding: 5px 0 9px;
    margin-top: -5px;
    width: 94px;
    text-align: center;
    background: url('~@/assets/images/type.png') no-repeat 0 0;
  }

  li {
    float: left;
    list-style: none;
    padding: 0.1rem 0;
    margin-right: 0.2rem;

    .tab {
      padding: 0 0.1rem;
      cursor: pointer;

      &.on {
        background: #b49441;
        color: #ffffff;
      }

      &:focus,
      &:hover {
        color: #b49441;
        background: #eeeeee;
      }
    }
  }
}

/* 筛选按钮 */
.switch-box {
  margin: 0 0.3rem;

  .icon {
    margin-left: 0.1rem;
  }

  .rbtn {
    float: right;
  }
}

/* 课程列表 */
ul.course-list {
  margin: 0;
  padding: 0;
  font-size: 0.18rem;
  color: #313131;

  .no-data {
    text-align: center;
    line-height: 2rem;
  }

  li.item {
    padding: 0.1rem 0;
    list-style: none;
    border-bottom: 1px solid #dcdcdc;
    overflow: hidden;

    &:first-child {
      padding-top: 0;
    }

    &:last-child {
      margin-bottom: 0.2rem;
    }

    .left-pic {
      position: relative;
      float: left;
      width: 1.8rem;
      // min-height: 1rem;
      overflow: hidden;

      .no-img {
        width: 100%;
        height: 1rem;
        text-align: center;
        line-height: 1.1rem;
        border: 1px solid #e2e2e2;

        i {
          font-size: 0.4rem;
          color: #e2e2e2;
        }
      }

      img {
        // position: absolute;
        // top: 50%;
        // left: 50%;
        display: block;
        width: 100%;
        // height: 100%;
        // transform: translate(-50%, -50%);
        cursor: pointer;
      }
    }

    .right-bd {
      position: relative;
      display: block;
      margin-left: 2rem;

      .title {
        width: 80%;
        line-height: 1.5;
        font-weight: 700;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        cursor: pointer;
      }

      .tags {
        margin: 0.05rem 0;
        font-size: 0.14rem;

        span {
          padding: 0 0.1rem;
          margin: 0;
          border-right: 1px solid #313131;

          &:first-child {
            padding-left: 0;
          }

          &:last-child {
            border: none;
          }
        }
      }

      .time {
        font-size: 0.12rem;

        em {
          font-style: normal;
          color: #898989;
        }
      }

      .progress {
        margin-top: 0.05rem;
        font-size: 14px;

        .el-progress {
          width: 50%;
          display: inline-block;
          vertical-align: text-bottom;
        }
      }

      .right-sel {
        position: absolute;
        right: 0;
        top: 0;
        font-size: 14px;
        padding: 0 0.1rem;
      }

      .in-btn {
        position: absolute;
        right: 0;
        bottom: 0;
      }
    }
  }
}

@media (max-width: 767px) {
  .switch-box {
    margin: 0;
  }
}
</style>
