// pages/mine/wxml/paypage/paypage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    textarea : "",
    prodUrl:app.globalData.prodUrl,
    discount: 0,
    setDiscount:0,
    totalSum: 0
  },
  bindTextAreaBlur: function (e) {
    this.setData({
      textarea: e.detail.value,
    })
  },
  formSubmit: function(e) {   
    let _this = this;
    wx.showModal({
      title: '提示',
      content: '请核对相关内容，确认支付！',
      success: function(res) {
        if (res.confirm) {
          if (_this.data.msg === '0') {
            wx.showModal({
              title: '提示',
              content: '请先添加收货地址',
              success: function(res) {
                if (res.confirm) {
                  //console.log('用户点击确定')
                  wx.navigateTo({
                    url: '../receivingaddress/receivingaddress?kid=' + e.target.dataset.kid
                  })
                } else if (res.cancel) {
                  //console.log('用户点击取消')
                }
              }
            })
          } else if (_this.data.msg === '1') {
            wx.showModal({
              title: '提示',
              content: '请先添加默认地址',
              success: function (res) {
                if (res.confirm) {
                  //console.log('用户点击确定')
                  wx.navigateTo({
                    url: '../receivingaddress/receivingaddress?kid=' + e.target.dataset.kid
                  })
                } else if (res.cancel) {
                  //console.log('用户点击取消')
                }
              }
            })
          } else {
            //console.log(_this.data.setDiscount)
            wx.request({
              url: app.globalData.prodUrl + 'web-pm/wx/pm/payment',
              data: {
                item_kid: e.detail.target.dataset.kid,
                address_id: e.detail.target.dataset.address_id,
                message: _this.data.textarea,
                red_pack_balance: _this.data.setDiscount
              },
              header: {
                'ydw-token': wx.getStorageSync('key').ydw_token
              },
              method: 'GET',
              dataType: 'json',
              responseType: 'text',
              success: function(res) {
                _this.setData({
                  payNo_id: res.data.payNo.kid
                })
                wx.request({
                  url: app.globalData.prodUrl + 'web-pm/wx/pay/to_pay',
                  data: {
                    payno_id: res.data.payNo.kid,
                    openid: wx.getStorageSync('key').openid
                  },
                  header: {
                    'ydw-token': wx.getStorageSync('key').ydw_token
                  },
                  success: function(data) {
                    var time = new Date()
                    wx.requestPayment({
                      'timeStamp': data.data.json.timeStamp,
                      'nonceStr': data.data.json.nonceStr,
                      'package': data.data.json.package,
                      'signType': 'MD5',
                      'paySign': data.data.json.paySign,
                      'success': function(res) {
                        wx.navigateBack({
                          delta: 1,
                          success: function() {
                            //console.log(data)
                            wx.request({
                              url: app.globalData.prodUrl + 'web-pm/wx/pm/push_one_user',
                              header: {
                                'ydw-token': wx.getStorageSync('key').ydw_token
                              },
                              data: {
                                payno_id: _this.data.payNo_id,
                                formId: e.detail.formId
                              },
                              success: function (data) {
                                //console.log(data)
                              }
                            })
                            wx.showToast({
                              title: '支付成功！',
                              icon: 'none',
                              duration: 3000
                            });
                            var pages = getCurrentPages();
                            var page = getCurrentPages().pop();
                            //console.log(pages)
                            if (page == undefined || page == null) return;
                            pages[1].onLoad(pages[1].options);
                          }
                        })
                      },
                      'fail': function(res) {
                        //console.log('fail', res)
                      }
                    })
                  }
                })
              },
              fail: function(res) {}
            })
          }
        } else if (res.cancel) {
          //console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var _this = this;
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/prepare_payment',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      data: {
        kid: options.kid
      },
      success: function(data) {
   
        _this.setData({
          array: data.data.item,
          shop: data.data.shop,
          msg: data.data.tip.msg,
          discount: data.data.balance,
          setDiscount: data.data.balance,
          totalSum: (data.data.item.price_end - data.data.balance).toFixed(2)
        });
        if (data.data.receive_address) {
          _this.setData({
            address: data.data.receive_address,
          })
        }
      },
    });
  },
  switch1Change: function (e) {
    //console.log(e);
    let _this = this;
    if(e.detail.value){
      _this.setData({
        setDiscount: _this.data.discount,
        totalSum: (_this.data.array.price_end - _this.data.discount).toFixed(2) 
      })
    } else {
      _this.setData({
        setDiscount: 0,
        totalSum: _this.data.array.price_end
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})