var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    smscode: '',
    second: '发送验证码',
    disabled: false,
    disabledUpdate: false
  },
  clearPhone(){
    this.setData({
      phone: ''
    });
  },
  clearCode(){
    this.setData({
      smscode: ''
    });
  },
  bindPhoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },
  bindSmscodeInput: function (e) {
    this.setData({
      smscode: e.detail.value
    });
  },
  /**
  * 用户领券
  */
  getCoupon: function () {
    var that = this;
    let regular = /^1[3|4|5|7|8]\d{9}$/;
    if (!regular.test(that.data.phone)) {
      util.showErrorToast('手机格式不正确')
      return false;
    }
    if (that.data.smscode.length == 0) {
      util.showErrorToast('验证码不能为空')
      return false;
    }

    util.request(api.newUserCoupn, {
      smscode: that.data.smscode,
      phone: that.data.phone
    }, 'POST').then(function (res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '手机号绑定成功'
        });
        wx.redirectTo({ url: '/pages/ucenter/coupon/coupon' });
      } else if (res.errno === 1) {
        util.showErrorToast(res.errmsg);
        // wx.showModal({
        //   title: '提示信息',
        //   content: res.errmsg,
        //   showCancel: false
        // });
      }
    });
  },
  /**
   * 发送短信
   */
  smscode: function () {
    var that = this;

    let regular = /^1[3|4|5|7|8]\d{9}$/;
    if (!regular.test(that.data.phone)) {
      util.showErrorToast('手机格式不正确')
      return false;
    }
    util.request(api.smscodeSend, {
      phone: that.data.phone
    }, 'POST').then(function (res) {
      let n = 59;
      var timer = setInterval(function () {
        if (n == 0) {
          clearInterval(timer);
          that.setData({
            second: '发送验证码',
            disabled: false
          });
        } else {
          that.setData({
            second: n-- +'s后重新获取',
            disabled: true
          });
        }
      }, 1000);
      if (res.errno == 0) {
        wx.showToast({
          title: '短信发送成功'
        })
      } else{
        util.showErrorToast('发送失败');
        clearInterval(timer);
        that.setData({
          second: '发送验证码',
          disabled: false
        });
        return false;
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkActivit();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkActivit() {
    let that = this;
    util.request(api.checkActivit).then(function (res) {
      if (res.errno === 2) {
        wx.showModal({
          title: '',
          content: res.errmsg,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/index/index'
              });
            }
          }
        });
        that.setData({
          disabled: true,
          disabledUpdate: true
        });
      }
    });
  }
})