// pages/real_name/real_name.js
const app = getApp();
const util = require('../../utils/utils.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    frameShow: false,
    cardVal: '',//身份证号
    nameVal: '',//姓名
    mobile:''

  },
  onLoad: function (options) {
    this.setData({
      mobile: my.getStorageSync({'key':'userInfo'}).data.mobile
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  frameClose: function () {
    let that = this
    that.setData({
      frameShow: false
    })
  },
  //验证名字
  ckName(e) {
    this.setData({
      nameVal: e.detail.value
    })
  },
  name(e) {
    this.setData({
      nameVal: e.detail.value
    })
  },
  namefirm(e) {
    this.setData({
      nameVal: e.detail.value
    })
  },
  //验证身份证号
  ckCard(e) {
    this.setData({
      cardVal: e.detail.value
    })
  },
  input(e) {
    this.setData({
      cardVal: e.detail.value
    })
  },
  valuefirm(e) {
    this.setData({
      cardVal: e.detail.value
    })
  },
  // sureSubmit:function(){
  //   my.reLaunch({
  //     url: '/pages/index/index',
  //   })
  // },
  changeMobile: function () {
    my.navigateTo({
      url: '/pages/my_account/my_account',
    })
  },
  //提交申请按钮
  sureSubmit: util.throttle(function () {
    let reg = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
    let reg1 = /^[\u4E00-\u9FA5]{2,4}$/;
    var that = this;
    if (!reg1.test(this.data.nameVal)) {
      my.showToast({
        content: '请输入正确的姓名',
        icon: 'none',
        duration: 2000
      })
    } else if (!reg.test(this.data.cardVal)) {
      my.showToast({
        content: '请输入正确的身份证号',
        icon: 'none',
        duration: 2000
      })
    } else {
      that.checkIDCard(that.data.cardVal)
    }
  }, 1000),
  // 一键获取手机号
  checkIDCard(idcode) {
    // 加权因子
    var that = this;
    var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    // 校验码
    var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
    var code = idcode + ''
    var last = idcode[17] //最后一位
    var seventeen = code.substring(0, 17)
    // ISO 7064:1983.MOD 11-2
    // 判断最后一位校验码是否正确
    var arr = seventeen.split('')
    var len = arr.length
    var num = 0
    for (var i = 0; i < len; i++) {
      num = num + arr[i] * weight_factor[i]
    }
    // 获取余数
    var resisue = num % 11
    var last_no = check_code[resisue]
    var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/
    // 判断格式是否正确
    var format = idcard_patter.test(idcode)
    console.log(format)
    console.log(last)
    console.log(last_no)
    // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
    if (last == last_no && format == true) {
      my.showLoading({
        content: '认证中...',
        mask: true
      })
      if (app.globalData.req) {
        let data = {
          card_id: that.data.cardVal,
          name: that.data.nameVal
        }
        app.request('/user/real-name-authentication', data, 'POST', app.globalData.req).then(res => {
          console.log(res)
          if (res.status == 200) {
            my.hideLoading()
           my.alert({
             title:'提示',
             content:'实名认证成功，祝您用车愉快',
             showCancel:false,
             success (res) {
                my.navigateBack()
            }
          })
         
          
          }
        }).catch(err => {
          console.log(err)
        })
      } else {
        my.showToast({
          content: '您的操作过于频繁，请稍后再试~',
          icon: 'none'
        })
      }
    } else {
      my.showToast({
        content: '身份证号输入有误~',
        icon: 'none'
      })
    }

  },
})