<template>
  <div class="learn-conatiner">
    <Header title="学习项目" />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="5"
      >
        <van-cell
          v-for="(item, index) in list"
          :key="index"
          @click="goDetail(item)"
        >
          <span>{{ item.name }}</span>
          <img src="/images/applets/coming.png" alt />
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <my-tabbar active="1"></my-tabbar>
  </div>
</template>

<script>
import Header from "@/components/Header/index.vue";
export default {
  components: { Header },
  data() {
    return {
      loading: false,
      finished: false,
      pageNum: 1,
      pageSize: 10,
      flag: true,
      list: [],
      refreshing: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取管理员列表
    getList() {
      let vm = this;
      let param = {
        _index: this.pageNum,
        _size: this.pageSize,
        type: JSON.parse(localStorage.getItem("userInfo")).type,
      };
      vm.$https(
        {
          url: "learningProject/getPageList",
          method: "post",
          authType: this.backToken,
        },
        vm.$qs.stringify(param)
      )
        .then((res) => {
          if (res.data.resultCode === "200") {
            let data = res.data.data;
            this.loading = false;
            vm.list = this.flag ? data.records : vm.list.concat(data.records);
            this.pageNum = this.pageNum + 1;
            this.finished = data.records.length < this.pageSize;
            this.flag = false;
            this.refreshing = false;
          } else {
            this.$toast(res.data.message);
          }
        })
        .catch(function (err) {
          console.log(err);
        });
    },
    // 列表的load上拉加载事件
    onLoad() {
      if (!this.flag) {
        console.log("加载数据");
        this.loading = true;
        this.flag = false;
        this.getList();
      }
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;
      this.flag = true;
      this.loading = true;
      this.pageNum = 1;
      this.getList();
    },
    goDetail(item) {
      this.$router.push({ path: "/learnContent", query: { id: item.id } });
    },
  },
};
</script>

<style lang="scss" scoped>
.learn-conatiner {
  width: 100%;
  height: 100vh;
  padding: 50px 16px 60px;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  .van-pull-refresh {
    overflow-y: auto;
    padding: 0 8px;
  }
  .van-cell {
    padding: 0;
  }
  .van-cell__value {
    display: flex;
    justify-content: space-between;
    background: #f8f8f8;
    border-radius: 33px;
    font-size: 16px;
    padding: 12px;
    color: #333333;
    margin-top: 20px;
    img {
      width: 24px;
      height: 24px;
    }
  }
}
</style>