// pages/more/bindphone/bindphone.js
var app = getApp();
var hostname = app.globalData.hostname;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canGetCode: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  // 获取输入的值
  getKey: function(e){
    var kind = e.currentTarget.dataset.kind;
    var value = e.detail.value;
    this.setData({
      [kind]: value
    })
  },
  //获取验证码
  getCode: function(){
    var that = this;
    var canGetCode = that.data.canGetCode;
    if ( !canGetCode){
      return false;
    }
    var phone = that.data.phone;
    var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var phoneValid = reg.test(phone);
    var errorContent;
    if ( phone == undefined || phone == ''){
      errorContent = '请输入您的手机号码';
    } else if (!phoneValid){
      errorContent = '请输入正确的手机号码';
    }
    if ( errorContent != undefined ){
      wx.showModal({
        title: '温馨提示',
        content: errorContent,
        showCancel: false,
      })
      return false;
    }

    /****测试 */
    that.timeCount(that);
    that.setData({
      correctCode: '1234',
      correctPhone: '13412341234',
    })
    return false;
    /****测试 */

    wx.request({
      url: hostname + '',
      data: {phone: phone},
      method:' GET',
      dataType: 'json',
      success: function(codeRes) {
        if (codeRes.data.code == 200){
          var correctCode = codeRes.data.code;
          that.setData({
            correctCode: correctCode,
            correctPhone: phone,
          })
        }
      },
      fail: function (codeRes) {},
      complete: function (recodeRes) {},
    })
  },
  // 提交绑定
  submitBind: function(){
    var that = this;
    var name = that.data.name;
    var phone = that.data.phone;
    var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var phoneValid = reg.test(phone);
    var code = that.data.code;
    var correctCode = that.data.correctCode;
    var correctPhone = that.data.correctPhone;
    var errorContent;
    if( name == undefined || name == '' ){
      errorContent = '请输入您的真实姓名';
    } else if (phone == undefined || phone == '') {
      errorContent = '请输入您的手机号码';
    } else if (!phoneValid) {
      errorContent = '请输入正确的手机号码';
    } else if (correctCode == undefined) {
      errorContent = '请获取验证码';
    } else if (code == undefined) {
      errorContent = '请输入验证码';
    } else if (correctCode != code) {
      errorContent = '验证码错误';
    } else if (correctPhone != phone) {
      errorContent = '您修改了手机号码，请重新获取验证码';
    }
    if ( errorContent != undefined){
      wx.showModal({
        title: '温馨提示',
        content: errorContent,
        showCancel: false,
      })
      return false;
    }


    //删掉
    wx.showToast({
      title: ' 绑定成功',
      duration: 1500,
      success: function () {
        setTimeout(function () {
          wx.navigateBack({})
        }, 1500)
      }
    })
    return false;//删掉

    
    wx.request({
      url: hostname + '',
      data: {name: name, phone: phone},
      method: 'GET',
      dataType: 'json',
      success: function(submitRes) {
        if (submitRes.data.code == 200){
          wx.showToast({
            title:' 绑定成功',
            duration: 1500,
            success: function(){
              setTimeout(function(){
                wx.navigateBack({})
              },1500)
            }
          })
        }
      },
      fail: function (submitRes) {},
      complete: function (submitRes) {},
    })
  },
  //计时
  timeCount: function(that){
    that.setData({
      canGetCode: false,
    })
    var second = 10;
    var timer = function(){
      that.setData({
        secondLeft: second--
      })
      setTimeout(function(){
        if( second == 0){
          that.setData({
            canGetCode: true,
          })
          return ;
        }
        timer();
      },1000)
    }
    timer();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

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

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

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

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

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

})