// pages/rentDetail/rentDetail.js
const moment = require('../../utils/moment')
const WXAPI = require('../../utils/request') 
const QR = require('../../utils/qrcode-base64/weapp-qrcode.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leases:[],
    payment:[],
    qrcodeStatus:false,
    codeUrl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getApartmentCardInfoMethods()
    // let imgData = QR.drawImg('weixin://wxpay/bizpayurl?pr=ke2McSTzz', {
    //   size: 500
    // })
    // wx.previewMedia({
    //   sources: [{url:imgData,type:'image'}], // 需要预览的资源列表
    //   current: 0, // 当前显示的资源序号
    //   url: imgData // 当前预览资源的url链接
    // })
    // this.setData({
    //   codeUrl:imgData,
    // })
  },
  // 支付
  payMentMethods(){
    wx.setStorageSync('leases', this.data.leases)
    wx.navigateTo({
      url: '/pages/paymentInterface/paymentInterface?type=2'
    })
    return
    let deviceinfoList = wx.getStorageSync('deviceinfoList')
    var data = {
      description: "交租",
      productName: "交租",
      total: parseInt(this.data.leases.expand.rent)*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:0
    }
    WXAPI.miniPrepayOrder(data).then(res=>{
      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.expand.rent)*100,
        paySign: res.data.data.paySign,
        success (res1) {
          wx.showToast({
            title: "支付成功",
            duration: 2000,
            icon: 'success'
          })
          let dataOne={
            code: "wx/updatePrepayStatus",
            signature:wx.getStorageSync('signature'),
            data: {
              orderNo: res.data.data.orderNo,
              status:0
            }
          }
          WXAPI.sendCommandOne(dataOne).then(res2=>{

          })
        },
        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'),
              data: {
                orderNo: res.data.data.orderNo,
                status:6
              }
            }
          }
          WXAPI.sendCommandOne(dataOne).then(res2=>{

          })
        }
      })
    })
  },
  // 获取合同信息
  getApartmentCardInfoMethods(){
    let deviceinfoList = wx.getStorageSync('deviceinfoList')
    if(deviceinfoList[0].cardId){
      let data={
        signature:wx.getStorageSync('signature'),
        id:deviceinfoList[0].cardId,
      }
      WXAPI.getApartmentCardInfo(data).then(res=>{
        if(res.success){
          let leases = res.data.leases
          let payment = res.data.payment
          if(leases.length>0){
            let getdate = new Date()
            let year = getdate.getFullYear()
            let month = getdate.getMonth()+1
            let leasesObject = leases[leases.length-1]
            leasesObject.signingTime = moment(leasesObject.signingTime).format('YYYY-MM-DD')
            leasesObject.contractTime = moment(leasesObject.contractTime).format('YYYY-MM-DD')
            leasesObject.expand=JSON.parse(leasesObject.expand)
            leasesObject.paymentDate = year+'-'+month+'-01'
            this.setData({
              leases:leases[leases.length-1],
              payment:payment
            })
          }else{
            wx.showToast({
              title: "还未签订合同",
              duration: 2000,
              icon: 'none'
            })
          }
        }else{
          wx.showToast({
            title: "获取合同信息失败",
            duration: 2000,
            icon: 'none'
          })
        }
      })
    }else{
      wx.showToast({
        title: "还未有对应合同",
        duration: 2000,
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})