// pages/mine/setting/inentity/index.js
var common = require('../../../utils/common');
import HTTP from "../../../../../requestFn/Api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    real_name: "",
    id_number: "",
    id_type: "",
    array: [{
      name: "身份证",
      value: 1
    }, {
      name: "护照",
      value: 2
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onShow() {
    this.getUserInfo();
  },
  async getUserInfo() {
    const {real_name,id_type,id_number} = await HTTP.GetUserInfo();
    this.setData({
      real_name,
      id_type:id_type-1,
      id_number,
    })
  },
  getName(e) {
    const that = this;
    that.setData({
      real_name: e.detail.value
    })
  },
  getNumber(e) {
    const that = this;
    that.setData({
      id_number: e.detail.value
    })
  },
  real_name_auth() {
    const that = this;
    const user = that.data;
    if (user.real_name == "") {
      wx.showToast({
        title: '请输入真实姓名',
        icon: "error",
      })
      return false
    }
    if (user.id_type === "") {
      wx.showToast({
        title: '请选择证件类型',
        icon: "error",
      })
      return false
    }
    if (user.id_number== "") {
      wx.showToast({
        title: '请输入证件号码',
        icon: "error",
      })
      return false
    }
    if (!common.identityCodeValid(user.id_number) == true) {
      wx.showToast({
        title: '身份证号码有误',
        duration: 2000,
        icon: "error",
      })
      return false
    }
    const {
      id_number,
      id_type,
      real_name
    } = user
    HTTP.UserRealName({
      id_type: id_type - 0 +1,
      id_number,
      real_name
    }).then(res => {
      wx.showToast({
        title: '设置成功',
        icon: "success",
        success() {
          setTimeout(() => {
            wx.navigateBack({
              detail: 1
            })
          }, 2000)
        }
      })
    })
  },
  bindPickerChange(e) {
    this.setData({
      id_type: e.detail.value,
    });
  },
})