// pages/pay/pay.js
const { api, formatTime, imgName, wxLogin, payApi, alertTip, useTime, changeTime} = require('../../lib/util.js')
Page({
  data: {
    price:'30.0',
    payType: 'rent',//deposit rent
    success:true,//
    orderNumber:'',
    startRentTime:'',
    endRentTime:'',
    useTime:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //判断是否未支付定单或者押金 个租
  getRentRecord() {
    let that=this
    api.get('order/getRentRecord', {
      userId: wx.getStorageSync('user').userMapp.id
    })
      .then(res => {
        let { orderNumber, rentAmount, startRentTime, endRentTime } = res.data
        let useTime = changeTime(new Date(endRentTime) - new Date(startRentTime))
        this.setData({
          price: rentAmount,
          orderNumber,
          startRentTime: formatTime(new Date(startRentTime)),
          endRentTime: formatTime(new Date(endRentTime)),
          useTime
        })
      })
      .catch(err => {
        that.setData({
          success:false
        })
        alertTip(err)
      })
  },
  //押金配置
  getDePosit() {
    let that = this
    api.get('pay/getDeposit.json')
      .then(res => {
        console.log(res)
        let { deposit } = res.data
        this.setData({
          price: deposit,
        })

      })
      .catch(err => {
        that.setData({
          success: false
        })
        alertTip(err)
      })
  },
  onPullDownRefresh: function () {
    if(this.data.success){
      wx.stopPullDownRefresh()
    }else{
      //重新请求
      this.setUI()
    }
    
  },
  payMoney(){
    //租金
    if (this.data.payType=='rent'){
      this.payRent()
    }else{
      this.payDeposit()
    }
  },
  payDeposit() {
    console.log('押金支付')
    wxLogin()
      .then(code=>{
        payApi.get('pay/getOpenId.json',{
          code
        })
          .then(res=>{
            let openId = res.openId
            payApi.post('pay/miniAppDepositOrder',{
              openId
            })
              .then(res => {
                let { timeStamp, nonce_str: nonceStr, signType, prepay_id,paySign } = res
                //统一下单了
                wx.requestPayment({
                  timeStamp,
                  nonceStr,
                  'package': prepay_id,
                  'signType': 'MD5',
                  paySign,
                  'success': function (res) {
                    alertTip('押金支付成功,开始租车',function(){
                      wx.navigateBack()
                    })
                  },
                  'fail': function (res) {
                    console.error(res)
                  }
                })
              })
              .catch(err => {
                alertTip(err.err_code_des || err.return_msg)
                console.error(err)               
              })
          })
          .catch(err=>{
            alertTip(err.remark)
            console.error('openid', err)
          })
      })
      .catch(err=>{
        console.error(err)
      })

  },
  payRent() {
    let that=this
    // 租金
    wxLogin()
      .then(code=>{
        payApi.get('pay/getOpenId.json',{
          code
        })
          .then(res=>{
            let openId = res.openId
            payApi.post('pay/miniAppRentOrder',{
              openId,
              outTradeNo: that.data.orderNumber
            })
              .then(res => {
                let { timeStamp, nonce_str: nonceStr, signType, prepay_id,paySign } = res
                //统一下单了
                wx.requestPayment({
                  timeStamp,
                  nonceStr,
                  'package': prepay_id,
                  'signType': 'MD5',
                  paySign,
                  'success': function (res) {
                    alertTip('租金支付成功!', function () {
                      wx.redirectTo({
                        url: '/pages/choose_rent_type/choose_rent_type?status=normal',
                      })
                    })
                    console.log(res)
                  },
                  'fail': function (res) {
                    console.error(res)
                  }
                })
              })
              .catch(err => {
                alertTip(err.err_code_des)
                console.error(err)               
              })
          })
          .catch(err=>{
            alertTip(err.remark)
            console.error('openid', err)
          })
      })
      .catch(err=>{
        console.log(err)
      })
  }
})