<%@ LANGUAGE = JavaScript %>
<%
/*
     $Revision$
     $Id$

     File:               fac_edit_reportcolumn.asp
     Description:        Wizard voor het toevoegen van een kenmerkkolom aan een rapport.
     Parameters:

     Context:           Wordt aangeroepen vanuit model_reportcolumns kolom expression (SQL-expressie).
     Note:

*/
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../API2/api2.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/selector.inc" -->

<%
FCLTHeader.Requires();
var rap_id = getQParamInt("rap_id");
var col_id = getQParamInt("col_id");
var autostart = getQParamInt("autostart",-1);

user.checkAutorisation("WEB_UDRMAN");

function BREAK()
{
  Response.Write("<tr><td colspan='2' style='height:5px'></td></tr>");
}
%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>
    <script type="text/javascript">

    function onLoadKenmerkinfo(p_module, p_tabel)
    {
        $.getJSON( "../fac/get_kenmerk_info_ajax.asp"
                 , { req_info: p_tabel
                   , module: p_module
                   }
                 , function(data)
                   {
                        process_kenmerk_info(data);
                        if (data.req_info == "srtdiscipline" && p_module != "RES")
                        {
                            onChangeVakgroeptype();
                        }
                   }
                 );
    }

    function onChangeModule()
    {
        var module = $("#kenmerkmodule").val();

        process_kenmerk_info({module: module, req_info: "srtdiscipline", lov: []});
        process_kenmerk_info({module: module, req_info: "discipline",    lov: []});
        process_kenmerk_info({module: module, req_info: "groep",         lov: []});
        process_kenmerk_info({module: module, req_info: "deel",          lov: []});
        process_kenmerk_info({module: module, req_info: "kenmerk",       lov: []});
        switch (module)
        {
            case "ALG": onLoadKenmerkinfo(module, "srtdiscipline");
                        $("label[for='srtdisc']").text(L("lcl_niveau"));
                        break;
            case "BES": onLoadKenmerkinfo(module, "srtdiscipline");
                        onLoadKenmerkinfo(module, "srtkenmerk");
                        $("label[for='srtdisc']").text(L("bes_kenmerk_type"));
                        $("label[for='disc']").text(L("bes_discipline"));
                        break;
            case "CNT": onLoadKenmerkinfo(module, "discipline");
                        onLoadKenmerkinfo(module, "srtkenmerk");
                        $("label[for='disc'").text(L("cnt_discipline"));
                        break;
            case "INS": onLoadKenmerkinfo(module, "discipline");
                        onLoadKenmerkinfo(module, "srtkenmerk");
                        $("label[for='disc']").text(L("ins_discipline"));
                        $("label[for='groep']").text(L("ins_srtinstallatie_srtgroep"));
                        $("label[for='deel']").text(L("ins_srtinstallatie_srtdeel"));
                        break;
            case "CTR": onLoadKenmerkinfo(module, "srtdiscipline");
                        onLoadKenmerkinfo(module, "srtkenmerk");
                        $("label[for='srtdisc']").text(L("ctr_discipline"));
                        $("label[for='disc']").text(L("ins_discipline"));
                        $("label[for='groep']").text(L("ins_srtinstallatie_srtgroep"));
                        $("label[for='deel']").text(L("ins_srtinstallatie_srtdeel"));
                        break;
            case "MLD": onLoadKenmerkinfo(module, "srtdiscipline");
                        onLoadKenmerkinfo(module, "srtkenmerk");
                        $("label[for='srtdisc']").text(L("lcl_vakgroup_type"));
                        $("label[for='disc']").text(L("mld_discipline"));
                        $("label[for='groep']").text(L("lcl_complain"));
                        break;
            case "OPD": onLoadKenmerkinfo(module, "groep");
                        onLoadKenmerkinfo(module, "srtkenmerk");
                        $("label[for='groep']").text(L("lcl_mld_typeopdr"));
                        break;
            case "RES": onLoadKenmerkinfo(module, "srtdiscipline");
                        onLoadKenmerkinfo(module, "discipline");
                        onLoadKenmerkinfo(module, "deel");
                        onLoadKenmerkinfo(module, "srtkenmerk");
                        $("label[for='srtdisc']").text(L("lcl_niveau"));
                        $("label[for='disc']").text(L("res_discipline"));
                        $("label[for='deel']").text(L("res_activiteit"));
                        break;
        }
        reload_kenmerk();
    }

    function onChangeVakgroeptype()
    {
        var module = $("#kenmerkmodule").val();
        switch (module)
        {
            case "ALG": process_kenmerk_info({module: module, req_info: "srtkenmerk", lov: []});
                        break;
            case "BES": break;
            case "CNT": break;
            case "INS":
            case "CTR":
            case "MLD":
            case "OPD": {
                process_kenmerk_info({module: module, req_info: "discipline", lov: []});
                process_kenmerk_info({module: module, req_info: "groep",      lov: []});
                process_kenmerk_info({module: module, req_info: "deel",       lov: []});
                process_kenmerk_info({module: module, req_info: "kenmerk",    lov: []});
                var req_data = { req_info: "discipline"
                               , module: module
                               , srtkenm_key: $("#srtkenm").val() || -1
                               , srtd_key: $("#srtdisc").val() || -1
                               , disc_key: -1
                               , groep_key: -1
                               }
                if (req_data.srtd_key > 0)
                {
                    $.getJSON( "../fac/get_kenmerk_info_ajax.asp"
                             , req_data
                             , process_kenmerk_info
                             );
                }
                break;
            }
            case "RES": break;
        }
        reload_kenmerk();
    }
    function onChangeVakgroep(vakgroep_key)
    {
        var module = $("#kenmerkmodule").val();
        if (module != "RES" && module != "CNT")
        {
            process_kenmerk_info({module: module, req_info: "groep",   lov: []});
            process_kenmerk_info({module: module, req_info: "deel", lov: []});
            process_kenmerk_info({module: module, req_info: "kenmerk", lov: []});
            var req_data = { req_info: "groep"
                           , module: module
                           , srtkenm_key: $("#srtkenm").val() || -1
                           , srtd_key: $("#srtdisc").val() || -1
                           , disc_key: $("#disc").val() || -1
                           , groep_key: -1
                           }
            if (req_data.disc_key > 0)
            {
                $.getJSON( "../fac/get_kenmerk_info_ajax.asp"
                         , req_data
                         , process_kenmerk_info
                         );
            }
        }
        reload_kenmerk();
    }
    function onChangeGroep()
    {
        var module = $("#kenmerkmodule").val();
        if (module == "INS" || module == "CTR")
        {
            process_kenmerk_info({module: module, req_info: "deel", lov: []});
            process_kenmerk_info({module: module, req_info: "kenmerk", lov: []});
            var req_data = { req_info: "deel"
                           , module: module
                           , srtkenm_key: $("#srtkenm").val() || -1
                           , srtd_key: $("#srtdisc").val() || -1
                           , disc_key: $("#disc").val() || -1
                           , groep_key: $("#groep").val() || -1
                           , deel_key: -1
                           }
            if (req_data.groep_key > 0)
            {
                $.getJSON( "../fac/get_kenmerk_info_ajax.asp"
                         , req_data
                         , process_kenmerk_info
                         );
            }
        }
        reload_kenmerk();
    }
    function onChangeDeel()
    {
        reload_kenmerk();
    }
    function onChangeKenmerksoort()
    {
        reload_kenmerk();
    }
    function onChangeKenmerk()
    {
        onChangeReferentie()
    }
    function onChangeReferentie()
    {
        var kenm_key = $("#kenm").val();
        var ref_key = $("#refcol").val();
        if (kenm_key > -1 && ref_key > -1)
        {
            iface.button.enable("closeButton");
        }
    }
    function reload_kenmerk()
    {
        var req_data = { req_info: "kenmerk"
                       , module: $("#kenmerkmodule").val()
                       , srtkenm_key: $("#srtkenm").val() || -1
                       , srtd_key: $("#srtdisc").val()|| -1
                       , disc_key: $("#disc").val() || -1
                       , groep_key: $("#groep").val() || -1
                       , deel_key: $("#deel").val() || -1
                       }
        iface.button.disable("closeButton");
        if (!(req_data.srtd_key == -1 && (req_data.module == "MLD" || req_data.module == "BES")))
        {
            $.getJSON( "../fac/get_kenmerk_info_ajax.asp"
                     , req_data
                     , process_kenmerk_info
                     );
        }
    }

    function process_kenmerk_info(data)
    {
        var select;
        var show_selectors = { BES: [1,1,0,0,1,1] // tonen: bes_kenmerk_type, bes_discipline, bes_srtkenmerk, bes_kenmerk
                             , CNT: [0,1,0,0,1,1] // tonen: cnt_discipline, cnt_srtkenmerk, cnt_kenmerk
                             , INS: [0,1,1,1,1,1] // tonen: ins_discipline, ins_srtgroep, ins_srtdeel, ins_srtkenmerk, ins_kenmerk
                             , CTR: [1,1,1,1,1,1] // tonen: ctr_discipline, ins_discipline, ins_srtgroep, ins_srtdeel, ins_srtkenmerk, ins_kenmerk
                             , MLD: [1,1,1,0,1,1] // tonen: ins_srtdiscipline, mld_discipline, mld_stdmelding, mld_srtkenmerk, mld_kenmerk
                             , OPD: [0,0,1,0,1,1] // tonen: mld_typeopdr, mld_srtkenmerk, mld_kenmerk
                             , RES: [1,1,0,1,1,1] // tonen: res_kenmerk_niveau, res_discipline, res_activiteit, res_srtkenmerk, res_kenmerk
                             , ALG: [1,0,0,0,0,1] // tonen: alg_kenmerk_niveau, alg_kenmerk
                             }

        switch(data.req_info)
        {
            case "srtdiscipline": select = $("#srtdisc"); vlgnr = 0; break;
            case "discipline":    select = $("#disc");    vlgnr = 1; break;
            case "groep":         select = $("#groep");   vlgnr = 2; break;
            case "deel":          select = $("#deel");    vlgnr = 3; break;
            case "srtkenmerk":    select = $("#srtkenm"); vlgnr = 4; break;
            case "kenmerk":       select = $("#kenm");    vlgnr = 5; break;
        }

        var sel_key = -1;
        $new_lov = (data.req_info == "srtdiscipline" ? null : $("<option>").val(-1).html("") );
        select.empty().append($new_lov);
        if (data.lov.length > 0)
        {
            for (var i=0; i<data.lov.length; i++)
            {
                var opt = data.lov[i];
                $new_lov = $("<option>").val(opt.key).html(opt.oms);
                if (opt.sel)
                {
                    $new_lov.attr("selected", "selected");
                    sel_key = opt.key;
                }
                select.append($new_lov);
            }
            select.prop("disabled", false);
            select.removeClass("btn_disabled");
        }
        else
        {
            select.prop("disabled", true);
            select.addClass("btn_disabled");
        }

        if (sel_key > -1)
        {
            select.val(sel_key);
        }

        // Toon aantal beschikbare kenmerken in huidige selectie.
        if (data.req_info == "kenmerk")
        {
            var kenm_label = $("label[for='kenm']");
            $(kenm_label).text(L("mgt_kenmerk") + " ("+ data.lov.length +")");
        }

        if (data.hide || show_selectors[data.module][vlgnr] == 0)
        {
            $(select).closest("tr").hide();
        }
        else
        {
            $(select).closest("tr").show();
        }
    }

    async function Sluiten()
    {
        document.activeElement.blur(); // trigger laatste onChanges
        if (!await validateForm("u2"))
            return false;

        var module = $("#kenmerkmodule").val();
        $.getJSON( "../fac/get_kenmerk_info_ajax.asp"
                 , { req_info: "kenmerkwaarde"
                   , module: module
                   , cols_key: $("#refcol").val()
                   , kenm_key: $("#kenm").val()
                   , srtk_key: $("#srtkenm").val()
                   , srtd_key: $("#srtdisc").val() || -1
                   , disc_key: $("#disc").val()
                   , stdm_key: $("#groep").val()
                   }
                 , process_kenmerk_expressie
                 );
    }

    function process_kenmerk_expressie(expr_data)
    {
        var return_data = { sql_expressie: expr_data.sql
                          , kenmerk_kolom: expr_data.module + L("mgt_kenmerk") + expr_data.key
                          , kenmerk_naam: expr_data.naam
                          , data_type: expr_data.type
                          , autoclose: (<%=autostart%> == 1)
                          };
        FcltMgr.closeDetail(window, return_data);
    }

    function Cancel()
    {
        var autostart = (<%=autostart%> == 1);
        var return_data = ( autostart
                          ? { autoclose: function()
                                            {   var mCount = parent.FcltMgr._modalCount;
                                                var mParam = parent.FcltMgr._modalParams;
                                                mParam[mCount].close();
                                                FcltMgr.stopEdit(parent);
                                            }
                            }
                          : { cancel: true }
                          );
        FcltMgr.closeDetail(window, return_data );
    }

    $(function()
    {
        var module = $("#kenmerkmodule").val();
        onChangeModule();
        iface.button.disable("closeButton");
    });
    </script>
  </head>
  <body class="fclt-modal" id="mod_editreportcolumn">
    <form name="u2" action="edit_reportcolumn_save.asp" method="post">
<%
    BLOCK_START("facReportcolumn", L("lcl_advanced"), { icon: "fa-filter-list" });
      var kenmerk_modules = L("lcl_usrrap_property_moduleLOV");
      var sql = api2.splitLOV2sql(kenmerk_modules);
      FCLTselector( "kenmerkmodule"
                  , sql
                  , { label: L("lcl_usrrap_property_module")
                    , onChange: "onChangeModule()"
                    }
                  );
      BREAK();

      FCLTselector( "srtdisc"
                  , ""
                  , { initkey: -1
                    , label: L("lcl_vakgroup_type")
                    , onChange: "onChangeVakgroeptype()"
                    }
                  );
      FCLTselector( "disc"
                  , ""
                  , { initkey: -1
                    , label: L("lcl_vakgroup")
                    , onChange: "onChangeVakgroep()"
                    }
                  );
      FCLTselector( "groep"
                  , ""
                  , { init_key: -1
                    , label: L("lcl_complain")
                    , onChange: "onChangeGroep()"
                    }
                  );
      FCLTselector( "deel"
                  , ""
                  , { init_key: -1
                    , label: "XX"+L("lcl_complain")
                    , onChange: "onChangeDeel()"
                    }
                  );
      BREAK();

      FCLTselector( "srtkenm"
                  , ""
                  , { label: L("mgt_srtkenmerk")
                    , onChange: "onChangeKenmerksoort()"
                    }
                  );
      BREAK();

      FCLTselector( "kenm"
                  , ""
                  , { init_key: -1
                    , label: L("mgt_kenmerk")
                    , onChange: "onChangeKenmerk()"
                    }
                  );
      BREAK();
      var sql_refcol = "SELECT fac_usrrap_cols_key"
                     + "     , fac_usrrap_cols_caption"
                     + "  FROM fac_usrrap_cols "
                     + " WHERE fac_usrrap_key = " + rap_id
                     + "   AND fac_usrrap_cols_key <> " + col_id
                     + "   AND fac_usrrap_cols_datatype = 'number'"
                     + "   AND fac_usrrap_cols_column_name like '%key'"
                     + " ORDER BY fac_usrrap_cols_volgnr";
      FCLTselector( "refcol"
                  , sql_refcol
                  , { init_key: -1
                    , label: L("lcl_usrrap_connect_key")
                    , onChange: "onChangeReferentie()"
                    }
                  );
    BLOCK_END();

    var buttons = [];
    buttons.push({id: "closeButton", title: L("lcl_window_done"), action: "Sluiten()", icon: "fa-fclt-save"});
    buttons.push({title: L("lcl_cancel"), action: "Cancel()", icon: "fa-fclt-cancel", tooltip: L("lcl_cancel") });
    SIMPLE_BLOCK_START();
        CreateButtons(buttons);
    SIMPLE_BLOCK_END();
    IFACE.FORM_END();
%>
    </form>
  </body>
</html>
<% ASPPAGE_END(); %>


