// pages/packet/packet.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mobile      : '', //手机号码
        code        : '',
        iscode      : null,   //用于存放验证码接口里获取到的code
        codename    : '获取验证码',
        countDownNum: '',
        popContHide : false,  //领取成功弹出
        popData     : '',   //领取金额
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {

    },

    /**
     * 获取手机号码
     */
    getNameValue(e) {
        this.setData({
            mobile: e.detail.value
        })
    },

    /**
     * 获取验证码
     */
    getCodeValue(e) {
        this.setData({
            code: e.detail.value
        })
    },

    /**
     * code
     */
    codeBind(e){
        let mobile = this.data.mobile,
            myreg    = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/

        var _this    = this

        if  (mobile == "") {
            wx.showToast({
                title     : '手机号不能为空',
                icon      : 'none',
                duration  : 1000
            })
            return false;
        }else if (!myreg.test(mobile)) {
            wx.showToast({
                title     : '请输入正确的手机号',
                icon      : 'none',
                duration  : 1000
            })
            return false;
        }else{
            wx.$api.user.send(mobile,'DEFAULT','unicom').then(res=>{
                wx.showToast({
                    title   : '发送成功',
                    icon    : 'success',
                    duration: 2000
                })
                var num = 61;
                var timer = setInterval(function () {
                    num--;
                    if (num <= 0) {
                        clearInterval(timer);
                        _this.setData({
                            codename    : '重新发送',
                            disabled    : false
                        })
          
                    } else {
                        _this.setData({
                            codename    : num + "s后重新获取",
                            disabled    : true
                        })
                    }
                }, 1000)
            }).catch(err => {});
        }
    },

    /**
     * 立即领取
     */
    forgetlogin(e) {
        let mobile          = this.data.mobile,
            code            = this.data.code,
            that            = this,
            countDownNum    = 5 //获取倒计时初始值 

        that.setData({
            countDownNum: countDownNum
        })
        
        wx.$api.user.unicom(mobile,'DEFAULT',code).then(res=>{
            that.setData({
                popContHide : !this.data.popContHide,
                popData     : res.data
            })
            var timerPop = setInterval(function () {
                countDownNum--;
                that.setData({
                    countDownNum: countDownNum
                })
                if (countDownNum <= 0) {
                    clearInterval(timerPop);
                    that.setData({
                        countDownNum: 0
                    })
                    wx.switchTab({
                        url: '/pages/user/user'
                    })
                }
            }, 1000)
        }).catch(err => {});
    },

    /**
     * 关闭弹窗
     */
    popContHide() {
        this.setData({
            popContHide : !this.data.popContHide
        })
        wx.switchTab({
            url: '/pages/user/user'
        })
    }
})