//获取应用实例
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order_list: [],
    height: '',
    url: app.globalData.url,
    pageNum: 1,
    id:''//记录id
  },

  onClickLeft() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
    this.setData({
      statusBarHeight: statusBarHeight,
      height: 46 + statusBarHeight,
    });
  },


  //跳转订单详情（去付款）
  // itemclick(e) {
  //   console.log("列表条目的index==" + e.currentTarget.dataset.index)
  //   //去订单详情页
  //   if (e.currentTarget.dataset.item != 0) {
  //     let item = JSON.stringify(e.currentTarget.dataset.item);
  //     //console.log("传递的item" + item);
  //     wx.navigateTo({
  //       url: "/pages/orderdetail/orderdetail?item=" + item,
  //     });
  //   } 
  // },
    //是否登录
    delete(e) {
      var that = this;
      wx.showModal({
        title: '提示',
        content: '是否删除',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            that.go_delete(e)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    //删除点击
    go_delete(e) {
      console.log("列表条目的index==" + e.currentTarget.dataset.index)
      //去订单详情页
      // if (e.currentTarget.dataset.item != 0) {
      //   let item = JSON.stringify(e.currentTarget.dataset.item);
      //   //console.log("传递的item" + item);
      //   wx.navigateTo({
      //     url: "/pages/orderdetail/orderdetail?item=" + item,
      //   });
      // } 
      console.log("列表条目的item==" + e.currentTarget.dataset.item.id)
      this.deleteRecord(e.currentTarget.dataset.item.id)
    },
    deleteRecord(itemid) {
      this.setData({
        id: itemid
      });
      var that = this;
      wx.request({
        url: app.globalData.url + 'app/Ruilaiwechat/deleteRecord',
        data: {
          uid: wx.getStorageSync('uid'), //人员id
          id: this.data.id, //记录id
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
         },   
        success(res) {
          if (res.data.erro == 0) {
            that.getOrderList()
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'error',
              duration: 2000
            })
          }
        }
      })
    },

  onClickLeft() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      order_list: [],
      pageNum: 1
    })
    this.getOrderList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //this.getOrderList();
    var that = this;
    wx.request({
      url: app.globalData.url + 'app/Ruilaiwechat/selectRecordList',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
       }, 
       method: 'POST',
      data: {
        page: this.data.pageNum,
        uid: wx.getStorageSync('uid') //人员id
      },
      success: (res) => {
        var resp = res.data;
        if (res.data.erro == 0) {
          that.setData({
            pageNum: 1,
            order_list: resp.lists,
          });
          wx.stopPullDownRefresh({
            success: (res) => {},
          })
        }
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("==页面上拉触底事件的处理函数==")
    this.getOrderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  getOrderList: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + 'app/Ruilaiwechat/selectRecordList',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
       },   
       method: 'POST',
      data: {
        page: this.data.pageNum,
        uid: wx.getStorageSync('uid') //人员id
      },
      success: (res) => {
        var resp = res.data;
        if (res.data.erro == 0) {
          if (res.data.currentPage == 1) {
            console.log("xxxe==" + res.data.currentPage)
                // 检查列表数量是否等于30
            if (this.data.order_list.length === 30){
                  console.log("列表数量等于30")
                  that.setData({
                    order_list: resp.lists,
                    pageNum: res.data.currentPage + 1
                  });
             }else{
              that.setData({
                order_list: resp.lists,
              }); 
             }
            console.log("xxxe=pageNum=" + this.data.pageNum)
          } else {
            console.log("dddddddddd==")
            that.setData({
              order_list: this.data.order_list.concat(resp.lists),
              pageNum: res.data.currentPage + 1
            });
          }
        }
      }
    });
  },
})



//遗留问题1:列表分页加载的上拉加载有问题，当第一页的数据少于30条的时候会出现空白页，解决办法，前台参数传page2的时候，后台返回的参数页能返回currentPage为2