<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$

    File:           fac_functie.asp

    Description:    Ingang via functies om autorisaties te beheren
                    Ook: als je ?editmode=1 meegeeft kun je de omschrijving
                    bewerken van userfuncties

    Context:        Vanuit het menu

    Notes:
*/
%>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../api2/model_fac_functie.inc" -->
<%
    var editmode = getQParamInt("editmode", 0);
    var groep_key = getQParamInt("fac_groep", -1);
    var disc_key = getQParamInt("ins_discipline", -1);
    var canAdd = getQParamInt("canAdd", 0) == 1;
    var showAll = getQParamInt("showall", 0) == 1;
    var model = new model_fac_functie(null, { groep_key: groep_key, disc_key: disc_key });

    if (editmode == 1)
    {
        if (!user.has("WEB_FACFAC"))
        {
            var currWhere = model.gparams.GET.wheres || [];
            currWhere.push("fac_functie_code LIKE 'WEB_USER%'");

            var gparams = { "GET": { "wheres": currWhere } };
            model.REST_GET = generic_REST_GET(model, gparams);
        }
        model.REST_PUT = generic_REST_PUT(model);
    }

    settings.overrule_setting("qp_maxrows", 300); // Toon alle 255 (DB44) functies direct

    function fnrowData(oRs)
    {
      var data = { funcdisc: oRs.Fields("discipline").Value.id };
      return JSON.stringify(data);
    }

    var scf_params =
        {
            "search": {
                "autosearch": !!model.module || showAll,
                "labels" : [],
                "filters": [
                    "module",
                    "description"
                ]
            },
            "list": {
                "columns": [
                    "id",
                    //"module",
                    "name",
                    "description",
                    "info",
                    "discipline"
                ],
                "orderby": [
                    "module",
                    "sequence",
                    "name",
                    "description"
                ],
                "fnRowData": fnrowData,
                "requires": {
                  js: ["./fac_functie.js"]
                },
                "script": "window.groep_key = " + groep_key + "; window.disc_key = " + disc_key,
                "buttons": []
            },
            "edit":{
                "modal": true
            },
            "layout": {
                "miscellaneous": {
                    "icon": "fa-shield"
                }
            },
            "transit": {
                "fac_groep": groep_key,
                "ins_discipline" : disc_key,
                "editmode" : editmode
            }
        };

    if (editmode == 1)
    {
        scf_params.search.filters.shift(); // "module" filter er uit
        scf_params.list.columns.pop(); // Discipline kolom mag er nu wel tussenuit
        scf_params.search.autosearch = true;
    }
    else
    {
        scf_params.list.default_action = "edit_rechten";
    }

    if (mode == "search" && groep_key > 0) // Als er al rechten zijn, tonen we default alleen geauthoriseerde functies
    {
        var groepmodel = new model_fac_groeprechten({ disc_key: disc_key, groep_key: groep_key })
        var data = groepmodel.REST_GET({ include: ["authorization"], filter: { disc_key: disc_key, groep_key: groep_key } });

        if (data.length)
        {
            model.includes.authorization.outertoggle.def = !showAll;
            scf_params.search.autosearch = true;
        }
    }

    if (groep_key > 0 || disc_key > 0)
    {
        var groep_count = 0; // hoeveel groepen met rechten?
        scf_params.list.totalCalc = function _totalCalc(oRs)
                                    {
                                        var data = oRs.dataset[oRs.position];
                                        if (data.authorization.length > 0)
                                            groep_count++
                                    }
        scf_params.list.noSetSummary = true; // we doen het zelf
        scf_params.list.totalShow = function (column, nrlines)
        {
            if (groep_count > 0)
            {
                return "<script>"
                        + "FcltMgr.setSubtabExtra(\"{0}\", true);".format(groep_count)
                        + "FcltMgr.setHeaderExtraTitle(\"" + L("lcl_nr_lines_result").format(nrlines) + "\");"
                      + "</script>";
            }
            else
            {
                return "<script>"
                        + "FcltMgr.setSubtabExtra(\"<span class='subtab-badge-icon subtab-badge-missing'>" + I("fa-exclamation-triangle fa-lg", { "fastyle": "fas" }) + "</span>\");"
                      + "</script>";
            }
        }
    }

    if (groep_key > 0)
    {
        var groep_desc = Oracle.Get("fac_groep_omschrijving", "fac_groep", groep_key);
        scf_params.search.title = groep_desc;
        scf_params.search.labels.push({ "lbl": L("fac_groep"), "txt" : groep_desc});
        scf_params.search.labels.push({ "lbl": L("fac_groep_opmerking"), "txt" : Oracle.Get("fac_groep_opmerking", "fac_groep", groep_key)});
        scf_params.list.columns.push(
                    "authorization.discipline",
                    "authorization.basislevelread",
                    "authorization.basislevelwrite",
                    "authorization.realestatelevelread",
                    "authorization.realestatelevelwrite"
                    );
    }
    if (disc_key > 0)
    {
        scf_params.search.labels.push({ "lbl": L("mgt_discipline"), "txt" : Oracle.Get("ins_discipline_omschrijving", "ins_tab_discipline", "ins_discipline_key", disc_key)});
        scf_params.list.columns.pop(); // Discipline kolom mag er nu wel tussenuit
        scf_params.list.columns.push(
                    "authorization.authorizationgroup",
                    "authorization.basislevelread",
                    "authorization.basislevelwrite",
                    "authorization.realestatelevelread",
                    "authorization.realestatelevelwrite"
                    );
    }
    else
        scf_params.search.filters.push("name");

    if (canAdd) {
        scf_params.list.buttons.push({
            "icon": "fa-plus", "title": L("lcl_add"), "action": "FcltMgr.openDetail('appl/mgt/fac_functie.asp?fac_groep=" + groep_key + "&showall=1');", "id": "btn_scf_add"
        });
    }

    scaffolding(model, scf_params);
%>
<% ASPPAGE_END(); %>
