var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
var app = getApp();
Page({
  behaviors: [yz_pay],
  data: {
    language: '',
    isclick: true,
    order_id: '',
    store_id: '',
    orderData: null,
    balance: 0,
    payParams: '',
    info_form: {},
    order_sn: '',
    money: '',
    buttons: [],
    pay_sn: '订单编号',
    order_pay_id: '',
    uid: '',
    pid: '',
    status: '',
  },
  fromChild(e){
    if(this.data.money=='0.00') {
      // 0元
      this.sureBtn();
    }else {
      let datas = e.detail.submitArr;
      this.confirm(datas);
    }
  },
  confirm(e) {
    let type = datas.value;
    let uelStr = app.getNetAddresss("plugin.room.frontend.code.pay-code");
    app._getNetWork({
      url: uelStr,
      data: {
        id: this.data.id,
        money: this.data.code.money,
        pay_id: type,
        client_type: type == 1 ? 2 : ''
      },
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          if (type == 3) {
            if (res.data[0]) {
              wx.showToast({
                title: '支付成功',
                duration: 1000,
                icon: 'none',
                success: function (e) {
                  setTimeout(() => {
                    wx.redirectTo({
                      url: '/packageH/liveAuthorization/liveAuthorization_scanCode/liveAuthorization_scanCode',
                    });
                  }, 1100);
                }
              });
            }
          } else if (type == 1) {
            var payParams = {
              'timeStamp': res.data.config.timestamp,
              'nonceStr': res.data.config.nonceStr,
              'package': res.data.config.package,
              'signType': res.data.config.signType,
              'paySign': res.data.config.paySign
            };
            console.log('payParams::::', payParams);
            this.WXPay(payParams);
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  WXPay: function (payParams) {
    // let that = this;
    wx.requestPayment({
      'timeStamp': payParams.timeStamp,
      'nonceStr': payParams.nonceStr,
      'package': payParams.package,
      'signType': payParams.signType ? payParams.signType : 'MD5',
      'paySign': payParams.paySign,
      'success': function (res) {
        wx.redirectTo({
          url: '/packageH/liveAuthorization/liveAuthorization_scanCode/liveAuthorization_scanCode',
        });
      },
      'fail': function (res) {
        wx.showToast({
          title: '支付失败请稍后重试',
          icon: 'none',
          duration: 2000,
          success: function () {},
          fail: function () {}
        });
      }
    });
  },
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    let scene = decodeURIComponent(options.scene);
    if (scene) {
      var info_arr = [];
      info_arr = scene.split(",");
      for (let i = 0; i < info_arr.length; i++) {
        let chil_arr = [];
        chil_arr = info_arr[i].split("=");
        if (chil_arr[0] == "id") {
          this.setData({
            id: chil_arr[1]
          });
        }
        if(chil_arr[0] == "mid"){
          app._setMid(chil_arr[1]);
        }
      }
    }
    let _url = app.getNetAddresss('plugin.room.frontend.code.get-pay-type');
    app._getNetWork({
      url: _url,
      data: {
        id: this.data.id
      },
      success: (res)=> {
        var _data = res.data.data;
        console.log('PayResult::::', _data);
        if (res.data.result == 1) {
          this.hanleIcon(_data);
          this.setData({
            buttons: _data.buttons,
            code: _data.code
          });
        } else {
          wx.showToast({
            title: "授权码已被购买",
            icon: 'none',
            duration: 2000,
            success: function (e) {
              setTimeout(() => {
                wx.redirectTo({
                  url: '/packageH/liveAuthorization/liveAuthorization/liveAuthorization',
                });
              }, 1100);
            }
          });
          console.log('错误', res.data.msg);
        }
      },
      fail: function (res) {}
    });
  },

  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onUnload: function () {
    // 页面关闭
    if (this.data.tag == 'activity') {
      wx.navigateBack({
        delta: 1
      });
    }
  },
});
