var loginPage = new Vue({
    el: "#loginPage",
    data: {
        maxtime:60,
        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(){//切换微信登录二维码
            var _self = this;
            var timeClock = null;
            this.qrCodeLogin = true;
            UserApi.ToWechatLogin({},function(response){
                var wxLogin = new WxLogin({
                        self_redirect: true,
                        id: "qrCodeWrap",
                        appid: response.data.AppId,
                        scope: 'snsapi_login',
                        redirect_uri: response.data.redirect_uri,
                        state: response.data.state,
                        style: "black",
                        href: "https://www-b.caimei365.com/web/login/css/login.css"
                });
                timeClock = setInterval(function(){
                    _self.maxtime--;
                    if (_self.maxtime == 0) {
                        clearInterval(timeClock);
                    };
                },1000);
                _self.getWrchatStatusData({uuid:response.data.state})
            });
        },
        getWrchatStatusData:function(params){//轮询查询用户是否扫描过二维码登录
            var _self = this;
            var timer = null;
            UserApi.ToWechatPolling(params,function(response){
                if(response.code == -90){
                    if(_self.maxtime == 0){
                        clearInterval(timer);
                        _self.maxtime = 60;
                        $('#qrCodeWrap').append('<div class="model"><div class="refresh-div"><img class="refresh" src="/web/login/img/shua.png" alt=""><p>二维码已失效,点击刷新</p></div></div>');
                        $('#qrCodeWrap .model').click(function () {
                            $(this).remove();
                            _self.toQrCodeLogin();
                        })
                    }else{
                        timer = setTimeout(function(){
                            _self.getWrchatStatusData(params);
                        },3000)
                    }
                }else {
                    clearTimeout(timer); //清理定时任务
                    if(response.code == 0){
                        _self.userData = {
                            account: response.data.account,
                            email: response.data.email,
                            phone: response.data.bindMobile,
                            name: response.data.name,
                            userName: response.data.userName,
                            userId: response.data.userID,
                            spId: response.data.serviceProviderID,
                            clubId: response.data.clubID,
                            shopId: response.data.shopID,
                            userIdentity: response.data.userIdentity,
                            permission: response.data.userPermission,
                            token: response.data.token
                        };
                        _self.setStorages( _self.userData);
                    }else if(response.code == -4){
                        window.location.href = 'binding.html';
                    }
                }
            });
        },
        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;
                var params =  {mobileOrEmail: _self.loginAccount,password: _self.loginPassword,source: 'www'};
                UserApi.PostLoginAccount(params,function(response){
                    _self.loginLoading = false;
                    if(response.code === 0){
                        console.log(response.data);
                        var _userIdentity = response.data.userIdentity;
                        _self.userData = {
                            account: response.data.account,
                            email: response.data.email,
                            phone: response.data.bindMobile,
                            name: response.data.name,
                            userName: response.data.userName,
                            userId: response.data.userID,
                            spId: response.data.serviceProviderID,
                            clubId: response.data.clubID,
                            shopId: response.data.shopID,
                            userIdentity: response.data.userIdentity,
                            permission: response.data.userPermission,
                            token: response.data.token
                        };
                       _self.setStorages( _self.userData);
                        // 登录成功页面跳转
                        var beforePath = localStorage.getItem("loginBeforePath");
                        if(_userIdentity == 4 || _userIdentity ==2){
                            window.location.href = '/user/dashboard.html';
                        }else if(_userIdentity == 3){
                            window.location.href = '/supplier/dashboard.html';
                        }else if (beforePath) {
                            window.location.href = beforePath;
                        }else {
                            window.location.href = '/index.html';
                        }
                    } else {// 登录失败
                        CAIMEI.Alert(response.msg,'确定',false);
                    }
                });
            });
        },
        setStorages:function(data){//存储本地数据
            localStorage.setItem('userInfo',JSON.stringify(data));
        }
    },
    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();
            }
        });
    }
});
