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

     Geeft een overzicht van de ruimtes in dit scenario

*/%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../CAD/FGII.inc" -->
<!-- #include file="../Shared/json2.js" -->

<%
var highlight = getQParamIntArray("highlight", []); // Comma-separated room_key_list (terrains). Optional
var vKey = getQParamInt("vKey", ""); // Verdieping key
var scenario_key = getQParamInt("scenario_key"); // current scenario.
var outputmode = getQParamInt("outputmode", 0);
var showall = getQParamInt("showall", 0) == 1;
var doremove = getQParamInt("doremove", 0) == 1;
var doadd = getQParamInt("doadd", 0) == 1;



var cad_tek_key = -1; // -1: er is geen aparte project-tekening

sql = "SELECT cad_tekening_key"
    + "     , cad_tekening_filenaam"
    + "  FROM cad_tekening ct"
    + " WHERE cad_tekening_verwijder IS NULL"
    + "   AND cad_tekening_type = 'P'"
    + "   AND alg_verdieping_key = " + vKey
oRs = Oracle.Execute(sql);
if (!oRs.eof)
{
  normalDWG = FindDWF("" + oRs("cad_tekening_filenaam").value);
  prjDWG = FindDWF("" + oRs("cad_tekening_filenaam").value, scenario_key );
  if (normalDWG != prjDWG)
    cad_tek_key = oRs("cad_tekening_key");
}

var sql = "";
if (vKey !="")
{
  sql = "SELECT x.prj_ruimte_key, x.alg_ruimte_aanduiding, x.alg_srtruimte_omschrijving"
      + ", x.prj_ruimte_bruto_vloeropp, x.prj_ruimte_omschrijving, x.cad_imp_contour_opp"
      + ", DECODE (y.prs_dep_string_new, NULL, '[Leegstand]', y.prs_dep_string_new) prs_dep_new"
      + ", DECODE (z.count_old, NULL, DECODE (y.prs_dep_string_new, NULL, '=', y.prs_dep_string_new)"
      + ", 1, DECODE (z.prs_dep_string_old, y.prs_dep_string_new, '=', z.prs_dep_string_old)"
      + ", z.prs_dep_string_old || ', ...') prs_dep_old FROM "
      + " (SELECT   prj_r.prj_ruimte_key, alg_g.alg_gebouw_code"
        + " ||'-'"
        + " ||TO_CHAR (alg_v.alg_verdieping_code)"
        + " ||'-'"
        + " ||prj_r.prj_ruimte_nr alg_ruimte_aanduiding"
      + ", "+ lcl.xsqla("alg_s.alg_srtruimte_omschrijving", "alg_s.alg_srtruimte_key")
      + ", prj_ruimte_bruto_vloeropp prj_ruimte_bruto_vloeropp"
      + ", prj_ruimte_omschrijving prj_ruimte_omschrijving"
      + ", c.cad_imp_contour_opp cad_imp_contour_opp"
      + " FROM prj_ruimte prj_r"
      + ", alg_verdieping alg_v"
      + ", alg_gebouw alg_g"
      + ", alg_srtruimte alg_s"
      + ", " + S("fg_prj_syncruimteview") + " sync"
      + ", cad_imp_contour c"
      + " WHERE  prj_r.prj_scenario_key = " + scenario_key
      + " AND prj_r.alg_verdieping_key = " + vKey
      + " AND alg_v.alg_verdieping_key = prj_r.alg_verdieping_key"
      + " AND alg_g.alg_gebouw_key = alg_v.alg_gebouw_key"
      + " AND prj_r.alg_srtruimte_key = alg_s.alg_srtruimte_key(+)"
      + " AND prj_r.alg_verdieping_key = sync.alg_verdieping_key(+) "
      + " AND c.CAD_IMP_CONTOUR_NR(+) = sync.CADLABEL "
      + " AND c.prj_scenario_key(+) = " + scenario_key
      + " AND sync.prj_ruimte_key(+) = prj_r.prj_ruimte_key"
      + " AND prj_r.prj_ruimte_verwijder IS NULL"
      + ((highlight.length > 0 && highlight.join(",") != "-1")?" AND prj_r.prj_ruimte_key IN (" + highlight.join(",") + ")":"")
      + " ) x"
      + " LEFT JOIN"
      + " (SELECT prj_r.prj_ruimte_key," + S("prs_dep_string") + " prs_dep_string_new"
      + " FROM prj_ruimte prj_r, prj_ruimteafdeling prj_ra, prs_v_afdeling d"
      + " WHERE  prj_r.prj_scenario_key = " + scenario_key
      + " AND prj_r.alg_verdieping_key = " + vKey
      + " AND prj_r.prj_ruimte_key = prj_ra.prj_ruimte_key"
      + " AND prj_ra.prs_afdeling_key = d.prs_afdeling_key"
      + " AND prj_ra.prj_ruimteafdeling_verwijder IS NULL"
      + " ) y"
      + " ON x.prj_ruimte_key = y.prj_ruimte_key"
      + " LEFT JOIN"
      + " (SELECT prj_r.prj_ruimte_key, MIN(" + S("prs_dep_string") + ") prs_dep_string_old"
      + ", COUNT(prj_r.prj_ruimte_key) count_old"
      + " FROM prj_ruimte prj_r, alg_ruimte alg_r, prs_ruimteafdeling prs_ra, prs_v_afdeling d"
      + " WHERE  prj_r.prj_scenario_key = " + scenario_key
      + " AND prj_r.alg_verdieping_key = " + vKey
      + " AND prj_r.alg_ruimte_key = alg_r.alg_ruimte_key"
      + " AND alg_r.alg_ruimte_key = prs_ra.alg_ruimte_key"
      + " AND prs_ra.prs_afdeling_key = d.prs_afdeling_key"
      + " AND prs_ra.prs_ruimteafdeling_verwijder IS NULL"
      + " GROUP BY prj_r.prj_ruimte_key"
      + " ) z"
      + " ON x.prj_ruimte_key = z.prj_ruimte_key"
      + " ORDER BY x.alg_ruimte_aanduiding, y.prs_dep_string_new";
}
else
{
  sql = "SELECT * FROM DUAL WHERE 1 = 0";
}
%>
<html>
  <head>
<%  FCLTHeader.Generate({ outputmode: outputmode }); %>
    <script type="text/javascript">

      $(document).ready(function()
                             {
                               PerhapsRefresh();
                             });

      function onClickRuimteAlternatief(row)
      {
        var room_key = row.getAttribute("ROWKEY");
        sl = parent.parent.document.getElementById("rooms");
        if (sl.selectedIndex == -1 || sl.options[sl.selectedIndex].value != room_key)
        {
          for (i = 0; i < sl.length; i++)
          {
            if (sl.options[i].value == room_key)
            {
              sl.selectedIndex = i;
              parent.parent.OnRoomChanged(sl);
            }
          }
        }
      }

      function onRemoveRuimte(prj_ruimte_key)
      {
        if (confirm(L('lcl_prj_room_delete')))
        {
          alert(L('lcl_prj_not_supported'))
          return true;
          var data = { actie : 'doremove'
                     , room_key : prj_ruimte_key
                    };
          <% protectRequest.dataToken("data"); %>
          $.post("rap_prj7_save.asp",
                 data,
                 FcltCallback,
                 "json");
        }
      }

      function onAddRuimte(row)
      {
        var cad_imp_key = row.getAttribute("ROWKEY");
        if (confirm(L('lcl_prj_room_create')))
        {
          var data = { actie : 'doadd'
                     , scenariokey : <%=scenario_key%>
                     , vkey : <%=vKey%>
                     , cad_imp_key : cad_imp_key
                    };
          <% protectRequest.dataToken("data"); %>
          $.post("rap_prj7_save.asp",
                 data,
                 FcltCallback,
                 "json");
        }
      }

      function editRoom(roomKey)
      {
        var sFeatures="dialogHeight:230px;dialogWidth:600px;status=no;scroll=no";
      }

      function onClickRuimte(row)
      {
        var url='../prj/edit_room.asp?room_key=' + row.getAttribute("ROWKEY");
        FcltMgr.openModalDetail(url, "<%=L("lcl_prj_graphics")%>",
             { callback: function (data) { if (data.refresh)
                            parent.myRefresh(); // Er is misschien iets veranderd.
                                                 // Pas plaatje aan. En indirect onszelf.
             }})
      }

      function scanDWF()
      {
        if (confirm(L('lcl_prj_floor_dwfscan')))
        {
          var data = { scenariokey : <%=scenario_key%>
                     , tKey : <%=cad_tek_key%>
                     , vKey : <%=vKey%>
                     };
          <% protectRequest.dataToken("data"); %>
          $.post("../prj/RuimteSync.asp",
                 data,
                 FcltCallbackRefresh,
                 "json");
        }
      }

      function PerhapsRefresh()
      {
<%      if (doadd ||
            doremove)
        { %>
          parent.parent.parent.parent.myRefresh();
<%      } %>
      }
    </script>
  </head>

  <body class="prjtabpage">
<%
    var lastValue = null;
    function showRoomDesc(oRs)
    {
      if (oRs("alg_ruimte_aanduiding").value == lastValue)
        var result = null;
      else
        var result = oRs("alg_ruimte_aanduiding").value;
      lastValue = oRs("alg_ruimte_aanduiding").value;
      return result;
    }

    function formatdiff(oRs)
    {
      var diff
      if (oRs("cad_imp_contour_opp").value == null)
        diff =  -oRs("prj_ruimte_bruto_vloeropp").value;
      else
        diff = oRs("cad_imp_contour_opp").value - oRs("prj_ruimte_bruto_vloeropp").value;
// trick: sum these values for later by calling increasetotaldiff
//    increasetotaldiff (diff);
      if (Math.abs(diff) < 0.02)
        return "";
      else
        return diff.toFixed(2);
    }

    function RemoveRuimte(oRs) //
    {
      if (oRs("cad_imp_contour_opp").value != null) // Gewoon gevonden Ruimte
        return oRs("cad_imp_contour_opp").value;
      else
        return   "<img onclick='onRemoveRuimte(" + oRs("prj_ruimte_key").value + ")'"
               + "    src='../pictures/delete.gif' title='Niet in tekening. Laat vervallen'>"
    }

    var rst = new ResultsetTable({ keyColumn: "prj_ruimte_key",
                                   sql: sql,
                                   ID: "workTable",
                                   outputmode: outputmode,
                                   title: L("lcl_prj_verhuis_scenarios"),
                                   showAll: showall
                                 });

    rst.addColumn(new Column({caption: L("lcl_obj_inf_room"), content: showRoomDesc}));
    rst.addColumn(new Column({caption: L("lcl_fg_func_names"), content: "alg_srtruimte_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_opp"), content: "prj_ruimte_bruto_vloeropp", datatype: "float"}));

    rst.addAction({action: "onClickRuimte", caption: L("lcl_prj_details"), isDefault: true});

    if (cad_tek_key != -1) // Er is een project tekening
    {
      rst.addColumn(new Column({caption: L("lcl_fg_tek_room_opp_g"), content: RemoveRuimte}));
      rst.addColumn(new Column({caption: L("lcl_fg_tek_room_opp_dif"), content: formatdiff, datatype: "float"}));
    }
    rst.addColumn(new Column({caption: L("lcl_descr"), content: "prj_ruimte_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_fg_dep_new"), content: "prs_dep_new"}));
    rst.addColumn(new Column({caption: L("lcl_fg_dep_current"), content: "prs_dep_old"}));

    var cnt = rst.processResultset();

    if (cad_tek_key != -1) // Er is een project tekening
    {
%>
      <input type=button value='<%=L("lcl_fg_tek_scan")%>' onClick='scanDWF();'>
    <%

      // ==========================
      // Ruimtes wel in tekening maar niet in database
      // ==========================
      sql = "SELECT C.CAD_IMP_CONTOUR_KEY, C.CAD_IMP_CONTOUR_NR, c.cad_imp_contour_opp "
          + " FROM cad_imp_contour c, cad_tekening t"
          + " WHERE c.cad_tekening_key = t.cad_tekening_key"
          + "   AND c.prj_scenario_key = " + scenario_key
          + "   AND t.alg_verdieping_key = " + vKey
          + "   AND NOT EXISTS ("
          + "     SELECT *"
          + "       FROM " + S("fg_prj_syncruimteview") + " sync, prj_ruimte"
          + "      WHERE t.alg_verdieping_key = " + vKey
          + "        AND sync.alg_verdieping_key = " + vKey
          + "        AND c.cad_imp_contour_nr = sync.cadlabel "
      	  + "        AND sync.prj_ruimte_key = prj_ruimte.prj_ruimte_key)";


      var rst = new ResultsetTable({ keyColumn: "cad_imp_contour_key",
                                     sql: sql,
                                     ID: "workTable2",
                                     outputmode: outputmode,
                                     title: L("lcl_prj_verhuis_scenarios"),
                                     showAll: showall
                                   });

      rst.addColumn(new Column({caption: L("lcl_prj_not_database"), content: "cad_imp_contour_nr"}));
      rst.addColumn(new Column({caption: L("lcl_opp"), content: "cad_imp_contour_opp", datatype: "float"}));

      rst.addAction({ action: "onAddRuimte", caption: L("lcl_prj_details"), isDefault: true });

      var cnt = rst.processResultset();
    }
%>
  </body>
</html>

