var loginPage = new Vue({
    el: "#loginPage",
    data: {
        loginLoading: false,
        qrCodeLogin: false,
        loginAccount: '',
        loginPassword: '',
        rule:{
            account: "(^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$)|(^\\d{6,12}$)",
            password: "^[^\\u4e00-\\u9fa5]{6,30}$"
        },
        userData: {
            account: '',
            email: '',
            phone: '',
            name: '',
            userName: '',
            userId: '',
            spId: '',
            clubId: '',
            shopId: '',
            identity: '',
            permission: '',
            token: ''
        }
    },
    computed: {

    },
    methods: {
        toQrCodeLogin: function(){
            this.qrCodeLogin = true;
            $.get(spiServer + '/user/toWechatLogin').done(function(res){
                var obj = new WxLogin({
                    self_redirect: true,
                    id: "qrCodeWrap",
                    appid: res.data.AppId,
                    scope: 'snsapi_login',
                    redirect_uri: res.data.redirect_uri,
                    state: res.data.state,
                    style: "black",
                    href: "https://www-b.caimei365.com/web/login/css/login.css"
                });
            }).fail(function(err){
                console.log(err);
                alertInfo('网络连接超时，请稍后重试！');
            });
        },
        toNormalLogin: function(){
            this.qrCodeLogin = false;
        },
        showPassword: function(event) {
            var el = event.currentTarget;
            var pwdEle = $(el).siblings('input');
            if(pwdEle.attr('type') === 'password') {
                $(el).addClass('on');
                pwdEle.attr('type','text');
            } else {
                $(el).removeClass('on');
                pwdEle.attr('type','password');
            }
        },
        // 失去焦点校验
        blurHandle: function(event) {
            var el = event.currentTarget;
            verifyHandle(el);
        },
        // 提交登录
        loginSubmit: function () {
            var _self = this;
            var pass = verifyForm();
            if (this.loginLoading) { return false; }
            this.$nextTick(function(){
                if (!pass) { return false; }
                _self.loginLoading = true;
                $.post(spiServer + '/user/login',{
                    mobileOrEmail: _self.loginAccount,
                    password: _self.loginPassword,
                    source: 'www'
                }).done(function(res){
                    _self.loginLoading = false;
                    if(res.code === 0){
                        _self.userData = {
                            account: res.data.account,
                            email: res.data.email,
                            phone: res.data.bindMobile,
                            name: res.data.name,
                            userName: res.data.userName,
                            userId: res.data.userID,
                            spId: res.data.serviceProviderID,
                            clubId: res.data.clubID,
                            shopId: res.data.shopID,
                            identity: res.data.userIdentity,
                            permission: res.data.userPermission,
                            token: res.data.token
                        };
                        localStorage.setItem('userInfo',JSON.stringify(_self.userData));
                        // 登录成功页面跳转
                        /*var beforePath = localStorage.getItem("loginBeforePath");
                        if (beforePath) {
                            window.location.href = beforePath;
                        }else{
                            window.location.href = '/index.html';
                        }*/
                    } else {
                        // 登录失败
                        alertInfo(res.msg);
                    }
                }).fail(function(err){
                    console.log(err);
                    alertInfo('登录失败，请稍后重试！');
                    _self.loginLoading = false;
                });
            });
        },

    },
    created: function () {

    },
    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();
            }
        });
    }
});
