<template>
  <div class="course-list-wrapper">
    <ul class="course-header">
      <li class="title">
        <span class="main">全部课程</span>
        <span class="sub">单个项目最多只能选择30个课程</span>
      </li>
      <li class="order">
        <div class="num" @click="toggleOrder">
          <span>学习人数</span>
          <img v-show="isRise" src="../../../assets/custom/icon/icon-rise.png" alt />
          <img v-show="!isRise" src="../../../assets/custom/icon/icon-drop.png" alt />
        </div>
        <div class="cart-wrapper">
          <div class="cart" @click="toggleCart">
            <span>已选课程</span>
            <img src="../../../assets/custom/icon/icon-cart.png" alt />
            <div class="count">
              <span>{{cartList.length}}</span>
            </div>
          </div>
          <div v-show="isShowCart" class="curt-position">
            <ShoppingCart @close="toggleCart"></ShoppingCart>
          </div> 
        </div>
      </li>
    </ul>
    <div class="list-wrapper">
      <ul class="list">
        <li 
          class="course" 
          v-for="(item, index) in courseList.list" 
          :key="index"
          @click="toggleChecked(item)"
        >
          <div class="img">
            <img src="../../../assets/custom/test.png" alt />
            <span class="level">{{item.typeStr}}</span>
          </div>
          <div class="detail">
            <span class="title">{{item.courseName | shortName}}</span>
            <div class="chapter">
              <span>{{item.docName}}</span><span class="section-num">{{item.chapterSum}}章{{item.lectureNum}}节 {{item.totalTime}}分</span><span v-if="true" class="exam-num">5场考试</span>
            </div>
            <div class="cost">
              <span class="no">免费</span>
              <span class="num">{{item.joinNum}}人已学</span>
            </div>
          </div>
          <img v-show="item.checked" class="course-selected" src="../../../assets/custom/icon/icon-selected.png" alt />
        </li>
      </ul>
    </div>

    <div class="page-wrapper">
      <el-pagination
        background
        :current-page="searchParam.pageNo"
        :page-sizes="[10, 30, 50, 100]"
        :page-size="searchParam.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="courseList.totalRows"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>      
    </div>
    <!-- 弹框提示 -->
    <dialog-componet :dialogObj="dialogObj"></dialog-componet>
  </div>
</template>
<script>
import ShoppingCart from '@/components/education/custom/shopping-cart'
import dialog from "@/components/education/custom/dialog";
import { mapGetters, mapActions } from 'vuex'
export default {
  data() {
    return {
      isRise: true,
      isShowCart: false,
      dialogObj: {
        title: '课程数量已达上限',
        visible: false,
        message: '单个项目最多只能选择30个课程',
        // tip: '单个项目最多只能选择30个课程',
        hideMsg: '我知道了'
      }
    };
  },
  computed: {
    ...mapGetters(['cartList', 'courseList', 'searchParam'])
  },
  components: {
    ShoppingCart,
    dialogComponet: dialog,
  },
  methods: {
    ...mapActions(['setCartList', 'setSeachParam']),
    toggleOrder() {
      this.isRise = !this.isRise;
      let dir = this.isRise ? 1 : 2;
      this.searchParam.dir = dir;
      this.setSeachParam(this.searchParam);
    },
    toggleCart() {
      this.isShowCart = !this.isShowCart;
    },


    // 选择当前分页个数
    handleSizeChange(val) {
      this.searchParam.pageSize = val;
      this.setSeachParam(this.searchParam);
    },

    // 选择当前分页
    handleCurrentChange(val) {
      this.searchParam.pageNo = val;
      this.setSeachParam(this.searchParam);
    },

    // 选择课程（并要处理过滤，删除等）
    toggleChecked(item) {
      item.checked = !item.checked;
      if(item.checked) {
        if(this.cartList.length >= 30) {
          item.checked = !item.checked;
          this.dialogObj.visible = true;
          return;
        }
        this.cartList.push(item);
      } else {
        let delItemIndex = this.cartList.findIndex( course => {
          return course.courseId == item.courseId;
        })
        this.cartList.splice(delItemIndex, 1);
      }
      this.setCartList(this.cartList);
      this.$forceUpdate();
    },

  }
};
</script>
<style lang="less" scoped>
.course-list-wrapper {
  padding: 0 30px;
  min-width: 1180px;
  max-width: 1600px;
  .course-header {
    display: flex;
    flex-direction: row;
    height: 78px;
    // line-height: 78px;
    align-items: center;
    margin-right: 30px;
    justify-content: space-between;
    .title {
      .main {
        margin-right: 6px;
        font-size: 18px;
        font-weight: 600;
        color: #000;
      }
      .sub {
        font-size: 14px;
        font-weight: 400;
        color: #676869;
      }
    }
    .order {
      display: flex;
      flex-direction: row;
      align-items: center;
      font-size: 14px;
      color: #333333;
      img {
        position: relative;
        top: 2px;
        width: 14px;
        height: 14px;
      }
      .num {
        margin-right: 30px;
      }
      .cart-wrapper {
        position: relative;
        .cart {
          position: relative;
          .count {
            position: absolute;
            height: 20px;
            top: -8px;
            right: -12px;
            display: inline-block;
            span {
              font-size: 12px;
              padding: 2px;
              background: red;
              border-radius: 10px;
              color: #fff;
            }
          }
        }
        .curt-position {
          background: #fff;
          position: absolute;
          top: 30px;
          right: 0px;
          z-index: 100;
        }
      }

    }
  }
  .list-wrapper {
    height: 500px;
    overflow: auto;
    .list {
      display: flex;
      flex-direction: row;
      // justify-content: space-between;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding-bottom: 30px;
      // min-height: 1200px;
      // max-height: 1200px;
      overflow-y: auto;
      .course {
        cursor: pointer;
        position: relative;
        top: 0;
        left: 0;
        z-index: 1;
        display: flex;
        flex-direction: row;
        height: 120px;
        width: 527px;
        padding: 15px;
        margin-bottom: 10px;
        margin-right: 10px;
        border-radius: 4px;
        border: 1px solid rgba(228, 231, 237, 1);
        .img {
          position: relative;
          top: 0;
          left: 0;
          width: 160px;
          height: 90px;
          margin-right: 15px;
          img {
            width: 160px;
            height: 90px;
            border-radius: 4px;
          }
          .level {
            position: absolute;
            top: 0;
            left: 0;
            width: 44px;
            height: 22px;
            line-height: 22px;
            text-align: center;
            font-size: 14px;
            font-weight: 500;
            color: #fff;
            background: rgba(0, 0, 0, 0.5);
            border-radius: 4px 0px 4px 0px;
          }
        }
        .detail {
          position: relative;
          display: flex;
          flex-direction: column;
          flex: 1;
          .title {
            position: relative;
            top: -3px;
            left: 0;
            font-size: 16px;
            font-weight: 600;
            color: #373839;
          }
          .chapter {
            font-size: 14px;
            font-weight: 400;
            color: #999999;
            .section-num::before, .exam-num::before {
              content: '';
              position: relative;
              top: 2.5px;
              left: 0;
              display: inline-block;
              height: 14px;
              line-height: 14px;
              margin: 0 10px;
              width: 1px;
              background: #999999;
            }
          }
          .cost {
            position: absolute;
            left: 0;
            bottom: -5px;
            .no {
              font-size: 18px;
              font-weight: 600;
              margin-right: 4px;
              color: #d82b2b;
            }
            .num {
              font-size: 14px;
              font-weight: 400;
              color: #999999;
            }
          }
        }
        .course-selected {
          position: absolute;
          right: 0;
          bottom: 0;
          width: 44px;
          height: 44px;
        }
      }
    }
  }
  .page-wrapper {
    float: right;
    margin: 10px 0;
    margin-right: 230px;
  }

}
</style>