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

    File:               cnt_edit_kenmerk_multi.asp
    Description:        Multi-edit wijzigen van overeenkomstige kenmerken van de geselecteerde contracten
    Parameters:         cnt_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="../cnt/cnt.inc" -->
<!-- #include file="../cnt/cnt_flexkenmerk.inc" -->

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

var cnt_key_arr = getQParamIntArray("cnt_key");
var multi = getQParamInt("multi", 0) == 1;
var transitParam = buildTransitParam(["cnt_key", "multi"]);

function allSrtcontractKenmerk(srt_key)
{
    var flexkm = getFlexKenmerkCodeSql( {disc_key_arr: [srt_key], cnt_key: -1, multiEdit: true} );
    var srtkenmerk_list = [];
    var kenmerk_list = [];
    var oRs = Oracle.Execute(flexkm.sql);
    while (!oRs.eof)
    {
        srtkenmerk_list.push(oRs("srtkenmerk_key").Value);
        kenmerk_list.push(oRs("kenmerk_key").Value);
        oRs.MoveNext();
    }
    oRs.Close();
    return { srt_key: srt_key
           , lvlkenmerk_list: srtkenmerk_list
           , kenmerk_list: kenmerk_list
           };
}

// Heb ik rechten op de geselecteerde contracten.
var ingesloten = [];
for (i=0; i<cnt_key_arr.length; i++)
{
    var this_cnt = cnt.func_enabled_contract(cnt_key_arr[i]);
    if (this_cnt.canChange)
    {
        ingesloten.push(cnt_key_arr[i]);
    }
}
user.anything_todo_or_abort(ingesloten.length);

// Van elke discipline die bij de geselecteerde contracten hoort de kenmerken ophalen.
var disc_arr = [];
var sql = "SELECT DISTINCT ins_discipline_key"
        + "  FROM cnt_contract"
        + " WHERE cnt_contract_key in (" + ingesloten.join(",") + ")";
var oRs = Oracle.Execute(sql);
while (!oRs.eof)
{
    var srtc_kenmerken = allSrtcontractKenmerk(oRs("ins_discipline_key").Value);
    disc_arr.push(srtc_kenmerken);
    oRs.MoveNext();
}
oRs.Close();

disc_arr = getObjectsCommonProperties(disc_arr);

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

        async function cnt_submit()
        {
            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="cnt_edit_kenmerk_multi_save.asp" method="post">
<%
        var buttons = [];
        MODAL_BLOCK_START("multikenmerk", L("lcl_multiedit_kenmerk_common"), { icon: "fa-tag"});

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

            }
            else
            {
                buttons.push({title: L("lcl_submit"), icon: "fa-fclt-save", action: "cnt_submit()", singlepress: true, id: "btn_kenmerk_submit", importance: 1});
                var flexParams = { disc_key_arr: [disc_arr[0].srt_key]
                                 , cnt_key: -1
                                 , kenmerk_keys: disc_arr[0].kenmerk_list
                                 , multiEdit: true
                                 };
                generateFlexKenmerkCode (flexParams);
            }

        MODAL_BLOCK_END();

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