const util  = require("../../../../utils/util.js");
Page({
  data: {
    data:{
      pageIndex: 1,
      pageSize: 10,
      courseId:'',
      playerId:''
    },
    list: [],
    bottom:true
  },
  onLoad(options){
    this.data.data.courseId = options.courseId; this.data.data.playerId = options.playerId;
    this.setData({ data: this.data.data })
  },
  onShow() {
    this.data.data.pageIndex=1;
    this.setData({data:this.data.data})
    this.getListData(false);
  },
  onPullDownRefresh() {
    var self = this;
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.data.data.pageIndex = 1;
      self.setData({ data: self.data.data });
      self.getListData(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom() {
    var self = this;
    if (self.data.bottom == false) return false;

    setTimeout(function () {
      self.data.data.pageIndex++;
      self.setData({ data: self.data.data });
      self.getListData(true);
    }, 1000);
  },
  getListData(load){
    let that = this;
    util.ajax({
      func: "v2/course/read/list",
      data: {'courseId': that.data.data.courseId, 'playerId': that.data.data.playerId, 'pageIndex': that.data.data.pageIndex, 'pageSize': that.data.data.pageSize},
      load: load
    }, function (res) {
      if (res.code == 0) {
        let newData = res.data.list, bottom = true;
        let list = !load ? newData : [...that.data.list, ...newData];
        if (newData.length < that.data.data.pageSize && list.length > that.data.data.pageSize) bottom = false;
        that.setData({ list, bottom })
      }else{
        util.showTips(res.reason);
      }
    });
  }
})