const config = require('../../../config/config.js')
const util = require('../../../utils/util.js')
const Http = require('../../../utils/http.js')
const Common = require('../../../common/common.js')
const qrCodeJS = require('../../../utils/qrcode.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billTypeValue: "",
    billId: "",
    disabled: true,
    detailData: {},
    tempFilePath: '',
    codeShow: false
  },
  qrcode(url) {
    let that = this;
    qrCodeJS.qrApi.draw(url, 'logoQRCode', wx.getSystemInfoSync().windowWidth * (260 / 375), wx.getSystemInfoSync().windowWidth * (260 / 375), function (res) {
      that.setData({
        tempFilePath: res
      })
    })
  },
  /**
   * 取消
   */
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  submit() {
    let that = this
    Http.getRequest(config.api.payChannel, app.globalData.token, '', {}, (res) => {
      if(res.code === 200) {
        if (res.data.payChannel === 1) {
          wx.login({
            success: res => {
              Common.getOpenId(res.code, app.globalData.bUserId, false)
                .then(res => {
                  if (res.data && res.data.data && res.data.data.openId) {
                    Http.postRequest(config.api.payBill, app.globalData.token, '', {
                      billTypeValue: that.data.billTypeValue, billId: that.data.billId,payMoney: that.data.detailData.billAllNeedPay, openId: res.data.data.openId
                    }, (el) => {
                      debugger
                      wx.requestPayment({
                        timeStamp: el.data.timeStamp,
                        nonceStr: el.data.nonceStr,
                        package: el.data.package,
                        signType: (el.data.signType) ? el.data.signType : "MD5",
                        paySign: el.data.paySign,
                        success: res => {
                          wx.showLoading({
                            title: '订单正在处理中...',
                          })
                          // that.payOrderUpdate(that.data.orderId, payOrderId, 1, '', '', that, that.data.composeOrderType);
                          if (res.errMsg == "requestPayment:ok") {
                            /**
                             * 用户支付成功以后跳转到券包列表
                             */
                            wx.navigateTo({
                              url: `/pages/bill/billdetail/index?billTypeValue=${that.data.billTypeValue}&billId=${that.data.billId}`,
                            })
                              // if (that.data.cardType == 100) {
                            //   wx.setStorage({
                            //     key: 'couponNum2',
                            //     data: "couponNum2"
                            //   })
                            // } else if (that.data.data.type != 5 && that.data.cardType != 100) {
                            //   wx.setStorage({
                            //     key: 'couponNum',
                            //     data: "couponNum"
                            //   })
                            // }
                          }
                        },
                        fail: res => {
                          wx.hideLoading();
                          wx.showToast({
                            title: '支付失败',
                            icon: 'none',
                          })
                          /**
                           * 支付失败，需要更新订单的状态
                           */
                          // that.payOrderUpdate(that.data.orderId, payOrderId, 2, '', 'fail', that, that.data.composeOrderType);
                          // that.setData({
                          //   showbutton: false
                          // })
                          return;
                        },
                        complete: res => { }
                      });
                      // if(el.code === 200) {
                      //   wx.navigateTo({
                      //     url: `/pages/bill/billdetail/index?billTypeValue=${el.data.billTypeValue}&billId=${el.data.billId}`,
                      //   })
                      // }
                    })
                  }
                })
                .catch(error => {
                  wx.showToast({
                    title: error.data.message,
                    icon: 'none',
                  })
                })
            }
          })
        } else {
          Http.postRequest(config.api.payBill, app.globalData.token, '', {
            billTypeValue: that.data.billTypeValue, billId: that.data.billId,payMoney: that.data.detailData.billAllNeedPay
          }, (el) => {
            if(el.code === 200) {
              that.setData({
                codeShow: true
              })
              that.qrcode(el.data)
            }
          })
        }
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    that.setData({
      billTypeValue: options.billTypeValue,
      billId: options.billId
    })  
    this.getData()
  },
  getData() {
    Http.getRequest(config.api.billDetail, app.globalData.token, '', {
      billId: this.data.billId,
      billTypeValue: this.data.billTypeValue,
    }, (res) => {
      if(res.code === 200) {
        this.setData({
          detailData: res.data
        })
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})