//#import session.js
//#import $owl_backend_session:services/modelService.jsx
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_orgs:services/modelService.jsx
//#import $owl_org_modules:services/modelService.jsx
//#import $owl_roles:services/modelService.jsx
//#import Util.js
//#import $owlPermission:services/RolesService.jsx



function main(){
    var isid = SessionService.getSessionId(request);
    if(typeof _isid != 'undefined'){
        isid = _isid;
    }

    var now = new Date().getTime();
    var sessionId = 'owl_backend_session_' + isid;

    var session = owl_backend_sessionService.get(sessionId);

    if( session && session.sessionObjs && session.lastModified > now - 7*24*3600*1000){
        var ip = $.getClientIp();
        var userId = session.userId;

        var orgAndRole = getRequestOrgAndRole(userId);
        if(!orgAndRole || orgAndRole.length!=2 && (orgAndRole[0]==null || orgAndRole[1]==null)){
            orgAndRole = getSessionOrgAndRole(session);
        }
        if(!orgAndRole || orgAndRole.length!=2 && (orgAndRole[0]==null || orgAndRole[1]==null)){
            var ret = {
                state:"err",
                msg:"错误，没有权限。"
            };
            out.print(JSON.stringify(ret));
            return;
        }

        var user = null;
        if(userId == '0' || userId=='1' || userId=='2'){
            user = {id:'0', name:'root'};
        }
        else{
            user = owl_mall_uService.get(userId);
        }

        var currentRole = orgAndRole[1];
        var currentOrg = orgAndRole[0];

        var roles = getRoles(currentOrg.id,userId);
        var orgs = session.orgs;

        var ret = {
            state:"ok",
            currentUser: user,
            orgs:orgs,
            roles:roles,
            orgId:currentOrg.id,
            currentOrg:currentOrg,
            currentRole:currentRole,
            session:session,
            ip: ip
        };
        out.print(JSON.stringify(ret));
        return;
    }
    else {
        var ret = {
            state: "err",
            msg: 'no user'
        };
        out.print(JSON.stringify(ret));
        return;
    }
};

main();


function getSessionOrgAndRole(session){
    var currentOrg = null;
    var currentRole = null;
    if(session.orgId){
        if(session.orgId=='0'){
            currentOrg = {id:'0',name:'平台',orgType:'org',org_modules:[{id:"owl_org_modules_102","name":"组织机构"}]}
        }
        else{
            currentOrg = owl_orgsService.get(session.orgId);
            if(currentOrg){
                currentOrg = {id:currentOrg.id, name:currentOrg.name, org_modules: currentOrg.org_modules}
            }
        }

    }

    if(session.roleId){
        if(session.roleId=='0') {
            currentRole = {id:'0',name:'管理员',"orgType":"org"}
        }
        else{
            currentRole = owl_rolesService.get(session.roleId);
            if(currentRole){
                currentRole = {id:currentRole.id, name:currentRole.name};
            }
        }

    }
    return [currentOrg,currentRole];
}


function getOrgModulesInfo(org_modules){
    return org_modules.map(function(org_module){
        return owl_org_modulesService.get(org_module.id);
    });
}
function getRequestOrgAndRole(userId){
    if(!$body){
        return null;
    }
    var params = JSON.parse($body);
    var rorgId = params.rorgId;
    var currentOrg = null;
    if(rorgId=='0'){
        currentOrg = {id:'0',name:'平台',orgType:'org',org_modules:[{id:"owl_org_modules_102","name":"组织机构"}]}
    }
    else{
        currentOrg = owl_orgsService.get(rorgId);
        if(currentOrg){
            currentOrg = {id:currentOrg.id, name:currentOrg.name, org_modules:getOrgModulesInfo(currentOrg.org_modules)}
        }
    }

    if(!currentOrg){
        return null;
    }

    var roleId = params.rroleId;
    //如果没有请求rroleId，则返回null
    if(!roleId){
        return [currentOrg,null];
    }


    var roles = getRoles(currentOrg.id,userId);
    if(!roles || roles.length==0){
        return null;
    }
    var currentRole  = null;
    if(roleId){
        if(isRoleOk(roles,roleId)){
            if(roleId=='0') {
                currentRole = {id:'0',name:'管理员',"orgType":"org"}
            }
            else{
                currentRole = owl_rolesService.get(roleId);
                if(currentRole){
                    currentRole = {id:currentRole.id, name:currentRole.name};
                }
            }
        }
        else{
            currentRole =  {id:roles[0].id , name:roles[0].name};
        }
    }
    return [currentOrg, currentRole];
}