const { api, alertTip } = require('../../lib/util.js')
var t = {}
Page({
  data: {
    userId: null,
    inputPhone:null,
    inputMeg:null,
    bindLoading:true,
    msgLoading:false,
    agree:false,
    setTimeSec: 60,
    limitSec:60,
    phoneNum: null,//输入的号码
  },
  onLoad: function (query) {
    if(!query.userId) return 
    this.setData({
      userId:query.userId
    })
    console.log(this.data.userId)
  },
  onShow: function () {
    if (!this.data.userId) {
      wx.reLaunch({
        url: '/pages/welcome/welcome',
      })
    }
  },
  phoneInput(e){
    this.setData({
      inputPhone: e.detail.value
    })
    if (this.canBind(false)) {
      this.setData({
        bindLoading: false
      })
    }else{
      this.setData({
        bindLoading: true
      })
    }
  },
  getMsg() {
    let inputPhone = this.data.inputPhone
    let _this=this
    console.log(inputPhone)
    
    if (!/^1\d{10}/i.test(inputPhone)){
      alertTip('请输入正确的手机号！')
      return
    }

    _this.timer()
    //请求验证码api
    wx.request({
      url: api('api/user/phoneMsgVerify'),
      data: { userPhone:inputPhone,userId:_this.data.userId },
      method: 'POST',
      success: function (res) {
        if (res.statusCode != 200) {
          alertTip('获取验证码请求失败')
          clearInterval(t)
          _this.setData({
            msgLoading: false
          })
          return
        }
        console.log(res.data)

      },
      fail: function (res) {
        alertTip('获取验证码请求fail')
        clearInterval(t)
        _this.setData({
          msgLoading: false
        })
      }
    })
  },
  msgInput(e){
    this.setData({
      inputMeg: e.detail.value
    })
    if (this.canBind(false)) {
      this.setData({
        bindLoading: false
      })
    } else {
      this.setData({
        bindLoading: true
      })
    }
  },
  hascheck(e) {
    let agree = e.detail.value.find((value, index, arr) => {
      return value == 'agree';
    })
    this.setData({
      agree:!!agree
    })
    if (this.canBind(false)) {
      this.setData({
        bindLoading: false
      })
    } else {
      this.setData({
        bindLoading: true
      })
    }
  },
  toProtocol: function () {
    wx.navigateTo({
      url: '/pages/agreement_use/agreement_use',
    })
  },
  bindPhone(){
    let This=this
    //绑定手机号
    wx.showLoading({
      mask:true
    })
    wx.request({
      url: api('api/user/register'),
      data: { 
        userPhone: This.data.inputPhone,
        phoneMessage: This.data.inputMeg,
        userId:this.data.userId
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading()
        if (res.statusCode != 200) {
          alertTip('绑定手机号请求失败')
          return
        }
        if (!This.canBind(true)){
          return
        }
        console.log(res.data)
        //返回结果判读
        if(!res.data.ok){
          alertTip(res.data.msg)
          return
        }
        //进入绑定页面
        wx.redirectTo({
          url: '/pages/device_bind/device_bind',
        })
        return
      },
      fail: function (res) {
        wx.hideLoading()
        alertTip('绑定手机号请求fail')
      }
    })
  },
  canBind(willTip){
    if (!/^1\d{10}/i.test(this.data.inputPhone)) {
      willTip&&alertTip('请输入正确的手机号！')
      return false
    }
    if (!/\d{6}/i.test(this.data.inputMeg)) {
      willTip &&alertTip('请输入正确的验证码！')
      return false
    }
    if (!this.data.agree) {
      willTip &&alertTip('请同意使用协议！')
      return false
    }
    return true
  },
  // 重发验证码定时器
  timer: function () {
    var count = this.data.setTimeSec
    var that = this
    that.setData({
      limitSec:count,
      msgLoading: true
    })
    clearInterval(t)
    t = setInterval(function () {
      if (count != 0) {
        count--
        that.setData({
          limitSec: count
        })
      } else {
        clearInterval(t)
        that.setData({
          limitSec: count,
          msgLoading: false
        })
      }
    }, 1000)
  }
})