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

    File:               fac_edit_groeprechten.asp
    Description:        Toon een dialoog om rechten te bewerken
    Parameters:
        fac_groep
        fac_functie
        ins_discipline
    Context:            Popup vanuit fac_groep/fac_functie/ins_tab_discipline
    Note:
*/
 %>
<!-- #include file="../scf/scaffolding.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../api2/model_fac_groeprechten.inc" -->
<!-- #include file="../Shared/audithistory.inc" -->

<%
var groep_key = getQParamInt("fac_groep");
var functie_key = getQParamInt("fac_functie");

user.checkAutorisation("WEB_PRSSYS");

var sql = "SELECT fac_functie_code"
        + "     , lcl.l(fac_functie_omschrijving, 1) fac_functie_omschrijving"
        + "     , lcl.l(fac_functie_info, 1) fac_functie_info"
        + "     , fac_functie_discipline"
        + "     , fac_functie_min_level "
        + "  FROM fac_functie"
        + " WHERE fac_functie_key = " + functie_key;
var oRs = Oracle.Execute(sql);
var flevel = oRs("fac_functie_min_level").Value;
var fdisc = oRs("fac_functie_discipline").Value;
if (fdisc)
    var disc_arr_key = getQParamIntArray("ins_discipline");
var fdesc = oRs("fac_functie_omschrijving").Value;
var finfo = oRs("fac_functie_info").Value;
var fcode = oRs("fac_functie_code").Value;
oRs.Close();

var sql = "SELECT * "
        + "  FROM fac_groep"
        + " WHERE fac_groep_key = " + groep_key;
var oRs = Oracle.Execute(sql);
var gdesc = oRs("fac_groep_omschrijving").Value;
var gremark = oRs("fac_groep_opmerking").Value;
oRs.Close();

var transitParam = buildTransitParam(["fac_groep", "fac_functie", "ins_discipline"]);

var prsread = 9;
var prswrite = 9;
var algread = 9;
var algwrite = 9;
var sql = "SELECT *"
        + "  FROM fac_groeprechten"
        + " WHERE fac_groep_key = " + groep_key
        + "   AND fac_functie_key = " + functie_key;
if (fdisc)
    sql += " AND ins_discipline_key = " + disc_arr_key[0];
var oRs = Oracle.Execute(sql);
if (!oRs.Eof)
{
    var prsread = oRs("fac_gebruiker_prs_level_read").Value;
    var prswrite = oRs("fac_gebruiker_prs_level_write").Value;
    var algread = oRs("fac_gebruiker_alg_level_read").Value;
    var algwrite = oRs("fac_gebruiker_alg_level_write").Value;
}
oRs.Close();


function radioTD(name, LOV, val)
{
    Response.Write("\n<td style='width:130px'>");
    for (var i in LOV)
    {
        Response.Write('\n<label for="' + name + (i+1) + '"><input id="' + name + (i+1) + '" name="' + name + '" class="' + name + '" type="radio" value="' + i + '"'
                      + (i == val ? ' checked' : '')
                      + '>');
        Response.Write(safe.html(LOV[i]) + "</label><br>");

    }
    Response.Write("</td>");
}
%>
<html>
  <head>
<%  FCLTHeader.Generate()
%>
    <script>
      async function grp_submit()
      {
        if (!await validateForm("u2"))
          return false;

        $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize(), FcltCallbackClose, "json");
        return true; // disable button
      }
      function auth_advanced(adv)
      {
        window.location.href = "fac_edit_groeprechten.asp?advanced=" + adv + "<%=transitParam%>";
      }
      function onchange ()
        {
            var val = $(this).val();
            switch (this.name)
            {
                case "allread":
                    if ($(".allwrite:checked").val() < val)
                        $(".allwrite[value="+val+"]").prop('checked', true); // FireFox doet hier niets als ik #prswrite gebruik?
                    break;
                case "allwrite":
                    if ($(".allread:checked").val() > val)
                        $(".allread[value="+val+"]").prop('checked', true);
                    break;
                case "prsread":
                    if ($(".prswrite:checked").val() < val)
                        $(".prswrite[value="+val+"]").prop('checked', true); // FireFox doet hier niets als ik #prswrite gebruik?
                    if (val == 9)
                        $(".algread[value=9], .algwrite[value=9]").prop('checked', true);
                    if (val < 9)
                    {
                        var algread = $(".algread:checked").val();
                        if (algread == 9)
                            $(".algread[value=-1]").prop('checked', true);
                    }
                    break;
                case "prswrite":
                    if ($(".prsread:checked").val() > val)
                    {
                        var prs = $(".prsread[value="+val+"]").prop('checked', true);
                        onchange.apply(prs[0]);
                    }
                    if (val < 9)
                    {
                        var algwrite = $(".algwrite:checked").val();
                        if (algwrite == 9)
                        {
                            var algread = $(".algread:checked").val()
                            var alg = $(".algwrite[value=" + algread + "]").prop('checked', true);
                            onchange.apply(alg[0]);
                        }
                    }
                    else
                        $(".algwrite[value=9]").prop('checked', true);
                    break;
                case "algread":
                    if (val < 9)
                    {
                        var prsread = $(".prsread:checked").val();
                        if (prsread == 9)
                            $(".prsread[value=-1]").prop('checked', true);
                    }
                    if ($(".algwrite:checked").val() < val)
                        $(".algwrite[value="+val+"]").prop('checked', true);
                    if (val == 9)
                        $(".prsread[value=9], .prswrite[value=9]").prop('checked', true);
                    break;
                case "algwrite":
                    if ($(".algread:checked").val() > val)
                    {
                        var alg = $(".algread[value="+val+"]").prop('checked', true);
                        onchange.apply(alg[0]);
                    }
                    if (val < 9)
                    {
                        var prswrite = $(".prswrite:checked").val();
                        if (prswrite == 9)
                        {
                            var prsread = $(".prsread:checked").val()
                            var prs = $(".prswrite[value=" + prsread + "]").prop('checked', true);
                            onchange.apply(prs[0]);
                        }
                    }
                    else
                        $(".prswrite[value=9]").prop('checked', true);
                    break;
            }

        }
      $(function ()
        {   // Zorg dat er altijd zinvolle combinaties overblijven
            $(".prsread, .prswrite, .algread, .algwrite, .allread, .allwrite").change(onchange)
        }
      )
    </script>
  </head>
  <body class="fclt-modal" id="mod_grprecht">
      <% MODAL_START(); %>
    <form name="u2" action="fac_edit_groeprechten_save.asp?x=x<%=transitParam%>" method="post">
<%  MODAL_BLOCK_START("grpRechten", L("lcl_auth_header"), { "icon": "fa-shield-check" });

        ROFIELDTR("fldtxt", L("fac_groep"), gdesc);
        ROFIELDTR("fldtxt", L("fac_groep_opmerking"), gremark, { suppressEmpty: true} );
        ROFIELDTR("fldtxt", L("fac_functie"), "{0} ({1})".format(fdesc, fcode));
        ROFIELDTR("fldtxt", L("fac_functie_info"), finfo);
        if (fdisc)
        {
            if (disc_arr_key.length == 1)
                ROFIELDTR("fldtxt", L("mgt_discipline"), Oracle.Get("ins_discipline_omschrijving", "ins_tab_discipline", "ins_discipline_key", disc_arr_key[0]));
            else
                ROFIELDTR("fldtxt", L("mgt_discipline"), "(" + disc_arr_key.length + ")");
        }
        var prsLOV = api2.splitLOV(buildPrsLevelLOV());
        var algLOV = api2.splitLOV(buildAlgLevelLOV());
        var jnLOV = { "-1": L("lcl_Yes"), "9": L("lcl_No") };

        //  0 is Ja/Nee
        //  1 is Ja/Nee (alleen FAC_FAQUSE en PRS_MODAAN)
        //  3 is Lees Ja/Nee en Schrijf Ja/Nee
        //  7 is PRS lezen/schrijven WEB_FACMAN en WEB_BEZPAF
        // 11 is ALG lezen/schrijven diversen
        // 13 is PRS+ALG lezen WEB_???BAC
        // 15 is PRS+ALG lezen/schrijven diversen
        // Ofwel: +1 is bedacht als afzonderlijke leesrechten. Die is wat onzinnig
        //        +2 is afzonderlijke schrijfrechten
        //        +4 is PRS-scope
        //        +8 is ALG-scope
        if (flevel & (4 | 8))
            var advanced = getQParamInt("advanced", S("auth_edit_advanced")?1:0) == 1;
        else
            var advanced = false;

        if (   prsread != algread
            || prswrite != algwrite
            || prsread != -1 && prsread != 9
            || algread != -1 && algread != 9
           )
        {
            advanced = true;
            var no_simpel = true;
        }

        if (!advanced)
        {
            Response.Write("<input type='hidden' name='simple_mode' value='1'>");
            if (flevel > 1)
                ROFIELDTR("fldtxt", L("fac_functie_min_level"), api2.splitLOV(L("fac_functie_min_levelLOV"))[flevel]);
            Response.Write("<tr><td></td><td><table>");
            if (flevel & 2)
                Response.Write("<tr><th>" + L("fac_gebruiker_lezen") + "</th><th>" + L("fac_gebruiker_schrijven") + "</th></tr>");
            else
                Response.Write("<tr><th>" + L("fac_gebruiker_toegang") + "</th></tr>");
            radioTD("allread", jnLOV, algread)
            if (flevel & 2)
                radioTD("allwrite", jnLOV, algwrite)
            Response.Write("</tr></table></td></tr>");
        }
        else
        {
            if (flevel == 15)
                Response.Write("<tr><td colspan='2'><table><tr>");
            else
                Response.Write("<tr><td></td><td><table><tr>");


            // Advanced dus in ieder geval scope kopjes
            Response.Write("<tr>");
            var spanner = "";
            if (flevel & 2) // read/write kopjes
                spanner = " colspan='2' style='text-align:center'";
            if (flevel & 4)
                Response.Write("<th" + spanner + ">" + L("lcl_prs_organisatie") + "</th>");
            if (flevel & 8)
                Response.Write("<th" + spanner + ">" + L("lcl_place") + "</th>");
            Response.Write("</tr>");

            if (flevel & 2) // read/write kopjes
            {
                Response.Write("<tr>");
                Response.Write("<th>" + L("fac_gebruiker_lezen") + "</th><th>" + L("fac_gebruiker_schrijven") + "</th>");
                if ((flevel & 4) && (flevel & 8))
                    Response.Write("<th>" + L("fac_gebruiker_lezen") + "</th><th>" + L("fac_gebruiker_schrijven") + "</th>");
                Response.Write("</tr>");
            }

            if (flevel & 4) // PRS
            {
                radioTD("prsread", prsLOV, prsread);
                if (flevel & 2) // Schrijfrechten
                    radioTD("prswrite", prsLOV, prswrite);
            }
            if (flevel & 8) // ALG
            {
                radioTD("algread", algLOV, algread);
                if (flevel & 2) // Schrijfrechten
                    radioTD("algwrite", algLOV, algwrite);
            }
            Response.Write("</tr></table></td></tr>");
        }
    MODAL_BLOCK_END();
    buttons = [];
    buttons.push({ title: L("lcl_submit"), action: "grp_submit()", importance: 1, icon: "fa-fclt-save" });
    buttons.push({ title: L("lcl_cancel"), action: "gen_cancel()", importance: 3, icon: "fa-fclt-cancel"});
    if (flevel & (4 | 8))
    {
        if (advanced)
        {
            if (!no_simpel)
                buttons.push ( {title: L("lcl_auth_simple"),  action:"auth_advanced(0)", importance: 2, icon: "fa-chevron-left" } );
        }
        else
            buttons.push ( {title: L("lcl_auth_advanced"),  action:"auth_advanced(1)", importance: 2, icon: "fa-chevron-right"  } );
    }


    var tablekeyArray = [];
    if (groep_key > 0 && functie_key > 0)
    {
      var sql = "SELECT fac_groeprechten_key"
              + "  FROM fac_groeprechten"
              + " WHERE fac_groep_key = " + groep_key
              + "   AND fac_functie_key = " + functie_key
              + (fdisc
                ? " AND ins_discipline_key IN (" + disc_arr_key.join(",") + ")"
                : "");
      var oRs = Oracle.Execute(sql);
      while (!oRs.Eof)
      {
        tablekeyArray.push(oRs("fac_groeprechten_key").Value);
        oRs.Movenext();
      }

      var tablerefArray = [{field: "fac_groep_key", fieldkey: groep_key},
                           {field: "fac_functie_key", fieldkey: functie_key}];
      if (fdisc)
        tablerefArray.push({field: "ins_discipline_key", fieldkey: disc_arr_key});
      // Als er audit history is, dan voegt de functie een extra button aan het meegegeven object buttons toe.
      AUDIT_HISTORY_BUTTON(buttons,
                           { tablekeyarray: tablekeyArray,
                             tableref: tablerefArray,
                             tablename: "fac_groeprechten",
                             historytitle: L("fac_groeprechten") });
    }

    SIMPLE_BLOCK_START();
        CreateButtons(buttons);
    SIMPLE_BLOCK_END();
    IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html><% ASPPAGE_END(); %>
