const app = getApp()
Page({
  data: {
    list:[],
    keyword:"",
    page:1,
    pageTotal:9999,
  },
  onLoad: function (options) {
    var that = this;
    that.data.list = []
    that.data.page = 1
    that.getList()
  },
  searchList: function (e) {
    var that = this;
    that.data.list = []
    that.data.page = 1
    that.getList()
  },
  setKeyword(e){
    this.setData({
      keyword:e.detail.value
    })
  },
  getList(){
      var that=this;
      wx.showLoading({
        title: '正在加载...',
        mask: true
      })
      app.requestP({
        url: "/deviceConsole/deviceList",
        // contentType:"application/json",
        method:'post',
        data: {
          deviceCode: that.data.keyword
        }
      }).catch(err => {
        app.showToptip(that,"error", "数据获取失败，请稍后再试")
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      }).then(res => {
        var data = res.data.data;
        that.setData({
          list: that.data.list.concat(data),
          // pageTotal:Math.ceil(res.data.total/5),
          page: that.data.page,
        })
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
        wx.hideLoading()
      })
  },
  goInstructConfig(){
    var that = this;
    wx.scanCode({
      success: function (res) {
        var {result} = res;
        if(result.indexOf('imei=') != -1){
          result = result.slice(result.indexOf('imei=')+5,result.length)
        }
        app.requestP({
          url: "/deviceConsole/deviceInfo",
          method:"post",
          data:{
            deviceCode:result
          }
        }).then(res=>{
          wx.navigateTo({
            url: '/pages/instruct/config?imei='+result
          })
        }).catch(err=>{
          app.showToptip(that,'error',err.data.msg)
        })
      },
      fail:function(err){
        console.log(err);
      }
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.data.list=[]
    that.data.page = 1
    that.getList()
  },
  //上拉加载更多
  onReachBottom: function () {
    var that = this;
    var page = that.data.page
    if (that.data.pageTotal <= page){
    
    }else{
      that.data.page = page + 1;
      that.getList()
    }
  },

})