// pages/member/set_balance_password/set_balance_password.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    pwd: "",
    rePwd: "",
    pwdBol: true,
    rePwdBol: true,
    popupSpecs:false,
    popupSpecs2:false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this._initVerify();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  //绑定新密码的值
  pwdinp(e) {
    // let val = e.detail.value;
    // this.setData({
    //   "pwd": val.trim()
    // });
    this.setData({
      popupSpecs:true
    });
  },
  popupSpecsClose(e){
    this.setData({
      popupSpecs:false
    });
  },
  popupSpecsClose2(e){
    this.setData({
      popupSpecs2:false
    });
  },
  onPassword(e){
    this.setData({
      pwd:e.detail
    });
  },
  onPassword2(e){
    this.setData({
      rePwd:e.detail
    });
  },
  //绑定新密码的值
  rePwdinp(e) {
    // let val = e.detail.value;
    // this.setData({
    //   "rePwd": val.trim()
    // });
    this.setData({
      popupSpecs2:true
    });
  },
  //验证是否绑定手机号
  _initVerify() {
    let that = this;
    let urlStr = app.getNetAddresss("member.balance-password.is-has-password");
    urlStr += '&password=' + that.data.pwd;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (app._isTextEmpty(res.data.mobile)) {
            wx.showModal({
              title: '提示',
              content: '设置密码必须先绑定手机号',
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../../member/editmobile/editmobile'
                  });
                } else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1
                  });
                }
              }
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  pwdshow() {
    if (this.data.pwdBol) {
      this.setData({
        pwdBol: false
      });
    } else {
      this.setData({
        pwdBol: true
      });
    }
  },
  rePwdshow() {
    if (this.data.rePwdBol) {
      this.setData({
        rePwdBol: false
      });
    } else {
      this.setData({
        rePwdBol: true
      });
    }
  },
  setPwd() {
    let that = this;
    if (app._isTextEmpty(this.data.pwd)) {
      wx.showToast({
        icon: 'none',
        title: '支付密码不能为空',
        duration: 1500
      });
      return;
    }

    if (app._isTextEmpty(this.data.rePwd)) {
      wx.showToast({
        icon: 'none',
        title: '确认密码不能为空',
        duration: 1500
      });
      return;
    }

    if (this.data.pwd != this.data.rePwd) {
      wx.showToast({
        icon: 'none',
        title: '支付密码和确认密码必须相同',
        duration: 1500
      });
      return;
    }
    let urlStr = app.getNetAddresss("member.balance-password.set-password");
    urlStr += '&password=' + that.data.pwd;
    urlStr += '&confirmed=' + that.data.rePwd;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          switch (res.data.code) {
          case 1: //成功
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  //要延时执行的代码
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1500); //延迟时间
              },
            });
            break;
          case 2: //失败
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
            break;
          case 3: //手机号未绑定
            wx.showModal({
              title: '提示',
              content: res.msg,
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../../member/editmobile/editmobile'
                  });
                } else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1
                  });
                }
              }
            });
            break;
          default:
            break;
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
});
