// pages/visitFind/visitFind.js
const app = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    idNumber: null
  },
  getInputValue(e) {
    this.setData({
      idNumber: e.detail.value
    })
  },
  bindNumber() {
    let id = wx.getStorageSync('openId');
    console.log(this.data.idNumber)
    if (!this.data.idNumber) {
      this.setData({
        numberNull: true
      })
    } else {
      this.setData({
        numberNull: false
      })
      if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(this.data.idNumber)) && this.data.idNumber.length != 18) {
        this.setData({
          eight: true
        })
      } else {
        this.setData({
          eight: false
        })
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        var age = myDate.getFullYear() - this.data.idNumber.substring(6, 10) - 1;
        if (this.data.idNumber.substring(10, 12) < month || this.data.idNumber.substring(10, 12) == month && this.data.idNumber.substring(12, 14) <= day) {
          age++;
        }
        console.log(age, '我是身份证的年龄')
        if (age < 60) {
          this.setData({
            wrong: true
          })
        } else {
          this.setData({
            wrong: false
          })
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.publicUrl + '/wxinfo/visitBing',
            method: "GET",
            data: {
              openId: id,
              number: this.data.idNumber
            },
            success: res => {
              if (res.data.code == 0) {
                console.log('我绑定成功了', res)
                wx.navigateTo({
                  url: '/pages/bindOld/bindOld?idNumber=' + this.data.idNumber,
                })
              } else {
                wx.showModal({
                  showCancel: false,
                  content: res.data.message,
                })
              }
            },
            complete: () => {
              wx.hideLoading()
            }
          });
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})