let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    getCodeTime: 40, //发送短信的间隔时间
    verifyCodeTime: '获取验证码',
  },

  getmobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  bSubmit: function (e) {
    var that = this,
      mobile = e.detail.value.mobile,
      code = e.detail.value.code,
      password = e.detail.value.password,
      regMobile = /^[1]([3-9])[0-9]{9}$/
    if (!mobile) {
      wx.showToast({
        title: '请先填写手机号',
        duration: 3000,
        icon: 'none'
      })
      that.setData({
        mobilefocus: true
      })
      return false
    }
    if (!regMobile.test(mobile)) {
      wx.showToast({
        title: '手机号错误',
        duration: 3000,
        icon: 'none'
      })
      that.setData({
        mobilefocus: true
      })
      return false
    }
    if (!code) {
      that.setData({
        codefocus: true
      })
      return false
    }
    if (!password) {
      that.setData({
        passwordfocus: true
      })
      return false
    }
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/password/forgot', {
      mobile,
      code,
      password
    }, function (res) {
      if (res.code == 0) {
        wx.showModal({
          confirmText: '好的',
          content: '密码已修改，请重新登录',
          showCancel: false,
          success(res) {
            wx.clearStorageSync()
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }
        })
      }
    })
  },

  /**
   * 执行发送验证码
   */
  verifyCodeEvent: function (e) {
    var that = this
    //验证手机号
    if (that.data.buttonDisable == true) return false
    var mobile = that.data.mobile,
      regMobile = /^[1]([3-9])[0-9]{9}$/
    if (!regMobile.test(mobile)) {
      wx.showToast({
        title: '手机号错误',
        duration: 3000,
        icon: 'none'
      })
      that.setData({
        mobilefocus: true
      })
      return false
    }
    //验证手机号 END
    wx.showLoading();
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      url: app.DOMAIN_NAME + '/password/send_mobile',
      data: {
        mobile: mobile
      },
      success: res => {
        if (res.data.code == 0) {
          wx.hideLoading()
          that.Countdown()
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail: () => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 发送短信倒计时,防止恶意刷短信
   */
  Countdown: function () {
    var that = this;
    that.setData({
      buttonDisable: true
    });
    var getCodeTime = wx.getStorageSync('getCodeTime');
    var intervalId = setInterval(function () {
      getCodeTime--;
      wx.setStorageSync('getCodeTime', getCodeTime);
      that.setData({
        verifyCodeTime: '重新获取' + '(' + getCodeTime + 's' + ')'
      });
      //倒计时完成
      if (getCodeTime == 0) {
        wx.setStorageSync('getCodeTime', that.data.getCodeTime);
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    //限制发送验证码的时间，防止恶意刷短信
    var getCodeTime = wx.getStorageSync('getCodeTime');
    if (getCodeTime <= that.data.getCodeTime - 1 && getCodeTime > 0) {
      that.Countdown(that);
    } else {
      wx.setStorageSync('getCodeTime', that.data.getCodeTime);
      that.setData({
        buttonDisable: false
      });
    }
  },

  /**
   * 授权成功 跳转回原页面
   */
  navigateBack: function () {
    wx.navigateBack();
    // let currentPage = wx.getStorageSync('currentPage');
    // wx.redirectTo({
    //   url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
    // });
  },

  navSelect() {
    let that = this
    wx.navigateTo({
      url: '../enterpriseSelect/enterpriseSelect',
      events: {
        acceptDataFromOpenedPage: function (data) { //由子页面触发
          that.setData({
            customer: data.data
          })
        },
      }
    })
  }
})