var loginPage = new Vue({
    el: "#loginPage",
    data: {
        maxtime:60,
        loginLoading: false,
        qrCodeLogin: false,
        loginAccount: '',
        activateCode: '',
        showClass:'',
        mobilCount:0,
        mobileCodeText:'获取验证码',
        mobilTime:null,
        isMobileDisabled:false,
        emailCount:0,
        emailCodeText:'获取验证码',
        emailTime:null,
        isEmailDisabled:false,
        isCodeModel:false,
        codeImagePath:'',
        codeImageToken:'',
        imageCode:'',
        isSuccess:false,
        isError:false,
        isLoginSuccess:false,
        rule:{
            phone: '^\\d{6,12}$',
            code: '^\\d{6}$',
        },
        isWeChat: false,
        maxtime:3
    },
    computed: {

    },
    methods: {
        getMobileImage:function() {//获取图形验证码
            var _self = this;
            var pass = verifyCheack('.massageBtn');
            if (_self.isMobileDisabled) { return false; }
            this.$nextTick(function() {
                if (!pass) {
                    return false;
                }
                DocumentApi.GetImgVerifyCode({},function(response){
                    if(response.code == 0){
                        _self.imageCode = '';
                        _self.codeImagePath = response.data.baseImage;
                        _self.codeImageToken = response.data.token;
                        _self.isCodeModel = true;
                    }else{
                        CAIMEI.Alert(response.msg,'确定',false);
                    }
                })
            });
        },
        getMobileCodeFn:function(){//获取登录短信验证码
            var _self = this;
            var params = {
                    mobile:_self.loginAccount,
                    imgCode:_self.imageCode,
                    token:_self.codeImageToken
            };
            _self.isMobileDisabled = true;
            DocumentApi.DocumentNote(params,function (response) {
                if(response.code == 0){
                    _self.isCodeModel = false;
                    CAIMEI.dialog('验证短信已发送',false,function () {});
                    var TIME_COUNT = 60;
                    if (!_self.mobilTime) {
                        _self.mobilCount = TIME_COUNT;
                        _self.isMobileDisabled = true;
                        _self.mobilTime = setInterval(function(){
                            if (_self.mobilCount > 1 && _self.mobilCount <= TIME_COUNT) {
                                _self.mobilCount--;
                                _self.mobileCodeText = _self.mobilCount +'s重新发送';
                            } else {
                                _self.isMobileDisabled = false;
                                clearInterval(_self.mobilTime);
                                _self.mobilTime = null;
                                _self.mobileCodeText = '获取验证码';
                            }
                        },1000)
                    }
                }else{
                    CAIMEI.Alert(response.msg,'确定',false);
                    _self.isMobileDisabled = false;
                    _self.isCodeModel = false;
                }
            })
        },
        blurHandle: function(event) {  // 失去焦点校验
            var el = event.currentTarget;
            verifyHandle(el);
        },
        refreshCodeImage:function(){
            this.getMobileImage()
        },
        closeCodeModel:function(){
            this.isCodeModel = false;
        },
        loginSubmit: function () {  // 账号登录
            var _self = this;
            var pass = verifyForm();
            if (this.loginLoading) { return false; }
            this.$nextTick(function(){
                if (!pass) { return false; }
                _self.loginLoading = true;
                var params =  {mobile: _self.loginAccount,code: _self.activateCode};
                DocumentApi.MobileCodeLogin(params,function(response){
                    _self.loginLoading = false;
                    if(response.code === 0){
                        // 登录成功页面跳转
                        _self.isLoginSuccess = true;
                        _self.isSuccess = true;
                        var timeClock = setInterval(function(){
                            _self.maxtime--;
                            if (_self.maxtime == 0) {
                                location.href = '/document/list.html';
                                clearInterval(timeClock);
                                _self.maxtime = 3;
                                _self.isRefresh = true;
                            }
                        },1000);
                    } else if(response.code == -2) {// 登录失败
                        _self.isLoginSuccess = true;
                        _self.isError = true;
                    }else{
                        CAIMEI.Alert(response.msg,'确定',false);
                    }
                });
            });
        },
        navLinkFn:function(){//跳转
            location.href = '/document/list.html';
        },
        goBackLogin:function(){
            this.isLoginSuccess = false;
            this.isError = false
        },
        setStorages:function(data){//存储本地数据
            localStorage.setItem('userInfo',JSON.stringify(data));
        }
    },
    created: function () {
        var userAgent = navigator.userAgent.toLowerCase();
        this.isWeChat = userAgent.match(/MicroMessenger/i);
        // this.weChatAutoLogin();
    },
    mounted: function () {
        var _self = this;
        $('body').on("focus",'[needverify]:visible',function(){
            $(this).siblings('.errTips').removeClass("show").siblings('.checked').removeClass("show");
        });
        //enter键登录
        $(document).keyup(function (e) {
            var e = e || event;
            e.stopPropagation();
            if (e.keyCode === 13) {
                _self.loginSubmit();
            }
        });
    }
});
