var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    orderIds: [],
    actualPrice: 0.00,
    isMergePay: '',
    payType: 'wx'
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderIds: options.orderIds,
      actualPrice: options.actualPrice,
      isMergePay: options.isMergePay
    })
    console.log(this.data.orderIds);
  },
  onReady: function () {

  },
  onShow: function () {
    let that = this;
    // 页面显示
    if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
      if (wx.getStorageSync('storeId')) {
        util.request(api.ChooseStoreId, {
          storeId: wx.getStorageSync('storeId'),
          merchSn: wx.getStorageSync('merchSn')
        }, 'POST').then(function (res) {
          if (res.errno === 0) {
            wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
            wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
            that.reLoad();
          }
        });
      }
    } else {
      wx.navigateTo({
        url: '/pages/auth/btnAuth/btnAuth',
      })
    }
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  reLoad: function () {
    let that = this;
    if (wx.getStorageSync('storeId')) {
      if (wx.getStorageSync('userId')) {
        wx.request({
          url: api.updateLoginUser,
          data: {
            userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
          },
          method: 'POST',
          header: {
            'Content-Type': 'application/json'
          },
          success: function (wxRes) {
            if (wxRes.data.errno === 0) {
              // console.log("用户信息更新成功");
            }
          },
          fail: function (err) {
            console.log("failed");
          }
        });
      }
    }
  },
  //改变支付方式
  changePayType: function (e) {
    console.log(e.currentTarget.id);
    this.setData({
      payType: e.currentTarget.id
    });
  },
  //向服务请求支付参数
  requestPayParam() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });

    console.log(this.data.payType);
    let url = '';
    if (this.data.payType == 'wx') {
      url = api.PayPrepayId;
    } else if (this.data.payType == 'pingan') {
      url = api.Payorder;
    } else if (this.data.payType == 'wxGlobal') {
      url = api.GlobalPayorder;
    } 

    // 测试领取优惠券
    // wx.redirectTo({
    //   url: '/pages/payResult/payResult?status=1&orderId=' + that.data.orderId,
    // })
    // todo
    util.request(url, {
      orderIds: that.data.orderIds, payType: 1,
      isMergePay: that.data.isMergePay
      }).then(function (res) {
        wx.hideLoading();
      if (res.errno === 0) {
        let payParam = res.data;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            wx.redirectTo({
              url: '/pages/payResult/payResult?status=1&orderIds=' + that.data.orderIds,
            })
          },
          'fail': function (res) {
            wx.redirectTo({
              url: '/pages/payResult/payResult?status=0&orderIds=' + that.data.orderIds,
            })
          }
        })
      }else {
        // util.showErrorToast(res.errmsg)
        wx.showModal({
          title: '',
          content: res.errmsg,
          showCancel: false
        });
      }
    });
  },
  startPay(e) {
    this.requestPayParam();
  }
})