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

    File:               mld_edit_kenmerk_multi.asp
    Description:        Multi-edit wijzigen van overeenkomstige kenmerken van de geselecteerde meldingen
    Parameters:         mld_melding_key

    Context:
    Note:

*/
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../mld/mld_flexkenmerk.inc" -->
<!-- #include file="../mld/mld.inc" -->

<%
FCLTHeader.Requires({ plugins: ["jQuery", "suggest"],
                      js: ["./mld_edit_melding.js", "../Shared/kenmerk_js.asp"] });

var mld_key_arr = getQParamIntArray("mld_key");
var urole = getQParamSafe("urole");
var multi = getQParamInt("multi", 0) == 1;

var fronto = urole == "fo";
var backo  = urole == "bo";
var minfo  = urole == "mi";
var frontend = (!fronto && !backo && !minfo);

var autfunction = "";
if (fronto)
  autfunction = "WEB_MLDFOF";
else if (minfo)
  autfunction = "WEB_MLDBAC";
else if (backo)
  autfunction = "WEB_MLDBOF";
else
  autfunction = "WEB_MLDUSE";
var authparams = user.checkAutorisation(autfunction); // Dit is nog ongeacht de melding

var transitParam = buildTransitParam(["mld_key", "multi", "urole"]);

function allStdmKenmerk(stdm_key)
{
    var flexkm = getFlexKenmerkBlockSql( { stdm_arr: [stdm_key]
                                         , mld_key: -1
                                         , melder_key: user_key
                                         } );
    var lvlkenmerk_list = [];
    var oRs = Oracle.Execute(flexkm.sql);
    while (!oRs.eof)
    {
        lvlkenmerk_list.push( { km_key: oRs("kenmerk_key").Value
                              , srt_key: oRs("srtkenmerk_key").Value
                              , km_name: oRs("kenmerk_omschrijving").Value
                              } );
        oRs.MoveNext();
    }
    oRs.Close();
    return { srt_key: stdm_key
           , lvlkenmerk_list: lvlkenmerk_list
           };
}

function getCommonPropertiesList(obj, list, elm)
{
    var elm_list = [];
    for (var i=0; i<obj[list].length; i++)
    {
        elm_list.push(obj[list][i][elm]);
    }
    return elm_list;
}

var ingesloten = [];
for (i=0; i<mld_key_arr.length; i++)
{
    var this_mld = mld.func_enabled_melding(mld_key_arr[i]);
    if (this_mld.canFlexChange)
    {
        ingesloten.push(mld_key_arr[i]);
    }
}
user.anything_todo_or_abort(ingesloten.length);

// Van elke standaardmelding die bij de geselecteerde meldingen hoort de kenmerken ophalen.
var ingesloten_stdm = [];
var sql = "SELECT DISTINCT mld_stdmelding_key"
        + "  FROM mld_melding"
        + " WHERE mld_melding_key in (" + ingesloten.join(",") + ")";
var oRs = Oracle.Execute(sql);
while (!oRs.eof)
{
    var stdm_kenmerken = allStdmKenmerk(oRs("mld_stdmelding_key").Value);
    ingesloten_stdm.push(stdm_kenmerken);
    oRs.MoveNext();
}
oRs.Close();

ingesloten_stdm = getObjectsCommonProperties(ingesloten_stdm);

%>
<html>
  <head>
<%	FCLTHeader.Generate(); %>
    <script>
        function mld_cancel()
        {
            FcltMgr.closeDetail(window, { cancel: true } );
        }

        async function mld_submit()
        {
            document.activeElement.blur(); // trigger laatste onChanges

            var selected_fields_arr = [];
            $("#multikenmerk tr").each(function()
                {
                    var multi_cb = this.childNodes[0].childNodes[0].id;
                    var multi_km = multi_cb.substr(4);
                    if ($("#"+multi_cb).prop("checked"))
                    {   // Alleen datavelden toevoegen voor validatie die gewijzigd moeten worden.
                        selected_fields_arr.push(multi_km);
                    }
                });
            if (selected_fields_arr.length > 0)
            {
                if (!await validateForm("u2", { checkOnly: selected_fields_arr}))
                    return false;
            }

            $.post( $("form[name=kenmerkform]")[0].action
                  , $("[name=kenmerkform]").serialize() + "<%=transitParam%>"
                  , FcltCallbackClose
                  , "json"
                  );
            return true; // disable button
        }

        $(function()
        {
            $('textarea').resize(function () { FcltMgr.resized(window) } );
            $('textarea').autogrow();
            $("#btn_kenmerk_submit").hide();
        });

    </script>
  </head>

  <body class="fclt-modal" id="mod_mldaccept" kenmerk_submit="btn_kenmerk_submit">
    <% MODAL_START(); %>
        <form id="kenmerkform" name="kenmerkform" action="mld_edit_kenmerk_multi_save.asp" method="post">
<%
        var buttons = [];
        MODAL_BLOCK_START("multikenmerk", L("lcl_multiedit_kenmerk_common"), { icon: "fa-tag"});

            if (ingesloten_stdm[0].lvlkenmerk_list.length == 0)
            {
                ROFIELD("fldoms", "", L("lcl_multiedit_kenmerk_warning"));

            }
            else
            {
                buttons.push({title: L("lcl_submit"), icon: "fa-fclt-save", action: "mld_submit()", singlepress: true, id: "btn_kenmerk_submit", importance: 1});
                var flexParams = { stdm_arr: [ingesloten_stdm[0].srt_key]
                                 , mld_key: -1
                                 , kenmerk_keys: getCommonPropertiesList(ingesloten_stdm[0], "lvlkenmerk_list", "km_key")
                                 , multiEdit: true
                                 , melder_key: user_key
                                 // parameters voor de behandelbare blokheaders hoeven niet worden meegegeven. Labels worden namelijk niet getoond bij multiedit.
                                 };
                generateFlexKenmerkBlock (flexParams);
            }

        MODAL_BLOCK_END();

        buttons.push({title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "mld_cancel()", importance: 3});
        SIMPLE_BLOCK_START();
            CreateButtons(buttons);
        SIMPLE_BLOCK_END();
        IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
