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

<!-- #include file="../../appl/Shared/common.inc"-->
<!-- #include file="../../appl/Shared/resultset_table_v2.inc" -->

<%
var sql = "";
var floorKey   = getQParamInt("vKey", -1);
var highlight_arr = getQParamIntArray("highlight", []);
var outputmode = getQParamInt("outputmode", 0);
var showall    = getQParamInt("showall", 0) == 1;
%>

<html>
  <head>
<%  FCLTHeader.Generate({ outputmode: outputmode, root: "../../appl/" }); %>
    <script>

      function onClickRuimte(room_key)
      {
        fwnd = window.location.href = '../../estate/ruimteafdeling.asp?rkey='+room_key, 'FGRoomAfdeling',
                "width=800,height=250,directories=no,location=no,menubar=no,"
                + "resizable=yes,status=no,titlebar=yes,toolbar=no, scrollbars=yes";
      }
    </script>
  </head>

  <body class="tabpage">
    <form name='u2' target='print_preview_composer' method=post
        action='<%=Request.ServerVariables("SCRIPT_NAME")%>?<%=Request.ServerVariables("QUERY_STRING")%>&print'>
<%
      if (floorKey != -1)
      {
        sql = " SELECT r.alg_ruimte_key"
            + "      , r.alg_ruimte_aanduiding"
            + "      , r.alg_ruimte_omschrijving"
            + "      , r.alg_srtruimte_omschrijving"
            + "      , r.alg_ruimte_bruto_vloeropp"
            + "      , rg.waarde"
            + "      , r.alg_ruimte_opp_alt1, r.alg_ruimte_opp_alt2"
            + "      , (SELECT ROUND(100 * ra.prs_ruimteafdeling_bezetting / DECODE(SUM(ra2.prs_ruimteafdeling_bezetting),0,1,SUM(ra2.prs_ruimteafdeling_bezetting)))"
            + "	        FROM prs_ruimteafdeling ra2"
            + "	       WHERE ra.alg_ruimte_key = ra2.alg_ruimte_key"
            + "	      GROUP BY ra.alg_ruimte_key) bezetting"
            + "      , (SELECT " + S("prs_dep_string")
            + "           FROM prs_v_afdeling d, prs_v_afdeling_boom b"
            + "          WHERE ra.prs_afdeling_key = b.prs_afdeling_key"
            + "            AND d.prs_afdeling_key = b.prs_afdeling_key1) divisie"
            + "      , (SELECT " + S("prs_dep_string")
            + "           FROM prs_v_afdeling d"
            + "          WHERE d.prs_afdeling_key = ra.prs_afdeling_key"
            + "            AND   d.prs_afdeling_parentkey is not null) afdeling"
            + "   FROM (SELECT alg_r.alg_ruimte_key alg_ruimte_key"
            + "              , alg_r.alg_ruimte_omschrijving alg_ruimte_omschrijving"
            + "              , alg_s.alg_srtruimte_omschrijving alg_srtruimte_omschrijving"
            + "              , alg_r.alg_ruimte_bruto_vloeropp"
            + "              , alg_r.alg_ruimte_opp_alt1"
            + "              , alg_r.alg_ruimte_opp_alt2"
            + "              ,  alg_g.alg_gebouw_code || '-' || TO_CHAR (alg_v.alg_verdieping_code) || '-' || alg_r.alg_ruimte_nr alg_ruimte_aanduiding"
            + "           FROM alg_gebouw alg_g"
            + "              , alg_verdieping alg_v"
            + "              , alg_v_aanwezigruimte alg_r"
            + "              , alg_srtruimte alg_s"
            + "          WHERE alg_v.alg_verdieping_key = " + floorKey
            + "            AND alg_v.alg_verdieping_key = alg_r.alg_verdieping_key"
            + "            AND alg_g.alg_gebouw_key = alg_v.alg_gebouw_key"
            + "            AND alg_r.alg_srtruimte_key = alg_s.alg_srtruimte_key(+)) r"
            + "      , prs_ruimteafdeling ra"
            + "      , aads_v_thema_ruimte_huurder rg"
            + " WHERE ra.alg_ruimte_key(+) = r.alg_ruimte_key"
            + "   AND rg.alg_ruimte_key(+) = r.alg_ruimte_key"
            + (highlight_arr.length > 0 && highlight_arr.join(",").indexOf("-1") == -1
              ? " AND r.alg_ruimte_key IN (" + highlight_arr.join(",") + ")"
              : "")
            + " ORDER BY r.alg_ruimte_aanduiding, divisie, afdeling";
      }
      else
      { // Er is (nog) geen verdieping bekend
        sql = "SELECT * FROM DUAL WHERE 1 = 0";
      }

      var lastValue = null;
      function showRoomDesc(oRs)
      {
        if (safe.htmlattr(oRs("alg_ruimte_aanduiding").value) == lastValue)
        {
          var result = null;
        }
        else
        {
          // var result = oRs("alg_ruimte_aanduiding").value;
          var result = "<a title='Klik voor ruimteverdeling'"
                  			+ " href='javascript:onClickRuimte(\"" + oRs("alg_ruimte_key").value + "\")'>"
                  			+ safe.htmlattr(oRs("alg_ruimte_aanduiding").value) + "</a>";
        }
        lastValue = safe.htmlattr(oRs("alg_ruimte_aanduiding").value);
        return result;
      }

      function OppAndCountIt(oRs)
      {
        var opp =  (oRs("alg_ruimte_bruto_vloeropp").value != null? parseFloat(oRs("alg_ruimte_bruto_vloeropp").value) : 0);
        return opp;
      }

      var rst = new ResultsetTable({ sql: sql,
                                     ID : "cadalgtable",
                                     keyColumn: "alg_ruimte_key",
                                     title: L("lcl_space_manage_h"),
                                     outputmode: outputmode,
                                     showAll: showall
                                   });

      rst.addColumn(new Column({caption: L("lcl_obj_inf_room"), content: showRoomDesc}));
      rst.addColumn(new Column({caption: "Ruimteomschrijving", content: "alg_ruimte_omschrijving"}));
      rst.addColumn(new Column({caption: "Ruimtesoort", content: "alg_srtruimte_omschrijving"}));
      rst.addColumn(new Column({caption: "Ruimtegebruik", content: "waarde"}));
      rst.addColumn(new Column({caption: "m2", content: OppAndCountIt, datatype: "float", total: true}));
      rst.addColumn(new Column({caption: "Code", content: "divisie"}));
      rst.addColumn(new Column({caption: L("lcl_department"), content: "afdeling"}));
      rst.addColumn(new Column({caption: "%", content: "bezetting"}));

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

