<template>
  <div class="course-list-wrapper">
    <ul class="course-header">
      <li class="title">
        <span class="main">全部课程</span>
        <span v-show="courseLimitCount != -1" class="sub">单个项目最多只能选择{{courseLimitCount}}个课程</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 v-if="courseList && courseList.list && courseList.list.length" class="list-wrapper">
      <ul class="list">
        <li
          class="course"
          :class="{ 'course-checked': item.checked }"
          v-for="(item, index) in courseList.list"
          :key="index"
          @click="toggleChecked(item)"
        >
          <div class="img">
            <img :src="item.courseImageUrl" alt />
            <span class="level">{{item.typeStr}}</span>
          </div>
          <div class="detail">
            <span class="title">{{item.courseName | shortName}}</span>
            <div class="chapter">
              <span>{{item.docName | shortName(5)}}</span>
              <span class="section-num">{{item.chapterSum}}章{{item.lectureNum}}节 {{item.totalTime}}分</span>
              <span v-if="item.testCount" class="exam-num">{{item.testCount}}场考试</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"
          />
        </li>
      </ul>
    </div>
    <div v-else class="no-list">
      <img src="../../../assets/custom/icon/img-no-content.png" alt />
      <span class="tips">没有找到相关结果，请重新查询</span>
    </div>

    <div class="page-wrapper">
      <div class="page">
        <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>
    </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: '单个项目最多只能选择3个课程',
        tip: "",
        hideMsg: "我知道了"
      }
    };
  },
  computed: {
    ...mapGetters(["cartList", "courseList", "searchParam", "courseLimitCount"])
  },
  components: {
    ShoppingCart,
    dialogComponet: dialog
  },
  methods: {
    ...mapActions(["setCartList", "setSearchParam"]),
    toggleOrder() {
      this.isRise = !this.isRise;
      let dir = this.isRise ? 1 : 2;
      this.searchParam.dir = dir;
      this.setSearchParam(this.searchParam);
    },
    toggleCart() {
      this.isShowCart = !this.isShowCart;
    },

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

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

    // 选择课程（并要处理过滤，删除等）
    toggleChecked(item) {
      item.checked = !item.checked;
      if (item.checked) {
        if (this.courseLimitCount != -1 && this.cartList.length >= this.courseLimitCount) {
          item.checked = !item.checked;
          this.dialogObj.message = '单个项目最多只能选择' + this.courseLimitCount + '个课程';
          this.dialogObj.visible = true;
          return;
        }
        this.cartList.unshift(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;
  min-width: 1136px;
  // max-width: 1600px;
  .course-header {
    // position: relative;
    // top: 0;
    // left: 0;
    display: flex;
    flex-direction: row;
    height: 78px;
    // line-height: 78px;
    align-items: center;
    // margin-right: 100px;
    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 {
      // margin-right: 40px;
      padding-right: 12px;
      display: flex;
      flex-direction: row;
      align-items: center;
      font-size: 14px;
      color: #333333;
      img {
        position: relative;
        top: 2px;
        width: 14px;
        height: 14px;
      }
      .num {
        cursor: pointer;
        margin-right: 30px;
      }
      .cart-wrapper {
        position: relative;
        .cart {
          cursor: pointer;
          position: relative;
          .count {
            position: absolute;
            height: 20px;
            top: -8px;
            right: -12px;
            display: inline-block;
            span {
              display: inline-block;
              min-width: 18px;
              text-align: center;
              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: flex-start;
      flex-wrap: wrap;
      overflow-y: auto;
      .course {
        user-select: none;
        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);
        &.course-checked {
          &::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            border: 2px solid #449284;
            border-radius: 4px;
          }
        }
        .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 {
    // width: 1074px;
    text-align: right;
    .page {
      // float: right;
      margin: 20px 0;
    }
  }
  .no-list {
    position: relative;
    top: 0;
    left: 0;
    z-index: 1;
    display: flex;
    flex-direction: column;
    // padding: 30px 200px;
    width: 1000px;
    text-align: center;
    margin-bottom: 100px;
    align-items: center;
    img {
      width: 300px;
      height: 300px;
    }
    .tips {
      position: relative;
      top: -30px;
      left: 10px;
      z-index: 2;
      color: #999999;
    }
  }
}
</style>
