// pages/paymentInterface/paymentInterface.js
const WXAPI = require('../../utils/request') 
const moment = require('../../utils/moment')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio:'0',
    leases:{},
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type){
      this.setData({
        type:options.type
      })
    }
    let leases = wx.getStorageSync('leases')
    leases.timePeriod = moment(leases.signingTime).format('YYYY-MM-DD')+'~'+moment(leases.contractTime).format('YYYY-MM-DD')
    leases.dueDate = moment(leases.dueDate).format('YYYY-MM-DD')
    this.setData({
      leases
    })
  },
  onClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },
  payMentMethods(){
    let deviceinfoList = wx.getStorageSync('deviceinfoList')
    if(this.data.type == 1){
      let leases =this.data.leases
      let userInfo = wx.getStorageSync('userInfo')
      var data = {
        description: "交租",
        productName: "交租",
        total: parseInt(leases.monthRent)*100,//parseInt(this.data.leases.expand.rent)*100,
        userId: leases.cardId,
        cardId:leases.cardId,
        openid:wx.getStorageSync('openid'),
        appid:'wx20de9fe5943934a1',
        uid:userInfo.id,
        eid:userInfo.enterpriseId,
        orderType:1,
        payType:this.data.radio,
        expand:JSON.stringify({
          phone:leases.phone,
          nickname:leases.name
        })
      }
      WXAPI.miniPrepayOrder(data).then(res=>{
        if(res.data.success){
          if(this.data.radio == '0'){
            if(parseInt(leases.monthRent) === 0){
              wx.showToast({
                title: "支付成功",
                duration: 2000,
                icon: 'success'
              })
              let dataOne={
                code: "wx/updatePrepayStatus",
                cardId:leases.cardId,
                signature:wx.getStorageSync('signature'),
                data: {
                  orderNo: res.data.data.orderNo,
                  status:0
                }
              }
              WXAPI.sendCommandOne(dataOne).then(res2=>{
                wx.navigateTo({
                  url: '/pages/rentManagement/rentManagement',
                })
              })
              return false;              
            }
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              appid: res.data.data.appId,
              nonceStr: res.data.data.nonceStr,
              package: res.data.data.package,
              signType: res.data.data.signType,
              totalFee: parseInt(leases.monthRent)*100,
              paySign: res.data.data.paySign,
              success (res1) {
                wx.showToast({
                  title: "支付成功",
                  duration: 2000,
                  icon: 'success'
                })
                let dataOne={
                  code: "wx/updatePrepayStatus",
                  cardId:leases.cardId,
                  signature:wx.getStorageSync('signature'),
                  data: {
                    orderNo: res.data.data.orderNo,
                    status:0
                  }
                }
                WXAPI.sendCommandOne(dataOne).then(res2=>{
                  wx.navigateTo({
                    url: '/pages/rentManagement/rentManagement',
                  })
                })
              },
              fail (res1) {
                wx.showToast({
                  title: "支付失败",
                  duration: 2000,
                  icon: 'none'
                })
                let dataOne={}
                if(res1.errMsg == 'requestPayment:fail cancel'){
                  dataOne={
                    code: "wx/updatePrepayStatus",
                    signature:wx.getStorageSync('signature'),
                    data: {
                      orderNo: res.data.data.orderNo,
                      status:8
                    }
                  }
                }else{
                  dataOne={
                    code: "wx/updatePrepayStatus",
                    cardId:leases.cardId,
                    signature:wx.getStorageSync('signature'),
                    data: {
                      orderNo: res.data.data.orderNo,
                      status:6
                    }
                  }
                }
                WXAPI.sendCommandOne(dataOne).then(res2=>{
      
                })
              }
            })
          }else{
            let dataOne={
              code: "wx/updatePrepayStatus",
              data: {
                orderNo: res.data.data.orderNo,
                status:0
              }
            }
            WXAPI.sendCommand(dataOne).then(res2=>{
              if(res2.success){
                wx.navigateTo({
                  url: '/pages/rentManagement/rentManagement',
                })      
                wx.showToast({
                  title: "支付成功",
                  duration: 2000,
                  icon: 'success'
                })       
              }else{
                wx.showToast({
                  title: res2.message,
                  duration: 2000,
                  icon: 'error'
                })
              }
            })
          }
        }else{  
          wx.showToast({
            title: `支付失败：${res.data.msg}`,
            duration: 2000,
            icon: 'success'
          })  
        }
      })
    }else if(this.data.type == 2){
      let leases =this.data.leases
      if(this.data.radio == ''){
        wx.showToast({
          title: '请选择支付方式',
          icon:'none',
          duration:2000
        })
      }
      var data = {
        description: "交租",
        productName: "交租",
        total: parseInt(this.data.leases.monthRent)*100,//parseInt(this.data.leases.expand.rent)*100,
        userId: deviceinfoList[0].cardId,
        cardId:deviceinfoList[0].cardId,
        openid:wx.getStorageSync('openid'),
        appid:'wx20de9fe5943934a1',
        uid:1,
        eid:this.data.leases.enterpriseId,
        payType:this.data.radio,
        orderType:1,
        expand:JSON.stringify({
          phone:this.data.leases.phone,
          nickname:this.data.leases.name
        })
      }
      WXAPI.miniPrepayOrder(data).then(res=>{
        if(parseInt(leases.monthRent) === 0){
          wx.showToast({
            title: "支付成功",
            duration: 2000,
            icon: 'success'
          })
          let dataOne={
            code: "wx/updatePrepayStatus",
            signature:wx.getStorageSync('signature'),
            cardId:this.data.leases.cardId,
            data: {
              orderNo: res.data.data.orderNo,
              status:0
            }
          }
          WXAPI.sendCommandOne(dataOne).then(res2=>{
            wx.showToast({
              title: '支付成功',
              duration:2000
            })
            wx.navigateTo({
              url: '/pages/rentDetail/rentDetail',
            })  
          })
        }else{
          wx.requestPayment({
            timeStamp: res.data.data.timeStamp,
            appid: res.data.data.appId,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.package,
            signType: res.data.data.signType,
            totalFee: parseInt(this.data.leases.monthRentt)*100,
            paySign: res.data.data.paySign,
            success (res1) {
              wx.showToast({
                title: "支付成功",
                duration: 2000,
                icon: 'success'
              })
              let dataOne={
                code: "wx/updatePrepayStatus",
                signature:wx.getStorageSync('signature'),
                cardId:this.data.leases.cardId,
                data: {
                  orderNo: res.data.data.orderNo,
                  status:0
                }
              }
              WXAPI.sendCommandOne(dataOne).then(res2=>{
                wx.showToast({
                  title: '支付成功',
                  duration:2000
                })
                wx.navigateTo({
                  url: '/pages/rentDetail/rentDetail',
                })  
              })
            },
            fail (res1) {
              wx.showToast({
                title: "支付失败",
                duration: 2000,
                icon: 'none'
              })
              let dataOne={}
              if(res1.errMsg == 'requestPayment:fail cancel'){
                dataOne={
                  code: "wx/updatePrepayStatus",
                  signature:wx.getStorageSync('signature'),
                  data: {
                    orderNo: res.data.data.orderNo,
                    status:8
                  }
                }
              }else{
                dataOne={
                  code: "wx/updatePrepayStatus",
                  signature:wx.getStorageSync('signature'),
                  cardId:this.data.leases.cardId,
                  data: {
                    orderNo: res.data.data.orderNo,
                    status:6
                  }
                }
              }
              WXAPI.sendCommandOne(dataOne).then(res2=>{              
                wx.navigateTo({
                  url: '/pages/rentDetail/rentDetail',
                })   
              })
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})