// pages/ucenter/accountsecurity/checkphonesecuirty/checkphonesecruity.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vcode: '',
    scene: '',
    userInfo: {},
    hidePhone: null,
    errorMsg: '',
    vcodeFlag: true,
    vcodeTimeOut: 0,
    vcodeTimeOutDefault: 0,
    vcodeLen:6,
    vcodeInterval:null,
    vcode_button_text:'获取验证码',
    reviserPasswordScene: 'MDF_PWD',
    unbindPhoneScene: 'UN_PHONE',
    showMsg: false,
    timeing: true
  },

  onError(e) {
    wx.showModal({
      title: '温馨提示',
      content: '当前网络环境较差，无法连接服务器，请稍后重试。有问题请联系客服电话4009608068，接听时段08:30-17:00。',
      showCancel: false,
      confirmColor:'#00AADD',
    });
  },
  
  inputVerificatrCode(e) {
    this.setData({
      vcode: e.detail.value,
      showMsg: false,
    });
    if(!this.msgVcode()){
      return;
    }
    this.checkAll();
  },
  msgVcode(){
    if (!this.checkVcode()) {
      this.setData({
        // errorMsg: '验证码格式不正确'
        errorMsg: '验证码格式不正确'
      });
      this.setData({
        checkCode: false
      });
      return false;
    } else {
      this.setData({
        errorMsg: ''
      });
    }
    return true;
  },

  checkAll() {
    if(!this.msgVcode()){
      return;
    }

    if (this.data.vcode) {
      this.setData({
        checkCode: true,
        // vcodeFlag: false
      });
    } else {
      this.setData({
        checkCode: false,
        // vcodeFlag: true
      });
    }
  },

  checkVcode() {

    if (this.data.vcode==undefined || this.data.vcode==""  || this.data.vcode==null) {
      return false;
    }
    if(this.data.vcode.length==this.data.vcodeLen){
      return true;
    }
    return false;
  },

  getVerificateCode(){
    let vcodeFlag = this.data.vcodeFlag;
    let phone = this.data.userInfo.phonenumber;
    let that = this;

    if (!vcodeFlag) {
      return;
    }

    this.setData({
      vcodeTimeOut:this.data.vcodeTimeOutDefault
    })

    wx.request({
      url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcodeNoUnique',
      data: {
        scene: that.data.scene,
        terminal: phone,
      },
      method: 'POST',
      success(res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '验证码已发送',
            icon: 'success',
            duration: 1000 //持续的时间
          })
          let vcodeInterval = setInterval(function () {
            if (that.data.vcodeTimeOut <= 0) {
              that.setData({
                vcodeFlag: true,
                vcode_button_text: '获取验证码',
                timeing:true
              })
              clearInterval(vcodeInterval);
            }else{
              let str = '获取验证码'+'(' + that.data.vcodeTimeOut + 's)'
              that.setData({
                vcodeTimeOut:that.data.vcodeTimeOut-1,
                vcodeFlag: false,
                vcode_button_text: str,
                timeing:false
              })
            }
          }, 1000);
        }else{
            if (res.data.code == 1) {
              if(res.data.msg!=null && res.data.msg.startsWith("503")){
                getApp().showNetworkError();
                return;
              }
              wx.showModal({
                title: '提示',
                content: '您未绑定手机号，请重新登录后自动刷新绑定信息',
                showCancel:false,
                confirmText: '去登录',
                confirmColor:'#00AADD',
                success: function (res1) {
                  if (res1.confirm) {
                    let url = '/pages/login/phone_login/phone_login';
                    wx.redirectTo({
                      url
                    })
                  }
                }
              });
          }else{
            wx.showModal({
              title: '提示',
              content:res.data.msg,
              showCancel:false,
              confirmColor:'#00AADD',
            });
          }
        }
      }
    });

  },

  nextConfirm(e){
    let that = this;

    this.checkAll();
    this.setData({
      showMsg:true
    })
    if(!this.data.checkCode){
      return;
    }

    wx.request({
      url: getApp().globalData.postHeadAgreement +'/restapi/wechat/checkPhoneSecruity',
      data: {
        phonenumber: that.data.userInfo.phonenumber,
        scene: that.data.scene,
        vcode: that.data.vcode
      },
      method: 'POST',
      success(res) {
        if (res.data.code == 1) {
          if(res.data.msg!=null && res.data.msg.startsWith("503")){
            getApp().showNetworkError();
            return;
          }
          //  that.setData({
          //    vcode: null,
          //  });
          // that.checkAll();
          wx.showModal({
            title: '提示',
            content:res.data.msg,
            showCancel:false,
            confirmColor:'#00AADD',
          });
        } else {
          if(that.data.scene==that.data.reviserPasswordScene){
            let url = `/pages/ucenter/accountsecurity/mdfpassword/mdfpassword`;
            wx.redirectTo({
              url
            });
          }else if(that.data.scene==that.data.unbindPhoneScene){
            let url = `/pages/ucenter/accountsecurity/unbindphone/unbindphone`;
            wx.redirectTo({
              url
            });
          }
        }
      },
      fail(e){
        getApp().showNetworkError();
      }
    });
  },

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

    let that = this
    wx.request({
      url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
      data: {
        scene:this.data.scene,
      },
      method: 'POST',
      success(res) {
        that.setData({
          vcodeInfo:false,
          vcodeTimeOut:res.data.result.expire,
          vcodeTimeOutDefault:res.data.result.expire,
          vcodeLen:res.data.result.len
        })
      }
    });
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      vcode: null
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})