// pages/login/activate/activate.js

let app = getApp()

let codeClock = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    code: '',
    password: '',
    codeTime: 60,
    activateState: false
  },

  /**
   * 输入框输入
   */
  bindInput: function (e) {
    // 输入数据绑定
    let item = e.currentTarget.dataset.item
    this.setData({
      [item]: e.detail.value
    })
    // 判断提交状态
    let activateState = false
    if (this.data.account !== '' && this.data.code !== '' && this.data.password !== '') {
      activateState = true
    }
    this.setData({
      activateState: activateState
    })
  },

  /**
   * 获取验证码
   */
  getCode: function () {
    if (this.data.account === '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 1000
      })
    } else {
      if (this.data.codeTime === 60) {
        app.$api('miniProgram_customer_AccountActivationSendCode', {
          account: this.data.account
        }).then(res => {
          codeClock = setInterval(() => {
            if (this.data.codeTime > 1) {
              this.setData({
                codeTime: this.data.codeTime - 1
              })
            } else {
              clearInterval(codeClock)
              this.setData({
                codeTime: 60
              })
            }
          }, 1000)
        }).catch(err => {})
      }
    }
  },

  /**
   * 账号激活
   */
  activateOk: function () {
    if (this.data.activateState === true) {
      app.$api('miniProgram_customer_AccountActivation', {
        account: this.data.account,
        mobileNumber: this.data.account,
        sendcode: this.data.code,
        password: this.data.password
      }).then(res => {
        wx.navigateBack({
          delta: 1
        })
      }).catch(err => {})
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff',
      animation: {}
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})