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

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    isLogin: false,
    sexType: [{
      value: 1,
      text: '男'
    }, {
      value: 2,
      text: '女'
    }],
    sexIndex: null,
    phone: null,
    email: null,
    checkPass: false,
    errorMsg: ''
  },
  inputSex(e) {
    //console.log(this.data.sexType[e.detail.value]);
    this.setData({
      sexIndex: e.detail.value
    });
    this.checkAll();
  },
  inputPhone(e) {
    this.setData({
      phone: e.detail.value
    });
    this.checkAll();
  },
  inputEmail(e) {
    this.setData({
      email: e.detail.value
    });
    this.checkAll();
  },
  checkPhone() {
    if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
      return false;
    }
    return true;
  },
  checkMail() {
    if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(this.data.email))) {
      return false;
    }
    return true;
  },
  checkAll() {
    if (this.data.userInfo.sex == null || (this.data.userInfo.sex != 1 && this.data.userInfo.sex != 2)){
      if (!this.data.sexIndex) {
        this.setData({
          errorMsg: '请选择性别'
        });
        this.setData({
          checkPass: false
        });
        return;
      } else {
        this.setData({
          errorMsg: ''
        });
      }
    }
    if (!this.checkPhone()) {
      this.setData({
        errorMsg: '手机号输入有误,请重新输入'
      });
      this.setData({
        checkPass: false
      });
      return;
    } else {
      this.setData({
        errorMsg: ''
      });
    }
    if (!this.checkMail()) {
      this.setData({
        errorMsg: '邮箱输入有误,请重新输入'
      });
      this.setData({
        checkPass: false
      });
      return;
    } else {
      this.setData({
        errorMsg: ''
      });
    }
    this.setData({
      checkPass: true
    });
  },
  bindUpdateUser() {
    let that = this;
    let loginName = this.data.userInfo.loginName;
    let {
      sexIndex,
      phone,
      email
    } = this.data;
    var sex = null;
    if (sexIndex!=null){
      sex = this.data.sexType[sexIndex].value;
    }else{
      sex = this.data.userInfo.sex;
    }
    wx.request({
      url: getApp().globalData.postHeadAgreement + '/restapi/wechat/updateUser',
      data: {
        loginName,
        sex: sex,
        phonenumber: phone,
        email,
      },
      method: 'POST',
      success(res1) {
        //console.log(res1);
        //console.log(res1.data.code == 1);
        if (res1.data.code == 1) {
          that.setData({
            errorMsg: res1.data.msg
          })
        } else {
          //返回该用户
          //跳转到上一页
          wx.setStorageSync("userInfo", res1.data);
          wx.setStorageSync("isLogin", true);
          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,
        phone: userInfo.phonenumber,
        email:userInfo.email
      });
    } else {
      //未登录信息
      this.setData({
        userInfo: {}
      });
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})