<template>
  <div class="payment">
    <Header @search="search($event)"></Header>
    <section class="section">
      <div class="section__header">
        <div class="container">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/index' }"
              >首页</el-breadcrumb-item
            >
            <el-breadcrumb-item>题库</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      </div>
      <div class="section__body">
        <div class="container">
          <div class="course-classify">
            <div class="course-classify__list">
              <div class="left-item">教育类型：</div>
              <div class="right-item">
                <div class="list">
                  <!-- <div
                    class="item"
                    :class="{ active: typeKey == '' }"
                    @click="changeType('')"
                  >
                    全部
                  </div> -->
                  <div
                    class="item"
                    v-for="(item, index) in typeList"
                    :key="index"
                    :class="{ active: params.educationTypeId == item.id }"
                    @click="changeType(item)"
                  >
                    {{ item.educationName }}
                  </div>
                </div>
              </div>
            </div>
            <div class="course-classify__list" v-if="businessList.length > 0">
              <div class="left-item">业务层次：</div>
              <div class="right-item">
                <div class="list">
                  <!-- <div class="item active">全部</div> -->
                  <div
                    class="item"
                    v-for="(item, index) in businessList"
                    :key="index"
                    :class="{ active: params.businessId == item.id }"
                    @click="changeBusiness(item)"
                  >
                    {{ item.projectName }} - {{ item.businessName }}
                  </div>
                </div>
              </div>
            </div>
            <div class="course-classify__list" v-if="subjectList.length > 0">
              <div class="left-item">科目分类：</div>
              <div class="right-item">
                <div class="list">
                  <div
                    class="item"
                    v-for="(item, index) in subjectList"
                    :key="index"
                    :class="{ active: params.subjectId == item.id }"
                    @click="changeSubject(item)"
                  >
                    {{ item.subjectName }}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="section__footer">
        <div class="container">
          <div class="course-list">
            <div class="course-list__header">
              <div class="sort-list">
                <div
                  class="sort-list__item"
                  @click="changeSort(1)"
                  :class="{ active: params.sortType == 1 ? true : false }"
                >
                  综合排序
                </div>
                <div
                  class="sort-list__item"
                  @click="changeSort(2)"
                  :class="{ active: params.sortType == 2 ? true : false }"
                >
                  低价优先
                </div>
                <div
                  class="sort-list__item"
                  @click="changeSort(3)"
                  :class="{ active: params.sortType == 3 ? true : false }"
                >
                  高价优先
                </div>
              </div>
            </div>
            <div class="course-list__body">
              <ul class="list clearfix">
                <li
                  class="course-item"
                  v-for="(item, index) in goodsList"
                  :key="index"
                  @click="goodsDetail(item)"
                >
                  <div class="course-item__img">
                    <img
                      :src="$tools.splitImgHost(item.coverUrl, true, 280)"
                      alt=""
                    />
                    <div class="note" v-if="item.year">{{ item.year }}</div>
                  </div>
                  <div class="course-item__title">
                    {{ item.goodsName }}
                  </div>
                  <div class="course-item__desc">
                    <div class="price">￥{{ item.standPrice | toFixed }}</div>
                    <a class="add" @click.stop="addCart(item)">加购物车</a>
                  </div>
                </li>
              </ul>
            </div>
          </div>

          <div class="pagination">
            <el-pagination
              @current-change="currentChange"
              background
              layout="prev, pager, next"
              :total="total"
              :pager-count="5"
              :page-size="params.pageSize"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </section>

    <ToolBar></ToolBar>
    <Footer></Footer>
  </div>
</template>

<script>
import Footer from "@/components/footer/index";
import Header from "@/components/header/index";
import ToolBar from "@/components/toolbar/index";
export default {
  name: "PaymentSuccess",
  components: {
    Footer,
    Header,
    ToolBar,
  },
  data() {
    return {
      projectId: "",
      typeList: [],
      businessList: [],
      subjectList: [],
      total: 0,
      params: {
        projectId: "",
        educationTypeId: "",
        businessId: "",
        subjectId: "",
        pageNum: 1,
        pageSize: 12,
        goodsStatus: 1,
        goodsType: 2,
        sortType: 1,
        searchKey: "",
      },
      goodsList: [],
    };
  },
  mounted() {
    this.params.searchKey = this.$route.query.searchKey || "";
    this.getEducationTypeList();
    this.changeSubject();
  },
  methods: {
    search(key) {
      this.params.searchKey = key || "";
      this.params.projectId = "";
      this.params.educationTypeId = "";
      this.params.businessId = "";
      this.params.subjectId = "";
      this.changeSubject();
    },
    changeSort(sortType) {
      if (this.params.sortType == sortType) return;
      this.params.sortType = sortType;
      this.changeSubject();
    },
    currentChange(e) {
      this.params.pageNum = e;
      this.changeSubject();
    },
    goodsDetail(item) {
      this.$router.push({
        path: "/bank-detail/" + item.goodsId,
      });
    },

    addCart(item) {
      this.$request
        .addCart({ goodsId: item.goodsId })
        .then((res) => {
          this.$message({
            message: "加入购物车成功",
            type: "success",
          });
        })
        .catch((err) => {
          if (err.code == 500) {
            this.$message({
              message: err.msg,
              type: "warning",
            });
          }
        });
    },

    changeType(item) {
      if (this.params.educationTypeId == item.id) {
        return;
      }
      this.params.educationTypeId = item.id;
      this.params.businessId = "";
      this.businessList = [];
      this.params.subjectId = "";
      this.subjectList = [];
      this.params.pageNum = 1;
      this.getBusinessList();
      this.changeSubject();
    },

    changeSubject(item) {
      if (item) {
        if (this.params.subjectId != item.id) {
          this.params.subjectId = item.id;
          this.params.pageNum = 1;
        }
      }
      this.$request.goodsList(this.params).then((res) => {
        this.goodsList = res.rows;
        this.total = res.total;
      });
    },

    getSubjectList() {
      this.$request
        .subjectList({
          businessId: this.params.businessId,
          projectId: this.projectId,
          educationId: this.params.educationTypeId,
        })
        .then((res) => {
          this.subjectList = res.rows;
          this.subjectList.unshift({ id: 0, subjectName: "全部" });
          this.params.subjectId = 0;

          this.changeSubject(this.subjectList[0]);
        });
    },

    changeBusiness(item) {
      if (this.params.subjectId == item.id) {
        return;
      }
      this.params.businessId = item.id;
      this.projectId = item.projectId;
      this.params.subjectId = "";
      this.subjectList = [];
      this.params.pageNum = 1;
      this.getSubjectList();
    },

    getBusinessList() {
      this.$request
        .businessList({ educationId: this.params.educationTypeId })
        .then((res) => {
          this.businessList = res.rows;
          this.projectId = this.businessList[0].projectId;
        });
    },

    getEducationTypeList() {
      this.$request.educationTypeList().then((res) => {
        this.typeList = res.rows;
      });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.payment {
  .section {
    &__header {
      height: 40px;
      display: flex;
      align-items: center;
      padding: 0 20px;
    }

    &__body {
      background: #f5f7fa;
      .course-classify {
        overflow: hidden;

        &__list {
          display: flex;
          margin: 24px 0;
          align-items: flex-start;
          .left-item {
            padding: 8px 0;
            width: 80px;
            font-size: 16px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            color: #333333;
          }

          .right-item {
            flex: 1;
            .list {
              display: flex;
              flex-wrap: wrap;
              .item {
                cursor: pointer;
                border-radius: 8px;
                margin: 0 10px;
                padding: 8px 16px;
                color: #666666;
                font-size: 16px;

                &.active {
                  color: #fff;
                  background: #3f8dfd;
                }
              }
            }
          }
        }
      }
    }

    &__footer {
      .course-list {
        &__header {
          margin-top: 32px;
          .sort-list {
            display: flex;
            align-items: center;
            &__item {
              cursor: pointer;
              width: 96px;
              height: 32px;
              border-radius: 16px;
              text-align: center;
              line-height: 32px;
              margin-right: 20px;

              &.active {
                color: #fff;
                background: #666666;
              }
            }
          }
        }

        &__body {
          .list {
            width: 100%;

            .course-item {
              float: left;
              margin: 110px 9px 0;
              width: 300px;
              height: 178px;
              background: #ffffff;
              box-shadow: 0px 10px 13px 3px rgba(63, 141, 253, 0.1);
              border-radius: 10px;
              position: relative;
              background: #fff;
              padding-top: 100px;
              cursor: pointer;

              &__img {
                width: 280px;
                height: 178px;
                background: #ffffff;
                box-shadow: 0px 0px 9px 1px rgba(0, 0, 0, 0.08);
                border-radius: 10px;
                position: absolute;
                left: 10px;
                top: -78px;
                overflow: hidden;

                .note {
                  position: absolute;
                  left: 0;
                  top: 0;
                  width: 80px;
                  height: 24px;
                  background: #d94404;
                  box-shadow: 0px 1px 1px 0px rgba(248, 78, 5, 0.4);
                  border-radius: 10px 0px 20px 0px;
                  text-align: center;
                  line-height: 24px;
                  color: #fff;
                }
              }

              &__title {
                margin: 0 8px;
                font-size: 14px;
                font-family: Microsoft YaHei;
                font-weight: 400;
                color: #333333;
                line-height: 24px;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
              }

              &__desc {
                height: 32px;
                position: absolute;
                left: 0;
                right: 0;
                bottom: 0;
                margin-left: 8px;
                display: flex;
                justify-content: space-between;

                .price {
                  font-size: 18px;
                  font-family: Microsoft YaHei;
                  font-weight: bold;
                  color: #ff2d55;
                  line-height: 32px;
                }

                .add {
                  display: block;
                  width: 118px;
                  height: 32px;
                  line-height: 30px;
                  background: #f2f4f7;
                  border-radius: 10px 0px 10px 0px;
                  font-size: 16px;
                  color: #3f8dfd;
                  text-align: center;

                  &:hover {
                    background: #3f8dfd;
                    color: #f2f4f7;
                  }
                }
              }
            }
          }
        }

        &__footer {
          overflow: hidden;
          .btn {
            cursor: pointer;
            width: 146px;
            height: 40px;
            background: #e3eaf7;
            border-radius: 8px;
            margin: 20px auto 40px;
            color: #3f8dfd;
            text-align: center;
            line-height: 40px;

            &:hover {
              color: #fff;
              box-shadow: 0px 8px 4px 0px rgba(7, 82, 208, 0.08);
              background: #3f8dfd;
            }
          }
        }
      }

      .pagination {
        padding: 30px 0;
        text-align: center;
      }
    }
  }
}
</style>
