import {
  isCardID,
  getWaterDrop
} from '../../utils/index/index'
import {
  userInfoQuery
} from '../../utils/api/api'
import {
  encryptAndSign
} from '../../utils/index/getSM4'

import {
  viceBroadcast
} from '../../utils/index/callAmpe'
const app = getApp()
Page({
  data: {
    toTitleRef: null,
    numVal: '',
    numList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 'X', 0, '退格'],
    isOk: true,
    startTimeStamp: 0,
    snDisposition: {},
    timer: null
  },
  onLoad() {
    my.hideBackHome();
    this.setData({
      snDisposition: app.globalData.snDisposition
    })
    viceBroadcast(app.globalData.snDisposition.inputIdcardVoice)
  },
  clickAudio() {
    getWaterDrop()
  },
  //操作倒计时结束
  finish(event) {
    if (event == 'finish') {
      this.backToSpecifiedPage('/pages/resultPage/index?result=结果页超时')
    }
  },
  getVal(e) {
    // console.log(new Date());
    // return
    let val = e.currentTarget.dataset.item
    let numVal = this.data.numVal
    if (this.data.numVal.length >= 18 && val != '退格') {
      return
    }
    if (!this.data.isOk) {
      this.setData({
        isOk: true,
      })
    }
    switch (val) {
      case '退格':
        this.setData({
          numVal: numVal.substr(0, numVal.length - 1)
        })
        break;
      default:
        this.setData({
          numVal: numVal + val
        });
        break;
    }
    // console.log(this.data.numVal);
    if (this.data.numVal.length == 18) {
      this.getIsOk()
    }
  },
  bindlongtap(e) {
    let val = e.currentTarget.dataset.item
    if (val == '退格') {
      this.setData({
        numVal: ''
      })
    }
  },
  getIsOk() {
    let data = isCardID(this.data.numVal)
    if (data.state) {
      this.setData({
        isOk: true,
      })
    } else {
      this.setData({
        isOk: false,
      })
      console.log(data);
      my.showToast({
        content: data.data,
        duration: 3000,
      });
    }

  },
  // 确认
  sure(e) {
    if (this.data.numVal.length != 18) {
      this.getIsOk()
    }
    if (!this.data.isOk) {
      return
    }
    let snDisposition = app.globalData.snDisposition
    app.data.inputIdCard = this.data.numVal
    // 联网查询用户信息
    this.userInfoQuery()
    // 是否开启输入手机号
    if (snDisposition.noIdcardInputPhone) {
      this.backToSpecifiedPage('/pages/inputPhone/index')
    } else {
      this.backToSpecifiedPage( '/pages/resultPage/index?result=联网查询')
    }
  },
  // 联网查询用户信息
  async userInfoQuery() {
    try {
      let appKey = app.globalData.appKey
      let appSecret = app.globalData.appSecret
      let privateKey = app.globalData.privateKey
      let content = {
        sfzh: app.data.inputIdCard
      }
      content = JSON.stringify(content)
      let sm4 = encryptAndSign(appKey, appSecret, privateKey, content, "encrypted")
      // console.log(sm4);
      let data = {
        "appId": app.globalData.appId,
        "bizContent": sm4.encrypted,
        "reqTimestamp": sm4.timestamp,
        "sign": sm4.sign
      }
      let res = await userInfoQuery(data)
      app.data.isNetworkQueryError = true
      // 解密
      let decryptedData = encryptAndSign(appKey, appSecret, privateKey, res.data.bizContent, "decrypt")
      if (!decryptedData.decrypted) {
        return
      }
      let decrypted = JSON.parse(decryptedData.decrypted)
      let brushingCardUserIInfo = {
        name: decrypted.xm,
        idNum: decrypted.sfzh,
        photoBase64: decrypted.xp,
      }
      app.data.brushingCardUserIInfo = brushingCardUserIInfo
      app.data.userInfo = decrypted
      app.data.userInfo.avatar = decrypted.xp
    } catch (error) {
      app.data.isNetworkQueryError = false
      console.log(error);
      // my.reLaunch({
      //   url: '/pages/resultPage/index?result=结果页超时&&timer=' + app.globalData.snDisposition.resultPageTimeout,
      // })
    }
  },
  saveRef(ref){
    // 将ref存起来，在想要调用的地方使用
    this.toTitleRef = ref
  },
  /**
   * 跳转指定页面 并清除顶部计时器
   */
  backToSpecifiedPage(url) {
    this.toTitleRef && this.toTitleRef.clearIntervalAll();
    my.reLaunch({
      url: url,
    })
  },
});
