var app = getApp();
let {business_id} = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moneys: [

    ],
    moneynum: '',
  },

  //充值
  pay_list: function (e) {
    var that = this;
    console.log(e
    )
    //获取money
    // that.setData({
    //   preferential_id: e.currentTarget.dataset.id,

    // })

    wx.request({
      url: app.globalData.hostname + 'Cards/up_money',
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        business_id: app.globalData.business_id,
        openid: wx.getStorageSync('openId'),
        money:e.currentTarget.dataset.money,
        preferential_money: e.currentTarget.dataset.preferential_money
        // pay_id: that.data.preferential_id

      },

      // 创建充值订单
      success: (res) => {
        console.log(res, "创建订单");
        if (res.data.status == 1) {
          wx.request({
            url: app.globalData.hostname + 'Cards/pay',
            data: { pay_no: res.data.pay_no, business_id},
            method: 'post',
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res, '支付');
              wx.requestPayment({
                'timeStamp': res.data.pay_return.timeStamp,
                'nonceStr': res.data.pay_return.nonceStr,
                'package': res.data.pay_return.package,
                'signType': 'MD5',
                'paySign': res.data.pay_return.paySign,
                'success': function (res) {
                  console.log(res);
                  var openid = wx.getStorageSync("openid");
                  // var page = 1, typekind = that.data.tabIndex;
                  wx.showToast({
                    title: '支付成功',
                    icon: 'success',
                    image: '',
                    duration: 1000,
                    mask: true,
                    success: function (res) {
                      setTimeout(function () {
                        that.setData({
                          page: 1,
                          hasMore: true,
                          orderList: [],
                        })
                        that.getOrderList(that)
                        // that.getOrderList(that, openid, page, that.data.size, typekind)
                      }, 1000)
                    },
                  })
                },
              })
            },
          })
        }
      }
    })

  },


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

    //rechange
    wx.request({
      url: app.globalData.hostname + 'Cards/cards',
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: { business_id: app.globalData.business_id },
      success: (res) => {
        let  moneysData = res.data.data;
        console.log(moneysData,"moneysData")

        for (var i = 0; i < moneysData.length; i++) {
          moneysData[i].money = Number(moneysData[i].money).toFixed(0);
        }


          for (var i = 0; i < moneysData.length; i++) {
            moneysData[i].preferential_money = Number(moneysData[i].preferential_money).toFixed(0);
          }

          
        that.setData({
          moneys: moneysData
        })
        // that.setData({
        //   moneys: res.data.data
        // })
        // console.log(that.data.moneys, moneysData,"获取充值列表");
      }

    })


    //被分享者 点击拿到分享着的share_id
    var share_id = options.userid;
    //更新数据
    that.setData({
      userInfo: wx.getStorageSync('userInfo'),
    })

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
           * 用户点击右上角分享
           */
  onShareAppMessage: function (res) {
    var userid = app.globalData.userId;
    console.log(userid);
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '汽车美容案例',
      path: '/pages/home_page/home_page?userid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面

        var currentPage = pages[pages.length - 1]    //获取当前页面的对象

        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?userid=' + userid;  //当前页面url

        console.log(url);

      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})