
let app = getApp();
let host = app.globalData.servsers; // 请求的url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state:2, // 请求状态]
    orderList:[], // 订单列表
    host: host,
    rows:1, //默认第一页
    requestState:true, //请求状态，默认是true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

      this.setData({
        state: options.state
      })
    
    
    
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderList(this, this.data.state, this.data.rows)
  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("上拉")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.requestState){
      this.setData({
        rows: this.data.rows + 1
      })
      this.getOrderList(this,this.data.state,this.data.rows)
    }
    console.log('到底了')
  },

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

  },
  getOrderList(that, state, rows){
    let orderList = that.data.orderList;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: host + '/queryOrderCustomerByUserId',
      method:'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        userId:app.globalData.user_id,
        orderStatus: state, //订单状态  0未支付\\1: 待定制\\2: 待发货\\3: 已发货\\4: 已完成\\5: 取消'
        limit:10,
        offset: rows,
      },
      success(res){
        console.log(res,"orderList")
        let data = res.data;
        data.forEach((el,index)=>{
          console.log(index)
          el['imgArray'] = el.orderCustomGoodsList.length > 0 && el.orderCustomGoodsList[0].customGoodsImg ? el.orderCustomGoodsList[0].customGoodsImg.split(',') : [];
        })
        orderList.push(...data)
        if(res.data.length < 10 && rows !==1){
          that.setData({
            requestState:false
          })
        }
        that.setData({
          orderList: orderList
        })

        console.log(that.data.orderList,"orderList")
      },
      complete(){
        wx.hideLoading()
      }
    })
  },
  orderPay(e){ // 订单付款
    console.log(e);
    let item = e.currentTarget.dataset.item;//列表数据
    wx.request({
      url: host +'/weChatPay',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        orderCustomId: item.orderCustomId,
        openId: app.globalData.openid,
      },
      success(el){
        let res = el.data;
        console.log(res)
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: res.signType,
          paySign: res.paySign,
          success(res) {
            console.log(res, "结算结果")
            if (res.errMsg === 'requestPayment:ok') {
              // wx.showToast({
              //   title: '支付成功',
              // })
              wx.redirectTo({
                url: '/customized/pages/result/result?state=true',
              })
            } else {
              wx.redirectTo({
                url: '/customized/pages/result/result?state=false',
              })
            }
          },
          fail(err) {
            wx.showToast({
              title: err.errMsg,
              icon: 'error'
            })
            console.log(err)
          },
          complete() {
            wx.hideLoading()
          }
        })
      }
    })
  }
})