/**
 * Created by wangmh on 2017/6/28.
 */
var app = {
    loading : function(is, position) {
        app.isLoading = is;
        toastr.clear();
        $('#loading').css('display', is ? 'block' : 'none');
        is && position && ($('#loading').addClass(position));
    },
    init : function () {
        var data = app.getParams(location.href)
        app.checkParams(data);
    },
    getParams : function(href){
        var args = href.split("?");
        if(args[0] == href){
            error("没有参数");
            return null;
        }
        var str = args[1];
        args = str.split("&");
        var data = {appId:"", t:-1, p:"", u:null, returnURL:"http://www.ubtob.com", baseURL:null};
        for(var i=0; i<args.length; i++){
            str = args[i];
            if(str.length <= 1) continue;
            var arg = str.split("=");
            data[arg[0]] = arg[1];
        }
        return data;
    },
    error : function(message, title, position) {
        toastr.clear();
        toastr.error(message, title, {
            positionClass : 'toast-' + (position || 'top-center')
        });
        window.location.href="http://www.ubtob.com";
    },
    returnHref: function () {
        var url = $('#body').val();
        window.location.href = url;
    },
    noticeOther: function (data) {
        for (var i=0;i<data.loginUrls.length;i++) {
            var loginUrl = data.loginUrls[i];
            $.ajax({
                type:"post",
                url:loginUrl,
                dataType:'jsonp',
                crossDomain: true,
                jsonp:'callback',
                jsonpCallback:"successCallback",
                data:data.data,
                success:function(json) {
                    console.log(loginUrl);
                    app.addCount(data.count);
                },
                error: function (json) {
                    console.log(loginUrl);
                    app.addCount(data.count);
                },
            });
        }
        var head = document.head || $('head')[0] || document.documentElement;
        var script = $('head').find('script')[0];
        script.onerror = function(evt) {
            app.addCount(a.count);
        }
    },
    addCount: function (count) {
        app.returnCount++;
        if (app.returnCount == app.count) {
            app.returnHref();
        }
    },
    loginOther: function (formdata, loginUrl) {
        app.loginMall(formdata, loginUrl);
    },
    loginUuzc: function (formdata, loginUrl) {
        $('#J_commenting').attr("action", loginUrl);
        $('#_username').val(formdata.username);
        $('#_password').val(formdata.password);
        $('#email').val(formdata.email);
        $('#mobile').val(formdata.mobile);
        $('#uid').val(formdata.uid);
        $('#salt').val(formdata.salt);
        console.log($('input[name="username"]').val());
        $('#J_commenting').submit();
        app.addCount(formdata.count);
    },
    loginMall: function (formdata, loginUrl) {
        var uid = formdata.uid;
        var time = formdata.time;
        var data = formdata.mallData;
        $.ajax({
            type:"post",
            url:loginUrl,
            dataType:'jsonp',
            jsonp:'callback',
            jsonpCallback:"successCallback",
            data:{
                uid: uid,
                time: time,
                data: data
            },
            success:function(json) {
                console.log("...");
                app.addCount(formdata.count);
            },
            error: function (json) {
                console.log(json);
                app.addCount(formdata.count);
            },
        });

    },
    checkParams : function(formData){
        app.loading(true);
        $.ajax('sso/login/auto', {
            data: {
                appId:formData.appId,
                tel:formData.t,
                password:formData.p,
                enuu:formData.u,
                returnUrl:formData.returnURL,
                baseUrl:formData.baseURL
            },
            dataType: 'json',
            type: 'POST',
            success: function (msg) {
                app.loading(false);
                if(msg.success) {
                    $('#body').val(msg.content.returnUrl || 'http://www.ubtob.com') ;
                    // // 为了使众创登陆成功，加了一个延迟
                    app.returnCount = 0;
                    app.count = 0;
                    app.loading(true);
                    app.noticeOther(msg.content);
                    setTimeout("app.returnHref()", 2000);
                } else if (msg.errMsg) {
                    if (formData.returnURL == "http://www.usoftmall.com/login/proxy"){
                        formData.returnURL = "http://www.usoftmall.com/logout/proxy";
                    }
                    window.location.href = formData.returnURL || 'http://www.ubtob.com';
                    // app.error(msg.errMsg);
                }
            },
            error: function (res) {
                if (formData.returnURL == "http://www.usoftmall.com/login/proxy"){
                    formData.returnURL = "http://www.usoftmall.com/logout/proxy";
                }
                window.location.href = formData.returnURL || 'http://www.ubtob.com';
                app.loading(false);
                // app.error(res);
            }
        });
    }
};
$(document).ready(function() {
    app.init();
});