//获取应用实例
const app = getApp();
var page = 1;
var flag = 0;
var size = 10;
var deviceId = 0;

Page({
  data: {
    list: [],
  },
  onLoad: function (option) {
    deviceId = option.id;

    page = 1;
    flag = 0;
    this.showData();
  },

  onShow: function () {
    var that = this;
    app.ajaxReadyCallback = res => {
      var oldlist = that.data.list;
      var list = res.data.data;
      if (page == 1) {
        oldlist = [];
      }
      var newlist = oldlist.concat(list);
      that.setData({
        list: newlist
      });

      if (list.length < size) {
        flag = 1;
      } else {
        page++;
      }
    };
    
  },

  onPullDownRefresh() { //下拉刷新
    page = 1;
    flag = 0;
    this.showData();
  },

  onReachBottom() { //上拉加载
    this.showData();
  },

  showData: function () {
    if (flag == 1) {
      return false;
    }

    var userId = app.globalData.userinfo.userId;
    var org = app.globalData.userinfo.org;

    app.ajax({
      url: app.globalData.serverUrl + 'server/deviceRecord/queryRecordList',
      type: 'POST',
      data: {
        userId: userId,
        orgId: org.orgId,
        deviceId: deviceId,
        page: page,
        size: size
      }
    });
  },

  recordDetail: function(e){
    var id = e.currentTarget.dataset.id;
    var did = e.currentTarget.dataset.did;
    wx.navigateTo({
      url: '../recordDetail/detail?id=' + id + '&did=' + did
    })
  }

})
