// pages/updateInfo/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginStatus: wx.getStorageSync('loginStatus') ? wx.getStorageSync('loginStatus') : false,
    showModal: false,
    isSubmitting:false,
    defaultText: '请选择',
    userId:'',
    name:'',
    phone:'',
    licensePlate: '',//用于提交
    vehicleNo: {},//默认显示值
  },
  //输入框焦点聚焦时隐藏车牌号输入框
  hideKeybord: function () {
    this.setData({
      isKeyboard: false,
      inputOnFocusIndex: ''
    })
  },
  //获取车牌号
  licensePlate: function (e) {
    this.setData({
      licensePlate: e.detail.carNum
    })
  },
  //下拉选择
  bindPickerChange: function (e) { //下拉菜单
    switch (e.target.dataset.pickertype) {
      case 'outIn': //出入库
        app.util.getPickerIndex(this, 'outInIndex', e);
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let getCarNumArr = wx.getStorageSync('nickname')?wx.getStorageSync('nickname').split(''):''
    let inputPlates = {};
    for (let i = 0; i < getCarNumArr.length; i++) {
      inputPlates['index' + i] = getCarNumArr[i]
    }
    this.setData({
      loginStatus: wx.getStorageSync('loginStatus') ? wx.getStorageSync('loginStatus') : false,
      userId:wx.getStorageSync('id'),
      name:wx.getStorageSync('name'),
      phone:wx.getStorageSync('phone'),
      inputPlates: inputPlates,
      isNewEnergy: getCarNumArr.length > 7 ? true : false,
      licensePlate: wx.getStorageSync('nickname'),
    })
  },
  //提交数据
  formSubmit: function (e) {
    let formData = e.detail.value;
    let reg = /^1[3-9]\d{9}$/;
    var xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
    var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
    var warn = ""; //弹框时提示的内容
    if (formData.name == '') {
      warn = "请输入司机姓名！";
    }else if (formData.phone == '') {
      warn = "请输入司机手机号！";
    } else if(!reg.test(formData.phone)){
      warn = "请确认手机号格式！";
    } else if (formData.nickname == '') {
      warn = "请选择车牌！";
    } else if (formData.nickname.length < 7) {
      warn = "请选择完整车牌号！";
    } else if (formData.nickname.length == 7 && !creg.test(formData.nickname)) {
      warn = "车牌号格式错误！";
    } else if (formData.nickname.length == 8 && !xreg.test(formData.nickname)) {
      warn = "车牌号格式错误！";
    }
    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.updateUserInfo,
        params: formData,
        page: this,
        isLoadingTxt: '处理中...',
        isSubmitting: true,
        successFun: true
      }).then(res => {
        let _this = this
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1500,
          mask:true,
          complete: function () {
            setTimeout( ()=> {
              wx.setStorageSync('name', formData.name); //用户姓名
              wx.setStorageSync('nickname', formData.nickname); //车牌号
              wx.navigateBack()
            }, 1500) //延迟时间  
          }
        })
      })


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})