const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loginName:null,
    oldPsw:'',
    newPsw:'',
    password:'',
    showOldPass:true,
    showNewPass:true,
    showPassWord:true,
    //数据加载参数
    show:false, 
  },

  onClickShow() {
    this.setData({ show: true });
  },

  onClickHide() {
    this.setData({ show: false });
  },

  seeTap1:function(){
    let that = this;
    this.setData({
      // 切换图标
      showOldPass:!that.data.showOldPass
    })
  },

  seeTap2:function(){
      let that = this;
      this.setData({
        // 切换图标
        showNewPass:!that.data.showNewPass
      })
    },

  seeTap3:function(){
        let that = this;
        this.setData({
          // 切换图标
          showPassWord:!that.data.showPassWord
        })
        if(this.data.newPsw!='' && this.data.newPsw!=this.data.password){
          app.toast("两次密码输入不一致！");
        }
      },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.onClickShow();
    var that = this;
    //获取缓存数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.onClickHide();
        that.setData({
          loginName:res.data.loginName,
        })
        if(that.data.loginName == null || that.data.loginName == ""){
          app.toast("数据异常！！");
          wx.redirectTo({
            url: '../updatePassword/updatePassword'
          }) 
        }
      }
    })
  },
  /**
   * 添加预警信息
   */
  submit:function(){
    var that = this;
    if(that.data.oldPsw == ''){
      app.toast("请输入旧密码！");
      return;
    }
    if(that.data.password == ''){
      app.toast("请确认新密码！");
      return;
    }
    if(that.data.newPsw!='' && that.data.newPsw!=that.data.password){
      app.toast("两次密码输入不一致！");
      return;
    }
    if(that.data.oldPsw.length<6){
      app.toast("请输入旧密码长度 [6-20]位字符！");
      return;
    }
    if(that.data.newPsw.length<6){
      app.toast("请输入新密码长度 [6-20]位字符！");
      return;
    }
    if(that.data.password.length<6){
      app.toast("请输入确认密码长度 [6-20]位字符！");
      return;
    }
    that.onClickShow();
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url:app.globalData.reqUrl+'/wechat/updatePassword',
      data:{
        loginName:that.data.loginName,
        oldPsw:that.data.oldPsw,
        newPsw:that.data.newPsw,
        password:that.data.password
      },
      method:"POST",
     success:function(res){
      that.onClickHide();
       if(res.data && res.data.data=="200"){
        wx.clearStorageSync();
        wx.setStorageSync('isReload', "1");
        app.toast("密码修改成功！");
          wx.redirectTo({
            url: '../login/login',
          }); 
       }else{
         app.toast(res.data.info);
         return;
       }
     }
    })
  },

  //返回到项目概况页面
  goGCLB:function(){
    let nav = wx.getStorageSync('nav-menu');
    if(nav=="xm"){
      wx.redirectTo({
        url: '../xiangmugaikuang/xiangmugaikuang'
      })
    }else if(nav=="gd"){
      wx.redirectTo({
        url: '../gengduogongneng/gengduogongneng'
      })
    }else if(nav=="aq"){
      wx.redirectTo({
        url: '../safety_manage/index'
      })
    }
  },
  //修改密码
  QRXG:function(){
    this.submit();
  }
})