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

    File:               res_edit_arrangement_artikel_multi.asp
    Description:        Multi toevoegen van artikelen aan een arrangement
    Parameters:         id

    Context:            res_artikel.asp
    Note:               Nvt

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

<%
FCLTHeader.Requires({ plugins:["suggest", "jQuery", "kenmerk"],
                      js: ["jquery-ui.js"] });

var art_key_arr = getQParamIntArray("id");
var submitting = getQParamInt("submit", 0) == 1;
var arrangement_key = -1;
if (submitting) { arrangement_key = getFParamInt("arrangement", -1); }

var warning = "";
var result = {};
var tobearranged = 0;
var ingesloten = [];
var aanwezig = {};
// select items that are already attached to the arrangment
if (submitting)
{
  var oRs = Oracle.Execute(
    "SELECT res_artikel_key "+
    "FROM   res_arrangement_artikel "+
    "WHERE  res_arrangement_key = "+arrangement_key+" "+
    "AND    res_artikel_key IN ("+art_key_arr.join(",")+")"
  );
  while (!oRs.eof)
  {
    aanwezig[oRs("res_artikel_key").Value]=1;
    oRs.MoveNext();
  }
  oRs.close();
}

// Bepaal de reservering artikelen in de selectie die
// ook echt aan het arrangement toegevoegd kunnen of mogen worden.
for (var i = 0; i < art_key_arr.length; i++)
{
  if (!aanwezig[art_key_arr[i]])
  {
    ingesloten.push(art_key_arr[i]);
    tobearranged++;
  }
}

user.auth_required_or_abort(ingesloten.length); // We klagen niet over enkele wel en enkele niet
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
<%
    if (submitting)
    {
      for (var i = 0; i < ingesloten.length; i++)
      {
        var fields = [
          { dbs: "res_arrangement_key",  typ: "key",   val: arrangement_key,  foreign: "res_arrangement" },
          { dbs: "res_artikel_key",  typ: "key",   val: ingesloten[i],  foreign: "res_artikel" }
        ];
        var resIns = buildInsert("res_arrangement_artikel", fields);
        var err = Oracle.Execute(resIns.sql, true);
        if (err.friendlyMsg)
            warning = err.friendlyMsg;
      }

%>    <script type="text/javascript">
        $(function()
        {
          FcltMgr.closeDetail(window, { success: true } )
        });
      </script>
<%    Response.End();
    }
%>

    <script type="text/javascript">
      function res_submit()
      { // Invoer van velden wordt realtime gevalideerd.
        document.forms.u2.submit();
        return true; // disable button
      }

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

      function res_arr_remark()
      {
        $("div.readonly.opmerkingen").html($("option:selected").attr("title").replace(/\n/g, "<br>"));
        FcltMgr.resized();
      }

      $(function()
      {
        res_arr_remark();
      });

    </script>
  </head>

  <body class="fclt-modal" id="mod_resmultiedit">
    <% MODAL_START(); %>
    <form name="u2" action="res_edit_arrangement_artikel_multi.asp?submit=1&id=<%=ingesloten.join(",")%>" method="post">
<%    MODAL_BLOCK_START("arrgArtikelen", L("res_arrangement"), { "icon": "fa-burger-glass" });
      // select arrangements
      var oRs = Oracle.Execute(
        "SELECT res_arrangement_key, res_arrangement_omschrijving "+
        "FROM   res_arrangement"
      );
      sql = "SELECT res_arrangement_key, res_arrangement_omschrijving, res_arrangement_opmerking title "+
            "FROM   res_arrangement";
      FCLTselector("arrangement",
                   sql,
                   { label: L("res_arrangement"),
                     extraParamValue: "title",
                     onChange: "res_arr_remark()"
                   });
      ROTEXTAREATR("opmerkingen", L("res_arrangement_opmerking"), "");
      MODAL_BLOCK_END();

      var buttons =  [];
      buttons.push({title: L("lcl_submit"), action:"res_submit()", singlepress: true, id: "btn_edit_submit", icon: "fa-fclt-save"});
      buttons.push({title: L("lcl_cancel"),  action:"res_cancel()", icon: "fa-fclt-cancel"});
      SIMPLE_BLOCK_START();
        CreateButtons(buttons);
      SIMPLE_BLOCK_END();
      IFACE.FORM_END();
%>
    </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
