// pages/ucenter/accountsecurity/mdfpassword/mdfpassword.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    password: '',
    rePassword: '',
    checkPass: false,
    errorMsg: '',
    showMsg: false
  },

  resetPassword(e) {
    let that = this;
    this.checkAll();
    this.setData({
      showMsg:true
    })
    if(!this.data.checkPass){
      return;
    }
    let{
      phone : phonenumber,
      password
    } = this.data;
    wx.request({
      url: getApp().globalData.postHeadAgreement + '/restapi/wechat/resetPassword',
      data: {
        phonenumber,
        password
      },
      method: 'POST',
      success(res) {
        if (res.data.code == 1) {
          if(res.data.msg!=null && res.data.msg.startsWith("503")){
            getApp().showNetworkError();
            return;
          }
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            confirmColor:'#00AADD',
          });
        }else if (res.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 {
          let url = `/pages/ucenter/index/index`;
          wx.redirectTo({
            url
          });
        }
      }
    });
  },
  inputPassword(e) {
    this.setData({
      password: e.detail.value,
      showMsg: false
    });
    this.checkAll();
  },
  inputRepassword(e) {
    this.setData({
      rePassword: e.detail.value,
      showMsg: false
    });
    this.checkAll();
  },
  checkPassword() {
    if (!this.data.password || !this.data.rePassword || this.data.password != this.data.rePassword) {
      return false;
    }
    return true;
  },
  checkAll() {
    if ((this.data.password.length < 5 || this.data.password.length > 20)) {
      this.setData({
        errorMsg: '密码长度应为5-20',
        showMsg: false
      });
      this.setData({
        checkPass: false
      });
      return;
    }
    if (!this.checkPassword()) {
      this.setData({
        errorMsg: '两次密码输入不一致，请重新输入',
        showMsg: false
      });
      this.setData({
        checkPass: false
      });
      return;
    } else {
      this.setData({
        errorMsg: ''
      });
    }
    this.setData({
      checkPass: true
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');
    // 页面显示
    if (userInfo && isLogin) {
      this.setData({
        phone: userInfo.phonenumber,
      });
    } else {
      //未登录信息
      this.setData({
        userInfo: {}
      });
      let url = `/pages/login/phone_login/phone_login`;
      wx.navigateTo({
        url
      });
      return;
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})