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

     Status: 100%
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/disciplineselector.inc" -->
<!-- #include file="../Shared/stdmeldingselector.inc" -->
<!-- #include file="../Shared/referentieselector.inc" -->
<!-- #include file="mld.inc" -->

<%
FCLTHeader.Requires({ plugins: ["suggest", "jQuery"] });
var urole = getQParamSafe("urole");
var autfunction = "";
switch(urole)
{
  case "fo": autfunction = "WEB_MLDFOF";
           break;
  case "bo": autfunction = "WEB_MLDBOF";
           break;
  case "mi": autfunction = "WEB_MLDBAC";
           break;
  case "fe":
  default  : autfunction = "WEB_MLDUSE";
           break;
}
// De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
var authparams = user.checkAutorisation(autfunction);

var mld_key_arr = getQParamIntArray("mld_key");
var ingesloten = [];
var this_mld;
var mld_info;
var this_stdm;
// Bepaal de meldingen in de selectie die
// ook echt gegroepeerd kunnen of mogen worden.
for (var i = 0; i < mld_key_arr.length; i++)
{
  // TODO: canGroup ? -> this_mld = mld.func_enabled_melding(mld_key_arr[i]); // Wat heb ik zoal aan rechten op deze specifieke melding
  mld_info  = mld.mld_melding_info(mld_key_arr[i]);
  this_stdm = mld.mld_stdmeldinginfo(mld_info.stdm_key);

  if (this_stdm.srt_disc_mldgroup & 2)
    ingesloten.push(mld_key_arr[i]);
}
user.anything_todo_or_abort(ingesloten.length); // We klagen niet over enkele wel en enkele niet
var multi = ingesloten.length > 1;
var titel = L("lcl_mld_groeperen") + (multi ? "(" + ingesloten.length + " " + L("lcl_complains") + ")"
                                            : (mld_info.prefix||"") + ingesloten[0]);
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>

    <script type="text/javascript">

      $(function() {
        parent.$('span.ui-dialog-title').html("<%=safe.jsstring(titel)%>");
      })

      function onChangeRef(ref_key)
      {
        $("#mldGroupNew").toggle(ref_key == -1);
        FcltMgr.resized();
      }

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

        // Standaard checks op verplichte velden, datum formaat, numeriek en float formaat, currency formaat.
        if (!await validateForm("groupform"))
          return false;

        $.post($("form[name=groupform]")[0].action, $("[name=groupform]").serialize(), FcltCallbackAndThenAlways(mld_group_callback), "json");
        return true; // Disable button
      }

      function mld_group_callback(json) {
        if (json.success) {
          FcltMgr.closeDetail(window, json);
        } else {
          iface.button.enable("btn_group_submit");
        }
      }

      function mld_cancel()
      {
          FcltMgr.closeDetail(window, { cancel: true } );
      }

      function mld_new()
      {
          FcltMgr.openDetail("appl/mld/mld_melding.asp?mld_grouped_key_arr=<%=ingesloten.join(",")%>");
          mld_cancel();
      }

    </script>
  </head>

  <body class="fclt-modal" id="mod_mldgroup">
  <% MODAL_START(); %>
    <form id="groupform" name="groupform" action="mld_group_save.asp?mld_keys=<%=ingesloten.join(",")%>&urole=<%=urole%>" method="post">
<%
      MODAL_BLOCK_START("mldGroup", L("lcl_mld_kies_bestaand") + L("lcl_mld_groepering"), { "icon": "fa-rectangle-history-circle-plus" });
      FCLTreferentieselector("mldgroup_key",
                             "sgmldgroup",
                             { label: L("lcl_mldgroup_key"),
                               referenceKey: -1,
                               referenceType: "MG",
                               onChange: "onChangeRef",
                               excludemldkey: ingesloten,
                               filtercode: "MGO",
                               required: true
                             });
      MODAL_BLOCK_END();

      var buttons =  [{ title: L("lcl_mld_new"),   icon: "fa-plus",        action: "mld_new()",   singlepress: true, id: "btn_new" },
                      { title: L("lcl_groeperen"), icon: "fa-fclt-save",   action: "mld_group()", singlepress: true, id: "btn_group_submit" },
                      { title: L("lcl_cancel"),    icon: "fa-fclt-cancel", action: "mld_cancel()" }];

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