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

    File:               ins_move_multi.asp
    Description:        Multi verplaatsen van objecten
    Parameters:         ins_keys

    Context:
    Note:

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

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

var urole = getQParamSafe("urole");
var fronto = urole == "fo";
var backo  = urole == "bo";
var minfo  = urole == "mi";
var frontend = (!fronto & !backo & !minfo);
var autfunction = urole == "fo"? "WEB_INSFOF" : "WEB_INSMAN";
var authparams = user.checkAutorisation(autfunction); // Nog nodig voor FCLTplaatsselector

var ins_key_arr = getQParamIntArray("ins_keys");
var submitting = getQParamInt("submit", 0) == 1;

var warning = "";
var result = {};
var tobemoved = 0;
var ingesloten = [];
// Bepaal de objecten in de selectie die
// ook echt geinspecteerd kunnen of mogen worden.
for (var i = 0; i < ins_key_arr.length; i++)
{
  var this_ins = ins.func_enabled_deel(ins_key_arr[i]);
  if (this_ins.canChange)
  {
    ingesloten.push(ins_key_arr[i]);
    tobemoved++;
  }
}
user.anything_todo_or_abort(tobemoved > 0); // We klagen niet over enkele wel en enkele niet
%>

<html>
  <head>
<%  FCLTHeader.Generate(); %>
<%
    if (submitting)
    {
      protectRequest.validateToken();

      var w_key = getFParamInt("werkplekkey");
      var r_key = getFParamInt("ruimtekey");
      var v_key = getFParamInt("verdiepingkey");
      var g_key = getFParamInt("gebouwkey");
      var l_key = getFParamInt("locatiekey");
      var btype = getFParam("bttype","");
      var wis_grafisch = getFParam("wis_grafisch",'off') == 'on';
      var bind_typ;
      var bind_key;

      if (l_key > 0)
      { bind_typ = 'L';
        bind_key = l_key;
      }
      if (btype == 'T')
      {
        if (g_key > 0)
        { bind_typ = 'T';
          bind_key = g_key;
        }
      }
      else
      {
        if (g_key > 0)
        { bind_typ = 'G';
          bind_key = g_key;
        }
        if (v_key > 0)
        { bind_typ = 'V';
          bind_key = v_key;
        }
        if (r_key > 0)
        { bind_typ = 'R';
          bind_key = r_key;
        }
        if (w_key > 0)
        { bind_typ = 'W';
          bind_key = w_key;
        }
      }

      var sql_wis_dwgxy = ", ins_deel_dwgx = null"
                        + ", ins_deel_dwgy = null"
                        + ", ins_deel_dwgz = null"
      var warning = null;
      var first = true;
      for (var i = 0; i < ingesloten.length; i++)
      {
        // Ik mag het object wijzigen dus voer wijziging uit
        sql = "UPDATE ins_deel"
            + " SET ins_alg_ruimte_key = " + bind_key
            + ", ins_alg_ruimte_type = " + safe.quoted_sql(bind_typ)
            + (wis_grafisch ? sql_wis_dwgxy : "")
            + " WHERE ins_deel_key = " + ingesloten[i];
        var err = Oracle.Execute(sql, true);
        if (err.friendlyMsg)
          warning = err.friendlyMsg;
      }

%>
      <script type="text/javascript">
        $(document).ready(function()
                          {
                            FcltMgr.closeDetail(window, { warning: "<%=safe.jsstring(warning)%>", success: true }); // Altijd success en een refresh
                          });
      </script>
<%
      Response.End();
    }
%>

    <script type="text/javascript">
      function ins_submit()
      {
          if (!validateForm("u2"))
            return false;
          document.forms.u2.submit();
          return true; // disable button
      }

      function ins_cancel()
      {
          FcltMgr.closeDetail(window, { cancel: true } );
      }
    </script>
  </head>


  <body class="modal" id="mod_insmultiMove">
<%
    var srtdeel_binding = 0xFF; // Veronderstel nog even dat alles mag
    var maxlevel = 3;
    var change = "";

    sql = "select sd.ins_srtdeel_binding "
        + "from ins_srtdeel SD "
        + ",    ins_deel D "
        + "where D.ins_srtdeel_key = SD.ins_srtdeel_key "
        + "and   D.ins_deel_key in (" + ingesloten.join(",") + ")";
    oRs = Oracle.Execute(sql);
    while (!oRs.eof)
    { // bitwise and om alleen de overeenkomstige bindings over te houden.
      srtdeel_binding = srtdeel_binding & oRs("ins_srtdeel_binding").value;
      oRs.MoveNext();
    }
    oRs.close();

    if (srtdeel_binding & (BIND_TERREI | BIND_RUIMTE | BIND_WERKPL))
    {
      maxlevel = 2;
    }
    if (srtdeel_binding & (BIND_RUIMTE | BIND_WERKPL))
    {
      maxlevel = 5;
    }
    if (srtdeel_binding & (BIND_WERKPL))
    {
      maxlevel = 6;
    }
%>
    <form name="u2"
          action="ins_move_deel_multi.asp?submit=1&r_key=<%=r_key%>&ins_keys=<%=ingesloten.join(",")%>>&urole=<%=urole%>"
          method="post">
<%
      BLOCK_START("insMultiMove", L("lcl_select_locatie"))

        FCLTplaatsselector( authparams.ALGreadlevel
                          , { terrein: true
                            , startlevel: 2 /*locatie*/
                            , eindlevel: maxlevel
                            , terrein: true
                            , requiredlevel: 2
                            }
                          );

      BLOCK_END()

      if (S("fg_ins_symbols"))
      {
        // zijn er objecten met coordinaten?
        var sql = "SELECT count(*) aantal"
                + "  FROM ins_deel"
                + " WHERE ins_deel_dwgx IS NOT NULL"
                + "   AND ins_deel_key IN (" + ingesloten.join(",") + ")";
        var oRs = Oracle.Execute(sql);
        if (oRs("aantal") > 0)
        {
%><table><%
            RWCHECKBOXTR("wis_grafisch", "fldcheck", L("lcl_ins_deel_wisgrafisch"), 1); // default wissen
%></table><%
        }
      }

      var buttons =  [];
      buttons.push({title: L("lcl_submit"), action:"ins_submit()", singlepress: true, id: "btn_move_submit"})
      buttons.push({title: L("lcl_cancel"),  action:"ins_cancel()"})
      CreateButtons(buttons)
IFACE.FORM_END();
%>
    </form>
  </body>
</html>
