<!--
 * @Author: ch
 * @Date: 2022-05-04 17:28:28
 * @LastEditors: ch
 * @LastEditTime: 2022-05-04 17:28:48
 * @Description: file content
-->

<template>
  <div class="page">
    <main class="main">
      <nav class="main__nav">
        <p class="main__nav-crumbs">
          全部商品<i class="el-icon-arrow-right"></i>开发书籍
        </p>
        <!-- <div class="main__nav-sort flex flex-middle">
          <span class="main__nav-sort-txt">分类 :</span>
        </div> -->
        <div class="main__nav-sort flex flex-middle">
          <span class="main__nav-sort-txt">排序 :</span>
          <span
            class="main__nav-sort-btn"
            :class="navActive == 0 ? 'main__nav-sort-active' : ''"
            @click="onNavClick(0)"
            >综合</span
          >
          <Sort
            :class="navActive == 1 ? 'main__nav-sort-active' : ''"
            sortText="价格"
            :sortType="sortType"
            @onSort="onSort"
          ></Sort>
          <span
            :class="navActive == 3 ? 'main__nav-sort-active' : ''"
            class="main__nav-sort-btn"
            @click="onNavClick(3)"
            >上新</span
          >
        </div>
      </nav>
      <div class="main__content">
        <UiGoodsItem
          :item="item"
          v-for="item in listData"
          :key="item.id"
        ></UiGoodsItem>
      </div>
      <el-pagination
        class="main__pagination flex flex-right"
        @current-change="handleCurrentChange"
        :current-page.sync="params.pageIndex"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </main>
  </div>
</template>
<script>
import { ApiGetGoodsList } from "@/plugins/api/goods";
import Sort from "./module/SortItem.vue";
import UiGoodsItem from "@/components/UiGoodsItem.vue";
export default {
  components: { Sort, UiGoodsItem },
  data() {
    return {
      navActive: 0,
      listData: [],
      total: 0,
      // 0:综合,1:desc,2:asc,3:上新
      sortType: 0,
      params: {
        length: 20,
        pageIndex: 1,
        name: "",
        categoryId: "",
        order: "",
      },
    };
  },
  async created() {
    this.params.name = this.$route.query.keyword;
    this.getGoodsListData();
  },
  methods: {
    onNavClick(i) {
      console.log(i);
      let vm = this;
      vm.sortType = i;
      vm.navActive = i;
      vm.getGoodsListData();
    },
    onSort() {
      let vm = this;
      vm.navActive = 1;
      vm.sortType < 2 ? vm.sortType++ : (vm.sortType = 1);
      vm.getGoodsListData();
    },
    // 获取商品列表
    async getGoodsListData() {
      let vm = this;
      switch (vm.sortType) {
        case 0:
          vm.$set(vm.params, "order", "");
          break;
        case 1:
          vm.$set(vm.params, "order", "starting_price:desc");
          break;
        case 2:
          vm.$set(vm.params, "order", "starting_price:asc");
          break;
        case 3:
          vm.$set(vm.params, "order", "create_time");
          break;
      }
      let res = await ApiGetGoodsList(vm.params);
      if (res.error) {
        vm.$message.error(res.error.message);
        return false;
      }
      console.log(`goodListRes`, res.result);
      vm.total = res.result.total;
      vm.listData = res.result.records;
    },
    handleCurrentChange(val) {
      let vm = this;
      vm.$set(vm.params, "pageIndex", val);
      vm.getGoodsListData();
    },
  },
  watch: {
    //监听路由
    //监听路由的categoryId属性的数据变化
    "$route.query.keyword": function () {
      this.params.name = this.$route.query.keyword;
      this.getGoodsListData();
    },
  },
};
</script>
<style lang="scss" scoped>
.page {
  background: #f8f8f8;
  width: 100%;
  min-height: 600px;
}

.main {
  @include layout-box;
  padding-top: 14px;
  padding-bottom: 60px;

  &__nav {
    padding-top: 24px;
    &::after {
      display: block;
      width: 1200px;
      content: "";
      height: 1px;
      background: #eee;
    }
    &-crumbs {
      .el-icon-arrow-right {
        margin: 0 10px;
      }
    }
    &-sort {
      width: 100%;
      height: 50px;
      padding: 0 30px;
      &-txt {
        color: #999999;
        margin-right: 30px;
      }
      &-btn {
        margin-right: 50px;
        cursor: pointer;
      }
      &-active {
        color: #ff512b;
      }
    }
  }
  &__content {
    margin-top: 30px;
    display: grid;
    width: 100%;
    grid-template-columns: repeat(auto-fill, 232px);
    justify-content: space-between;
    grid-row-gap: 10px;
  }
  &__pagination {
    margin-top: 60px;
    /deep/.el-pager {
      margin-left: 8px;
    }

    /deep/button,
    /deep/.number,
    /deep/.btn-quicknext,
    /deep/.btn-quickprev {
      width: 32px;
      height: 32px;
      text-align: center;
      line-height: 32px;
      margin-left: 8px;
      border-radius: 2px 2px 2px 2px;
      border: 1px solid rgba(0, 0, 0, 0.15);
      font-size: 14px;
      font-family: Microsoft YaHei-Regular, Microsoft YaHei;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.65);
    }

    /deep/.active {
      background: #ff512b;
      color: #fff;
    }
  }
}
</style>
