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

     File:               sle_sleutelcilinder.asp
     Description:        Laat een sleutel aan meerdere cilinders toevoegen of een cilinder aan meerdere sleutels
     Parameters:         slecil_keys: komma separated lijst met ins_srt_deel_keys
     Context:
     Note:

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/afdelingselector.inc" -->
<!-- #include file="../Shared/srtdeelselector.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["./sle_sleutelcilinder.js"]
                    });

// TODO: Autorisatie controle "WEB_SLEBOF" op slecil_keys
// De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
var autfunction = "WEB_SLEBOF";
var authparamsSLEBOF = user.checkAutorisation(autfunction);

var slecil_keys = getQParamIntArray("slecil_keys", []);
var submitting = getQParamInt("submit", 0) == 1;

var sql = "SELECT sd.ins_srtgroep_key"
        + " FROM ins_srtdeel sd"
        + " WHERE sd.ins_srtdeel_key = " + slecil_keys[0]; // 24742
oRs = Oracle.Execute(sql);
var sletype = (oRs("ins_srtgroep_key").value == S("sle_ins_srt_groep_key")? "S" : "C");

if (submitting)
{
  FCLTHeader.Generate();

  protectRequest.validateToken();
  var nrRows = getFParamInt("nrRows", 0);

  //var afdeling_key = -1;
  var attach_key = -1;
  var attach_keys = [];
  for (var i = 0; i < nrRows; i++)
  {
    attach_key = getFParamInt("slecil" + i, 0);

    if (attach_key > 0)
    {
      attach_keys.push(attach_key);
    }
  }

  for (var j = 0; j < slecil_keys.length; j++)
  { // Verwijderen
    var sql = "DELETE ins_srtdeel_srtdeel"
            + " WHERE" + (sletype == "S"? " ins_srtdeel_key_2" : " ins_srtdeel_key_1") + " = " + slecil_keys[j];
    if (attach_keys.length) // 'nieuwe' laten staan, er komt straks wel een update
        sql += "   AND" + (sletype == "S"? " ins_srtdeel_key_1" : " ins_srtdeel_key_2") + " NOT IN (" + attach_keys.join(",") + ")";
    Oracle.Execute(sql);
  }

  for (var i = 0; i < attach_keys.length; i++)
  {
    for (var j = 0; j < slecil_keys.length; j++)
    {
      var sql = "SELECT ins_srtdeel_srtdeel_key"
              + "  FROM ins_srtdeel_srtdeel"
              + " WHERE ins_srtdeel_key_1 = " + (sletype == "S"? attach_keys[i] : slecil_keys[j])
              + "   AND ins_srtdeel_key_2 = " + (sletype == "S"? slecil_keys[j] : attach_keys[i]);
              //+ "   AND ins_srtdeel_srtdeel_verwijder IS NULL";
      oRs = Oracle.Execute(sql);
      if (oRs.eof)
      { // Insert, waneer item nog niet bestaat
        var sql = "INSERT INTO ins_srtdeel_srtdeel ("
                + "ins_srtdeel_key_1"
                + ", ins_srtdeel_key_2"
                + ", ins_module"
                + ") VALUES ("
                + (sletype == "S"? attach_keys[i] : slecil_keys[j])
                + "," + (sletype == "S"? slecil_keys[j] : attach_keys[i])
                + ", 'SLE')";
        Oracle.Execute(sql);
      }
    }
  }
%>

  <script type="text/javascript">
    FcltMgr.closeDetail(window, { success: true });
  </script>
<%
}
else
{ %>
  <html>
    <head>
<%    FCLTHeader.Generate(); %>
      <script type="text/javascript">
        sletype = "<%=sletype%>";
      </script>
    </head>

    <body class="fclt-modal" id="mod_sleutelcilinder">
      <% MODAL_START(); %>
      <div id="email">
        <form name="u2" method="post" action="sle_sleutelcilinder.asp?submit=1&slecil_keys=<%=slecil_keys%>">
        <% MODAL_BLOCK_START("sle_sleutelcilinder", (sletype == "S"? L("lcl_sle_geselecteerde_sleutels") : L("lcl_sle_geselecteerde_cilinders")) + ": " + slecil_keys.length, {icon: "fa-minus-circle fa-rotate-90"}); %>
          <input type="hidden" id="nrRows" name="nrRows" value="0">
<%
          // Toevoegen object soort
          FCLTsrtdeelselector("srtdeel",
                              "sgSrtdeel",
                              { label: (sletype == "S"? L("lcl_cil_cilinders") : L("lcl_sle_sleutels")),
                                srtgroep_key: (sletype == "S"? S("cil_ins_srt_groep_key") : S("sle_ins_srt_groep_key")),
                                autfunction: autfunction,
                                module: "SLE"
                              });
%>
          <tr>
            <td class="label">
              <label for="sleutelcilindertable" style="white-space: nowrap"><%=(sletype == "S"? L("lcl_sle_te_koppelen_cilinders") : L("lcl_sle_te_koppelen_sleutels"))%></label>
            </td>
            <td>
              <table id="sleutelcilindertable" cellspacing="0" cellpadding="0" class="rstable">
              <thead>
              <tr>
                <th width="15"></th>
                <th><%=(sletype == "S"? L("lcl_cil_cilinders") : L("lcl_sle_sleutels"))%></th>
              </tr>
              </thead>
              <tbody>
<%              var sql = "SELECT DISTINCT isd.ins_srtdeel_key"
                        + ", isd.ins_srtdeel_omschrijving"
                        + " FROM ins_srtdeel_srtdeel isdsd"
                        + ", ins_srtdeel isd"
                        + (sletype == "S"
                          ? " WHERE isdsd.ins_srtdeel_key_1 = isd.ins_srtdeel_key"
                          : " WHERE isdsd.ins_srtdeel_key_2 = isd.ins_srtdeel_key")
                        + " AND isdsd.ins_module = 'SLE'"
                        + " AND" + (sletype == "S"? " isdsd.ins_srtdeel_key_2" : " isdsd.ins_srtdeel_key_1") + " IN (" + slecil_keys + ")"
                    + " ORDER BY isd.ins_srtdeel_omschrijving";
                oRs = Oracle.Execute(sql);
                var teller = 0;
                while (!oRs.eof)
                {
                  teller++; %>
                  <script language="JavaScript">
                    CreateRow(<%=oRs("ins_srtdeel_key").value%>,
                              "<%=safe.jsstring(oRs("ins_srtdeel_omschrijving").value)%>");
                  </script>

<%                oRs.MoveNext();
                } %>
              </tbody>
              </table>
            </td>
          </tr>

<%        MODAL_BLOCK_END();
          var buttons =  [ {title: L("lcl_add"),    icon: "fa-plus", action: "sle_add()" },
                           {title: L("lcl_submit"), icon: "fa-fclt-save",   action: "sle_submit()" } ];
          SIMPLE_BLOCK_START();
            CreateButtons(buttons);
          SIMPLE_BLOCK_END();
IFACE.FORM_END();
%>
        </form>
      </div>
      <% MODAL_END(); %>
    </body>
  </html>
<%
}
%><% ASPPAGE_END(); %>
