const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const util = require('../../utils/util.js')
const qrCodeJS = require('../../utils/qrcode.js')
var app = getApp()

// pages/cardPay/cardPay.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeInfo: {},
    remainAmount: '',
    cardId: '',
    cardIdCover: '',
    totalFee: '',
    isPwd: false,
    payCheck: false,
    pwdSw: false,
    password: ''
  },

  checkPwd() {
    if (!this.data.totalFee) {
      wx.showToast({
        title: '金额不能为空！',
        icon: 'error'
      })
      return
    } else if (this.data.payCheck && !this.data.pwdSw) {
      this.setData({
        isPwd: true, // 打开密码框
        pwdSw: true // 允许点击其他区域时关闭密码框
      })
    } else {
      this.goGetPay()
    }
  },

  goGetPay(e) {
    const that = this
    const len = that.data.totalFee.toString().split('.')
    if (len[1] && len[1].length > 2) {
      wx.showToast({
        title: '金额不得超过两位小数',
        icon: 'none'
      })
      // 仅保留两位小数
      const num = len[0] + '.' + len[1][0] + len[1][1]
      that.setData({
        totalFee: num * 1
      })
      return
    }

    // 通行密钥，支付成功测试（夹带私货）
    if (that.data.totalFee == "YWQHKIX") {
      wx.navigateTo({
        url: `/pages/cardSuccess/cardSuccess?money=TEST`,
      })
    }

    if (that.data.totalFee > that.data.remainAmount) {
      wx.showToast({
        title: '卡余额不足',
        icon: 'error'
      })
      return
    } else if (that.data.totalFee == 0) {
      return
    }

    const data = {
      dynamicId: that.data.codeInfo.ID || that.data.cardId,
      totalFee: that.data.totalFee
    }

    // 需要密码时
    if (e && e.currentTarget.dataset.type == "pwdTrue") {
      // 非空判断
      if (that.data.password) {
        data.password = that.data.password
      } else {
        wx.showToast({
          title: '密码不能为空！',
          icon: 'error'
        })
        return
      }
    }

    // 当来自手机号收款时
    if (that.data.cardPayType == '3') {
      data.cardPayType = '3'

    }

    console.log(data, 'data');
    Http.post({
      url: config.api.cardPayScanCard,
      data
    })
      .then(res => {
        wx.showToast({
          title: '支付成功！',
          icon: 'success'
        })
        wx.reLaunch({
          url: `/pages/cardSuccess/cardSuccess?money=${data.totalFee}`,
        })
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'error'
        })
      })
  },

  getPayMoney(e) {
    this.setData({
      totalFee: e.detail.value
    })
  },

  // 关闭密码框
  contentClick(e) {
    if (this.data.pwdSw && e.target.id != 'checkPwd') {
      this.setData({
        isPwd: false,
        pwdSw: false
      })
    }
  },

  getDetail(dynamicId) {
    const data = {
      dynamicId
    }
    if (this.data.cardPayType) {
      data.type = 0
    }
    Http.get({
      url: config.api.cardInfoDetail,
      data
    }).then(res => {
      this.setData({
        remainAmount: res.data.remainAmount / 100,
        cardId: res.data.cardId,
        cardIdCover: res.data.cardId.slice(0, 4) + `******` + res.data.cardId.slice(14),
        payCheck: res.data.payCheck,
      })

    }).catch(err => {
      console.log(err, 'err');
      wx.showModal({
        title: err.message,
        complete: (res) => {
          if (res.cancel) {
            wx.navigateBack()
          }
          if (res.confirm) {
            wx.navigateBack()
          }
        }
      })
    })
  },

  inputPwd(e) {
    this.setData({
      password: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 'options');
    let codeInfo = null
    let id = null
    if (typeof options.codeInfo === 'string') {
      if (options.isTransform) {
        const JSONSTR = decodeURIComponent(options.codeInfo)
        codeInfo = JSON.parse(JSONSTR)
        id = codeInfo.ID
      } else {
        codeInfo = JSON.parse(options.codeInfo)
        id = codeInfo.ID
      }
    }

    if (options.cardPayType) {
      console.log(3);
      this.setData({
        cardPayType: '3'
      })
    }

    // 电子卡
    if (id) {
      this.getDetail(id)
      this.setData({
        codeInfo: codeInfo
      })
      // 实体卡
    } else {
      console.log(options.codeInfo);
      this.getDetail(options.codeInfo)
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})