<template>
<div>
  <template v-if="newLiveMsg.live">
    <div class="live-msg">
      <div class="txt">直播提醒：</div>
      <div class="txt">您的直播课程 《{{newLiveMsg.course_name}}》 将于 {{newLiveMsg.live.start_time}} 开始</div>
      <el-button class="in-btn" type="primary" size="small" round @click="goLive">进入直播</el-button>
    </div>
  </template>
  <div class="con-title">我的课程</div>
  <div class="con-box">
    <template v-for="(item, index) in find">
      <ul v-bind:key="index" class="tabs-list">
        <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>
      </ul> 
    </template>
  </div>

  <div class="switch-box">
    <el-button type="info" size="medium" @click="studyEarlyUpdate" plain>最近学习<i :class="['el-icon-caret-' + (filter.studyEarly == 'down' ? 'bottom' : 'top'), 'icon']"></i></el-button>
    <el-button type="info" size="medium" @click="selectTimeUpdate" plain>选修时间<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">选课广场</el-button> -->
  </div>

  <div class="con-box">
    <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">
              视频观看进度&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'>查看课程</el-button>
          </div>
        </li>
      </template>
      <template v-if="!homeList.lengsssth">
        <div class='no-data'>暂无相关课程</div>
      </template>
    </ul>
  </div>
</div>
</template>

<script>
import cAction from '@actions'

export default {
  components: { },
  data () {
    return {
      filter: {
        studyEarly: 'down',
        selectTime: 'down'
      },
      find: [{
        name: '学期',
        isShow: false,
        selectIndex: 0,
        key: 'semester_id',
        arrItem: [ // 从后台请求
          { val: '-1', name: '全部' },
          { val: '', name: '第一学期' },
          { val: '', name: '第二学期' },
          { val: '', name: '第三学期' },
          { val: '', name: '第四学期' }
        ]
      }, {
        name: '课程类型',
        isShow: false,
        selectIndex: 0,
        key: 'course_type',
        arrItem: [
          { val: '-1', name: '全部' },
          { val: '1', name: '必修课' },
          { val: '2', name: '选修课' },
          { val: '3', name: '重修课' }
        ]
      }],
      homeList: [
        // {
        //   id: '',
        //   src: '',
        //   title: '这里是一条长的标题不要超过25个汉字标题不要超过25个汉字',
        //   arrTab: ['3学分', '必修课', '第一学期'],
        //   status: '已发布',
        //   time: '2016-09-10至2017-01-10',
        //   myStatus: '已选修',
        //   progress: 0
        // }
      ],
      param: {},
      timeInterval: null,
      newLiveMsg: {}
    }
  },
  mounted () {
    if (this.timeInterval) {
      clearInterval(this.timeInterval)
      this.timeInterval = null
    }
    this.timeInterval = setInterval(() => {
      cAction.chapterAction.getNewLiveMsg().then(json => {
        if (json.status === 200) {
          this.newLiveMsg = json.data
        }
      }).catch(e => { this.$message.error(e.message) }).finally(() => { })
    }, 3000)

    const loading = this.$loading({ lock: true, text: '', spinner: '', background: 'rgba(255, 255, 255, 0.9)' })
    cAction.courseAction.getlearnFindList().then(data => {
      this.find[0].arrItem = data
      this.getAjaxList(true, '请去选课广场选课')
    }).catch(e => { this.$message.error(e.message) }).finally(() => { loading.close() })
  },
  destroyed () {
    if (this.timeInterval) {
      clearInterval(this.timeInterval)
      this.timeInterval = null
    }
  },
  methods: {
    goCourseAll () {
      this.$router.push({ path: '/app/my-learn/course-all' })
    },
    goCourseContent (e) {
      let cid = e.currentTarget.dataset.cid
      let sid = e.currentTarget.dataset.sid
      this.$router.push({ path: `/app/my-learn/course-detail/${sid}/${cid}` })
    },
    getAjaxList (bool, str) {
      cAction.courseAction.getCourseList(bool, this.param).then(json => {
        this.homeList = json
        if (!json.length) {
          this.$message(str)
        }
      }).catch(e => { this.$message.error(e.message) }).finally(() => {})
    },
    /**
     * 分类选择 - 选中某一项
     */
    selFindSelect (e) {
      let _data = e.currentTarget.dataset
      let index = _data.index
      let json = this.find
      let 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, '暂无符合条件的课程')
    },
    /**
     * 筛选 - 最近学习顺序
     */
    studyEarlyUpdate () {
      let 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, '请去选课广场选课')
    },
    /**
     * 筛选 - 选修时间顺序
     */
    selectTimeUpdate () {
      let 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, '请去选课广场选课')
    },
    /* 直接进直播 */
    goLive () {
      this.$router.push({ path: `/player/${this.newLiveMsg.semester_id}/${this.newLiveMsg.course_id}/live/${this.newLiveMsg.live.id}` })
    }
  }
}
</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(https://zws-imgs-pub.ezijing.com/754005be709bf2295bc55923c2b91fd8.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>

