<template>
  <div class="cart-list-wrapper">
    <div class="cart-header">
      <span class="title">已选项目课程（共{{this.cartList.length}}节）</span>
      <img @click="close" src="../../../assets/custom/icon/close.png" alt />
    </div>
    <ul class="cart-list">
      <li class="cart-course" v-for="item in cartList" :key="item.courseId">
        <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>
          <span class="cost">免费</span>
        </div>
        <div class="delete" @click="deleteCart(item)">
          <img src="../../../assets/custom/icon/icon-delete.png" alt />
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters(["cartList", "courseList"])
  },
  methods: {
    close() {
      this.$emit("close");
    },
    deleteCart(item) {
      // 先删除够物车中的课程
      let delItemIndex = this.cartList.findIndex(course => {
        return course.courseId == item.courseId;
      });
      this.cartList.splice(delItemIndex, 1);
      // 再查找当前课程列表中是否有，如果有，则将其置为不选中状态
      let courseIndex = this.courseList.list.findIndex(course => {
        return course.courseId == item.courseId;
      });
      if (courseIndex >= 0) {
        this.courseList.list[courseIndex].checked = false;
      }
    }
  }
};
</script>
<style lang="less" scoped>
.cart-list-wrapper {
  font-family: PingFang SC !important;
  width: 440px;
  height: 550px;
  background: #fff;
  box-shadow: 0px 0px 30px 0px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  .cart-header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    line-height: 50px;
    padding: 0 15px;
    font-size: 18px;
    font-weight: 700;
    color: #000000;
    background: #f9f9f9;
    img {
      width: 15px;
      height: 15px;
    }
  }
  .cart-list {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    height: 490px;
    overflow: auto;
    .cart-course {
      user-select: none;
      position: relative;
      display: flex;
      flex-direction: row;
      width: 410px;
      margin: 15px;
      align-items: center;
      border-radius: 4px;
      &::after {
        position: absolute;
        left: 0;
        bottom: -15px;
        z-index: 1;
        content: "";
        width: 410px;
        height: 1px;
        background: #e4e7ed;
      }
      .img {
        position: relative;
        top: 0;
        left: 0;
        width: 120px;
        height: 68px;
        margin-right: 15px;
        img {
          width: 120px;
          height: 68px;
          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;
        height: 60px;
        .title {
          position: relative;
          top: -7px;
          left: 0;
          font-size: 16px;
          font-weight: 600;
          color: #373839;
        }
        .cost {
          position: absolute;
          bottom: -7px;
          left: 0;
          font-size: 18px;
          font-weight: 600;
          margin-right: 4px;
          color: #d82b2b;
        }
      }
      .delete {
        width: 35px;
        img {
          cursor: pointer;
          width: 20px;
          height: 20px;
          float: right;
        }
      }
    }
  }
}
</style>