var app = getApp();
const util = require('../../utils/utils.js');
let timer1 = null;
Page({
  data: {
    smsVal: '', //输入的短信验证码
    phoneVal: '', //输入的手机号码
    code: '获取验证码', //获取短信验证码
    currentTime: 60, //倒计时秒数
    codeDisabled: false, //是否禁用按钮
    timer: null, //定时器
  },
  onLoad: function (options) {

  }, 
  //验证手机号
  ckPhone(e) {
    this.setData({
      phoneVal: e.detail.value
    })
  },
  //验证短信验证码
  ckSms(e) {
    this.setData({
      smsVal: e.detail.value
    })
  },
  getCode: util.throttle(function () {
    let self = this
    let currentTime = self.data.currentTime
    let timer = self.data.timer
    if (self.data.phoneVal.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
    } else {
      // 调用短信验证码接口
      let data = {
        mobile: this.data.phoneVal,
        type:2
      }
      app.request('/verification-code', data, 'POST').then(res => {
        console.log(res)
        if (res.statusCode == 200) {
          if (!timer) {
            timer = setInterval(() => {
              if (currentTime > 0 && currentTime <= 60) {
                currentTime--;
              }
              if (currentTime !== 0) {
                self.setData({
                  code: "重新发送" + "(" + currentTime + ")",
                  codeDisabled: true,
                })
              } else {
                clearInterval(timer);
                self.setData({
                  code: '获取验证码',
                  codeDisabled: false,
                  currentTime: 60,
                  timer: null,
                })
              }
            }, 1000)
          }
        }
      })
      //60秒倒计时
      
    }
  }, 1000),
  verify: util.throttle(function () {
    let reg = 11 && /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
    let reg1 = /^\d{4}$/
    if (!reg1.test(this.data.smsVal)) {
      wx.showToast({
        title: '请输入短信验证码',
        icon: 'none',
        duration: 2000
      })
    }
    if (!reg.test(this.data.phoneVal)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if (!this.data.smsVal) {
      wx.showToast({
        title: '短信验证码不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    } else {
      if (app.globalData.req) {
        let data = {
          mobile: this.data.phoneVal,
          code: this.data.smsVal
        }
        app.request('/user/bind-mobile', data, 'POST', app.globalData.req).then(res => {
          if (res.statusCode == 200) {
            wx.showToast({
              title: '换绑成功',
              icon:'none'
            })
          }
          if (res.statusCode == 422) {
            console.log(res)
            wx.showToast({
              title: res.data.errors.mobile[0],
              icon: 'none',
              duration: 2000
            })
          }
        })
      } else {
        wx.showToast({
          title: '您的操作过于频繁，请稍后再试~',
          icon: 'none'
        })
      }
    }
  }, 1000),
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})