//#import $owl_orgs:services/modelService.jsx
//#import $owlService:services/org/OrgUtil.jsx
//#import @handlers/include/checklogin.jsx

function main() {
    var env = ctx.get("env");
    var jOrg = ctx.get("data");

    var roleId = env.roleId
    var orgId = env.orgId;
    var userId = env.loginUserId;

    if(jOrg.parent_id == "0" && jOrg._orgId != "0") {
        throw "抱歉，您无权在根目录下新增组织或商家";
    }
    if(orgId != "0" && jOrg.orgType == "org") {
        throw "抱歉，您无权新增和管理组织机构";
    }
    $.log("setIdByOrgType.jsx...begin...jOrg.id=" + jOrg.id);

    if(jOrg.parent_id != "0") {
        var jParentOrg = owl_orgsService.get(jOrg.parent_id);
        if(jParentOrg.merType == "gp") {
            jOrg.orgType = "department";
            jOrg.merType = "dp";
        }
    }

    if (!jOrg.id) {
        if (jOrg.orgType == "merchant") {
            var idNumber = owl_orgsService.getId({});
            idNumber = idNumber.replace("owl_orgs_", "");
            jOrg.id = "owl_orgs_m_" + idNumber;
        } else if (jOrg.orgType == "department") {
            var idNumber = owl_orgsService.getId({});
            idNumber = idNumber.replace("owl_orgs_", "");
            jOrg.id = "owl_orgs_dp_" + idNumber;
        }
    }

    if(jOrg.parent_id) {
        jOrg._orgId = jOrg.parent_id;
        jOrg._orgIds = OrgUtil.getSaveOrgIds(jOrg.parent_id);
    }
}

main();
