CooWorkApp.factory('login', function (cooConfig) {

    var lg = {};


    lg.autoLogin = function () {

        console.log('login');

        var dtd = $.Deferred();

        var etor =
         {
             key: cooConfig.configObj.key,
             cid: cooConfig.configObj.companyId
         };


        if (cooConfig.configObj.isLogin)
        {
            dtd.resolve(true);
        }
        else if (etor.key != undefined && etor.key != '' && cooConfig.configObj.isAutoLogin) {
            //     console.log(global.configObj.url);

            $.ajax({
                url: cooConfig.configObj.url + 'Account/corlogin',
                type: "post",
                data: JSON.stringify(etor),
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {

                    console.log(data);
                    cooConfig.configObj.isLogin = true;
                    dtd.resolve(true);
                    // $("#user").html(JSON.stringify(data));            

                },
                error: function (jqXHR, exception) {
                   console.log(exception);
                }

            });
        }
        else
        {
            dtd.resolve(false);
        }

        return dtd.promise();

    };
       

    return lg;
});

 