//#import $owl_org_admin_groups:services/modelService.jsx
//#import $owl_platform_admin_groups:services/modelService.jsx
//#import $owl_orgs:services/modelService.jsx

function getOrgGroupsOfUser(userId){
    var searchArgs = {
        "staff.id":userId
    }
    var searchResult = owl_org_admin_groupsService.search("0",searchArgs,0,0,1000,null);
    if(searchResult && searchResult.state==='ok'){
        return searchResult.list;
    }
    else{
        return [];
    }

}

function  getPlatformGroups(userId){
    var searchArgs = {
        "staff.id":userId
    }
    var searchResult = owl_platform_admin_groupsService.search(null,searchArgs,null,0,1000,null);
    if(searchResult && searchResult.state==='ok'){
        $.log("=============getPlatformGroups state===ok, result=" + JSON.stringify(searchResult));
        return searchResult.list;
    }
    else{
        return null;
    }
}

function getOrgsOfUser(userId){
    $.log("++++++++++++getOrgsOfUser userId=" + userId);
    var orgs = [];
    var orgIds = [];
    if(userId=='0' || userId=='1' || userId=='2'){
        orgs.push({
            id:'0',
            name:'平台'
        });
        return orgs;
    }

    var platformGroups = getPlatformGroups(userId)
    if(platformGroups && platformGroups.length>0){
        // $.log("getOrgsOfUser platformGroups.length=" + platformGroups.length);
        orgIds.push('0');
        orgs.push({
            id:'0',
            name:'平台'
        })

    }
    else{
        $.log("getOrgsOfUser platformGroups.length=0");
    }
    var orgGroups = getOrgGroupsOfUser(userId);
    if(orgGroups){
        orgGroups.forEach(function(group){
            if(orgIds.indexOf(group._orgId)==-1){
                orgIds.push(group._orgId);
                var org = owl_orgsService.get(group._orgId);
                if(org){
                    orgs.push({id:org.id, name:org.name});
                }
                else{
                    $.log("org not found, orgId===" + group._orgId);
                }

            }

        })
    }


    return orgs;
}

function getParentOrgIds(orgId) {
    if (orgId == '0') {
        return ['0'];
    }

    var org = owl_orgsService.get(orgId);
    if (org) {
        return org.parentPathIds;
    }
    return [orgId];
}
function getRolesOfGroups(groups){
    var roleIds = [];
    var resultRoles = [];
    for(var i=0;i<groups.length; i++){
        var g = groups[i];
        var roles = g.roles;
        if(roles){
            roles.forEach(function(r){
                if(roleIds.indexOf(r.id)===-1){
                    roleIds.push(r.id);
                    resultRoles.push(r);
                }
            });
        }
    }
    return resultRoles;
}

function getRoles(orgId,userId) {
    //获得某个组织下，用户拥有的所有的角色
    // $.log("getRoles orgId=" + orgId + ",userId=" + userId);

    if(userId=='0' || userId=='1' || userId=='2'){
        return [{
            id:'0',
            name:'系统管理员'
        }]
    }

    if(orgId!='0'){
        var parentOrgIds = getParentOrgIds(orgId);
        var orgGroups =  getOrgGroupsOfUser(userId);
        var groups = [];
        for(var i=0;i<orgGroups.length; i++){
            var orgGroup = orgGroups[i];
            if(orgGroup._orgId==orgId || parentOrgIds.indexOf(orgGroup._orgId)>-1){
                groups.push(orgGroup);
            }
        }
        var platformGroups = getPlatformGroups(userId);
        if(platformGroups && platformGroups.length>0){
            groups = groups.concat(platformGroups);
        }
        return getRolesOfGroups(groups);
    }

    if (orgId == '0') {
        var platformGroups = getPlatformGroups(userId);
        return getRolesOfGroups(platformGroups);
    }
}

function isRoleOk(roles,roleId){
    //roleId 是否有效
    if(!roleId){
        return false;
    }
    if(!roles || roles.length===0){
        return false;
    }
    for(var i=0; i<roles.length; i++){
        var r = roles[i];
        if(r.id===roleId){
            return true;
        }
    }
    return false;
}

function isRoleOpened(orgId){
    //是否启动了角色功能
    if(orgId === '0'){
        return true;
    }
    var org = owl_orgsService.get(orgId);
    if(!org){
        return false;
    }
    if(org.role_enabled==='yes'){
        return true;
    }
    return true;
}