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

     Plaats alle objecten van een verdieping alfanumeriek in
     de ruimte waar ze grafisch in (b)lijken te staan

     IN: highlight, lijst van geselecteerde alg_ruimte_keys
*/

var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/save2db.inc" -->
<!-- #include file="../SlnkDWF/Slnk2IMG.inc" -->
<!-- #include file="../SlnkDWF/DWFProperties.inc" -->
<!-- #include file="FGII.inc" -->
<%
protectRequest.validateToken();

var disc_keys = getQParamIntArray("discs", []);
var highlight_arr  = getQParamIntArray("highlight", []); // Comma-separated room_key_list (terrains). Optional
var floorKey = getQParamInt("vKey", -1);

var dwfPath = SetVerdPlanDWF(floorKey, 0/*inoutMode*/, 0);
if (dwfPath)
{
  dwfHandle = GetContoursHandle(dwfPath, S("fg_contourlayers"), S("fg_labellayers"),
                                         { minArea: S("fg_contour_minArea")
                                         , minmergedistance: S("fg_minmergedistance")
                                         , flags: S("fg_slnkdwf_flags")
                                        });
}

// ==========================
//
// ==========================
if (floorKey > 0 && disc_keys.length)
{
  var sql = "SELECT ins_deel_key, ins_deel_omschrijving, ins_deel_dwgX, ins_deel_dwgY, "
          + "       alg_ruimte_nr, cadlabel"
          + "  FROM ins_deel id, alg_ruimte ar, ins_srtdeel isd,"
          +         S("fg_syncruimteview") + " sync"
          + " WHERE id.ins_srtdeel_key = isd.ins_srtdeel_key"
          + "   AND ins_deel_verwijder IS NULL"
          + "   AND id.ins_alg_ruimte_key = ar.alg_ruimte_key"
          + "   AND ar.alg_verdieping_key = " + floorKey
          + "   AND sync.alg_verdieping_key = " + floorKey
          + "   AND sync.alg_ruimte_key = ar.alg_ruimte_key"
          + "   AND id.ins_deel_dwgX iS NOT NULL and id.ins_deel_dwgY IS NOT NULL"
          + "   AND ins_discipline_key in (" + disc_keys + ")"
          + " ORDER BY ins_deel_key";
}
else
{
  var sql = "SELECT * FROM DUAL WHERE 1=0";
}

var cnt = 0;
var oRs = Oracle.Execute(sql)
while (!oRs.Eof)
{
    var deel_key = oRs("ins_deel_key").Value;
    var rnr = dwfHandle.FindInContour(oRs("ins_deel_dwgX").value, oRs("ins_deel_dwgY").value);
    if (rnr && rnr != oRs("cadlabel").value)
    {
      {  // via sync-view vertalen naar herkenbaar ruimtelabel
        var sql = "SELECT ar.alg_ruimte_key, alg_ruimte_nr"
                + "  FROM alg_ruimte ar,"
                +         S("fg_syncruimteview") + " sync"
                + " WHERE ar.alg_verdieping_key = " + floorKey
                + "   AND sync.alg_verdieping_key = " + floorKey
                + "   AND sync.alg_ruimte_key = ar.alg_ruimte_key"
                + "   AND cadlabel = " + safe.quoted_sql(rnr);
        var oRs2 = Oracle.Execute(sql);
        if (oRs2.EOF)
        {
          // Onbekende ruimte negeren
        }
        else
        {
          var alg_ruimte_key = oRs2("alg_ruimte_key").Value;
          var alg_ruimte_nr = oRs2("alg_ruimte_nr").Value;
          sql = "UPDATE ins_deel"
              + "   SET ins_alg_ruimte_key = " + oRs2("alg_ruimte_key").Value
              + " WHERE ins_deel_key = " + deel_key;
          Oracle.Execute(sql);
          tracktekst = L("lcl_ins_cad_moved") + ": " + buildTrackText("varchar", oRs("alg_ruimte_nr").Value, alg_ruimte_nr, { nodiff: true });
          shared.trackaction("INSUPD", deel_key, tracktekst);
          cnt++;
        }
        oRs2.Close();
      }
    }
    oRs.MoveNext();
}
oRs.Close();

var warning = "";
if (cnt > 0)
    warning = L("lcl_fg_consi_resetdone").format(cnt);

Response.Write(JSON.stringify({ success: true, warning: warning }));

ASPPAGE_END();
 %>
