// pages/ucenter/center/center.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    isLogin: false,
    isBindPhone:0,
    sexType: [
      {
        value: 0,
        text: '保密'
      },{
      value: 1,
      text: '男'
    }, {
      value: 2,
      text: '女'
    }],
    sexIndex: 0,
    carNum: null,
    email: null,
    checkPass: false,
    errorMsg: ''
  },

  inputSex(e) {
    this.setData({
      sexIndex: parseInt(e.detail.value)
    });
    let userdata= {
      sexIndex: e.detail.value,
      carNum: null,
      email: null,
    };
    this.bindUpdateUser(userdata);
  },

  inputCarNum(e){
    let that = this;
    wx.showModal({
      title: '修改车牌号',
      showCancel:true,
      editable: true,
      confirmColor:'#00AADD',
      success: function (res1) {
        if (res1.confirm) {
          let carNum = res1.content;
          that.confirmCarNum(carNum);
        }
      }
    });
  },

  confirmCarNum(carNum) {
    if (!this.checkCarNum(carNum)) {
      wx.showModal({
        title: '提示',
        showCancel:false,
        content: '车牌号输入有误，请重新输入',
        confirmColor:'#00AADD',
      });
      return;
    } else {
      this.setData({
        carNum: carNum,
        errorMsg: ''
      });
    }
    let userdata= {
      sexIndex: null,
      carNum: carNum,
      email: null,
    };
    this.bindUpdateUser(userdata);
  },

  inputEmail(e) {
    let that = this;
    wx.showModal({
      title: '修改邮箱',
      showCancel:true,
      editable: true,
      confirmColor:'#00AADD',
      success: function (res1) {
        if (res1.confirm) {
          let email = res1.content;
          that.confirmEmail(email);
        }
      }
    });
  },

  confirmEmail(email) {
    if (!this.checkMail(email)) {
      wx.showModal({
        title: '提示',
        showCancel:false,
        content: '邮箱输入有误，请重新输入',
        confirmColor:'#00AADD',
      });
      return;
    } else {
      this.setData({
        email: email,
        errorMsg: ''
      });
    }
    let userdata= {
      sexIndex: null,
      carNum: null,
      email: email
    };
    this.bindUpdateUser(userdata);
  },

  checkMail(email) {
    if(email){
      if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(email))) {
        return false;
      }
    }
    return true;
  },
  checkCarNum(carNum){
    if (carNum){
      // if (!(/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(this.data.carNum))) {
      if (!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领].*$/.test(carNum)) {
        return false;
      }
    }
    return true;
  },

  bindUpdateUser(userdata) {
    let that = this;
    let loginName = this.data.userInfo.loginName;
    let {
      sexIndex,
      carNum: license_number,
      email
    } = userdata;
    var sex = null;
    if (sexIndex!=null){
      sex = this.data.sexType[sexIndex].value;
    }

    wx.request({
      url: getApp().globalData.postHeadAgreement + '/restapi/wechat/updateUser',
      data: {
        loginName,
        sex: sex,
        license_number,
        email,
      },
      method: 'POST',
      success(res1) {
        if (res1.data.code == 1) {
          that.setData({
            errorMsg: res1.data.msg
          })
        }else if (res1.data.code == 2) {
          wx.showModal({
            title: '提示',
            content: '您未绑定手机号,请重新登录后自动刷新绑定信息',
            confirmText: '去登录',
            showCancel:false,
            confirmColor:'#00AADD',
            success: function (res1) {
              if (res1.confirm) {
                wx.removeStorageSync('userInfo');
                wx.removeStorageSync('isLogin');
                let url = '/pages/login/phone_login/phone_login';
                wx.redirectTo({
                  url
                })
              }
            }
          });
        } else {
          //返回该用户
          //跳转到上一页
          wx.setStorageSync("userInfo", res1.data);
          wx.setStorageSync("isLogin", true);
          let userInfo = wx.getStorageSync('userInfo');
          console.info("fffffffffffff")
          console.info(userInfo)
          that.setData({
            userInfo: userInfo
          });

    
          // wx.navigateBack();
          //let url = `/pages/ucenter/index/index`;
          //wx.redirectTo({
          //url
          //});
        }
      }
    });


  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');

    // 页面显示
    if (userInfo && isLogin) {
      //userInfo.flag = true;
      console.log(userInfo);
      this.setData({
        userInfo: userInfo,
        isLogin: isLogin,
        isBindPhone: userInfo.bindingPhone,
        carNum: userInfo.license_number,
        email:userInfo.email,
        sexIndex:userInfo.sex
      });


    } else {
      //未登录信息
      this.setData({
        userInfo: {}
      });
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})