<template>
<div>
  <div class="con-title">{{ $t('pages.learn.course.changeCourse') }}</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="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="">
            </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">{{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 {
      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') }
        ]
      }, {
        name: this.$t('pages.learn.course.changeStatus'),
        isShow: false,
        selectIndex: 0,
        key: 'select_status',
        arrItem: [
          { val: '-1', name: this.$t('pages.learn.course.allLearn') },
          { val: '0', name: this.$t('pages.learn.course.noChange') },
          { val: '1', name: this.$t('pages.learn.course.alreadyChange') }
        ]
      }],
      homeList: [], // 从后台请求
      param: {}
    }
  },
  mounted () {
    /* sidebar取消 当前选中项 */
    this.VueEvent.$emit('can-change-sidebar', { defaultActive: '0' })

    const loading = this.$loading({ lock: true, text: '', spinner: '', background: 'rgba(255, 255, 255, 0.9)' })
    cAction.Course.getLearnFind().then(data => {
      this.find[0].arrItem = data
      this.getAjaxList(false, 'no course!!')
    }).catch(e => { loading.close(); this.$message.error(e.message) }).finally(() => { loading.close() })
  },
  methods: {
    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) {
      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(() => {})
    },
    /**
     * 分类选择 - 选中某一项
     */
    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(false, this.$t('pages.learn.course.noFitCourseStr'))
    }
  }
}
</script>
<style lang="scss" scoped>

/* 列表 筛选 */
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%);
      }
    }
    .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;
      }
      .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>
