<% /*
     $Revision$
     $Id$
    File:               alg_ruimte.inc
    Description:        Show an overview of ruimte items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from alg_ruimte_search_list.asp form
    Note:
*/ %>

<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../Shared/getkenmerksql.inc" -->
<!-- #include file="alg.inc" -->

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

function ruimte_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var addButton = [];
  var authparams = alg.checkAutorisation();
  // Met ALG* bepaal je wat je ziet (Read), met RUI* en TER* bepaal je wat je kunt wijzigen (Write).
  var canAdd = (authparams.rmALGwritelevel < 9);

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var noref = params.noref;
  var reg_key = params.reg_key;
  var dis_key = params.dis_key;
  var loc_key = params.loc_key;
  var bld_key = params.bld_key;
  var lvl_key = params.lvl_key;
  var room_key = params.room_key;
  var area_nr = params.area_nr;
  var area_descr = params.area_descr;
  var srtruimte_key = params.srtruimte_key
  var dept_key = params.dept_key;
  var chkgeb = params.chkgeb;
%>

  <html>
    <head>
      <% FCLTHeader.Generate({outputmode: outputmode}); %>
      <script type="text/javascript">
        var autosearch = 1; // automatisch laden van resultaat op search pagina

        function gotoDetails(row)
        {
          var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
          var url = "appl/alg/alg_werkplek_search.asp?ruimte_key="+algData.detail_key+"&autosearch="+autosearch;
          FcltMgr.openDetail(url, "<%=L("lcl_workspace")%>");
        }

        function gotoParent(row)
        {
          var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
          var url = "appl/alg/alg_verdieping_search.asp?verdieping_key="+algData.parent_key+"&autosearch="+autosearch;
          FcltMgr.openDetail(url, "<%=L("lcl_floor")%>");
        }

        function ruimteEdit(row)
        {
          var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
          var url = "appl/alg/alg_ruimte.asp?mode=viewUpdate&key="+algData.key;
          FcltMgr.openDetail(url, "<%=L("lcl_room")%>" + ' ' + algData.oms);
        }

        function ruimteEditMulti(rowArray)
        {
          var algKeyString = getKeyString(rowArray);
          var subject = L("lcl_alg_multi_edit");
          var url = "../alg/alg_edit_ruimte_multi.asp?alg_level=R&alg_keys=" + algKeyString;
          FcltMgr.openModalDetail(url, subject, {callback: FcltCallbackRefresh});
        }

        function doRuimteAfdeling(rowArray)
        {
          var ruimteArr = [];
          for (var i = 0; i < rowArray.length; i++)
          {
            ruimteArr.push(rowArray[i].getAttribute("ROWKEY"))
          }
          FcltMgr.openModalDetail("alg_ruimteafdeling.asp?ruimte_keys=" + ruimteArr.join(","), L("lcl_alg_ruimte_bezetting"));
        }

        function doRESScope(rowArray)
        {
          var ruimteArr = [];
          for (var i = 0; i < rowArray.length; i++)
          {
            ruimteArr.push(rowArray[i].getAttribute("ROWKEY"))
          }
          FcltMgr.openModalDetail("alg_ruimteresscope.asp?ruimte_keys=" + ruimteArr.join(","), L("res_srtartikel_onrgoed"));
        }

        function doDelete(row, isMulti)
        {
          FcltMgr.confirm(L("lcl_alg_del_txt_ruimte"), { autoconfirm: isMulti }, function() {
            $(row).addClass('dirty');
            var room_key = row.getAttribute("ROWKEY");
            var data = { key: room_key,
                         level: "R"
                       };
            <% protectRequest.dataToken("data"); %>
            $.post("alg_delete.asp", data, FcltCallbackRefresh, "json");
          });
        }
      </script>
    </head>

    <body id="listbody">
<%    var sqln = "SELECT r.alg_ruimte_key"
               + ",      v.alg_verdieping_key"
               + ",      v.alg_gebouw_key"
               + ",      g.alg_locatie_key"
               + ",      l.alg_locatie_omschrijving"
               + ",      g.alg_gebouw_omschrijving"
               + ",      v.alg_verdieping_omschrijving"
               + ",      r.alg_srtruimte_key"
               + ", "+lcl.xsqla("s.alg_srtruimte_omschrijving", "s.alg_srtruimte_key")
               + ",      r.alg_ruimte_nr"
               + ",      r.alg_ruimte_omschrijving"
               + ",      r.alg_ruimte_bruto_vloeropp"
               + ",      r.alg_ruimte_opmerking"
               + ",      r.alg_ruimte_omtrek"
               + ",      r.alg_ruimte_inhoud"
               + ",      v.alg_verdieping_code"
               + ",      r.alg_ruimte_opp_alt1"
               + ",      r.alg_ruimte_opp_alt2"
               + "  FROM alg_v_aanwezigruimte r"
               + ",      alg_verdieping v"
               + ",      alg_gebouw g"
               + ",      alg_locatie l"
               + ",      alg_srtruimte s"
               + " WHERE v.alg_verdieping_key = r.alg_verdieping_key "
               + "   AND g.alg_gebouw_key = v.alg_gebouw_key "
               + "   AND l.alg_locatie_key(+) = g.alg_locatie_key "
               + "   AND s.alg_srtruimte_key(+) = r.alg_srtruimte_key "
               + "   AND v.alg_verdieping_verwijder IS NULL ";

      if (authparams.ALGreadlevel > -1)
      { // Er is een scope-beperking van kracht
        sqln += " AND r.alg_ruimte_key IN"
                + " (SELECT alg_ruimte_key FROM fac_v_my_rooms"
                + " WHERE prs_perslid_key = " + user_key
                + " AND niveau = " + authparams.ALGreadlevel + ")";
      }

      sqln += getKenmerkSql("ALG", "r.alg_ruimte_key");

      if (room_key)
      {
        sqln += " AND r.alg_ruimte_key = " + room_key;
      }
      else if (lvl_key)
      {
        sqln += " AND v.alg_verdieping_key = " + lvl_key;
      }
      else if (bld_key)
      {
        sqln += " AND v.alg_gebouw_key = " + bld_key;
      }
      else if (loc_key)
      {
        sqln += " AND g.alg_locatie_key = " + loc_key;
      }
      else if (dis_key)
        sqln += " AND l.alg_district_key = " + dis_key;
      else if (reg_key)
        sqln += " AND l.alg_district_key IN (SELECT alg_district_key FROM alg_district WHERE alg_regio_key = " + reg_key+")";

      if (srtruimte_key)
      {
        sqln += " AND r.alg_srtruimte_key = " + srtruimte_key;
      }

      if (area_nr)
      {
        sqln += " AND UPPER(r.alg_ruimte_nr) LIKE " + safe.quoted_sql_wild("%" + area_nr + "%");
      }

      if (area_descr)
      {
        sqln += " AND UPPER(r.alg_ruimte_omschrijving) LIKE " + safe.quoted_sql_wild("%" + area_descr + "%");
      }

      if (dept_key)
      {
        sqln += " AND r.alg_ruimte_key in "
             +  "                    (SELECT alg_ruimte_key "
             +  "                      FROM prs_ruimteafdeling "
             +  "                     WHERE prs_ruimteafdeling_verwijder IS NULL "
             +  "                       AND prs_afdeling_key = " + dept_key + ")";
      }

      if (!chkgeb)
        sqln += " AND (g.alg_gebouw_vervaldatum IS NULL OR g.alg_gebouw_vervaldatum > TRUNC(SYSDATE)) ";

      sqln += " ORDER BY l.alg_locatie_upper ASC"
            + ",         g.alg_gebouw_upper ASC"
            + ",         v.alg_verdieping_volgnr ASC"
            + ",         r.alg_ruimte_upper_nr ASC"

      function fnrowData(oRs)
      {
        var parent_key = oRs("alg_verdieping_key").Value;
        var detail_key = oRs("alg_ruimte_key").Value;
        var key = oRs("alg_ruimte_key").Value;
        var oms = oRs("alg_ruimte_omschrijving").Value;

        var data = {parent_key:parent_key, detail_key:detail_key, key:key, oms:oms};
        return JSON.stringify(data);
      }

      var floorCache = [];
      // Voor performance bekijken we de rechten per verdieping. In de praktijk nauwkeurig genoeg
      function fnrowActionEnabler(oRs)
      {
        if (!floorCache[oRs("alg_verdieping_key").Value])
        {
          eEditMulti = true;
            floorCache[oRs("alg_verdieping_key").Value] =
                {
                    eDelete: alg.canWriteVerdieping(oRs("alg_verdieping_key").Value, authparams.rmALGwritelevel),
                    // Bezetting moet per regel en is afhankelijk van WEB_PRSBOF (werkplekbeheer)
                    eBezetting: alg.func_enabled_ruimte(oRs("alg_verdieping_key").Value).canPRSBOFwrite,
                    eEditMulti: eEditMulti
                }
        }
        return (floorCache[oRs("alg_verdieping_key").Value]);
      }

      function fnrowFlexParams(oRs)
      {
        var room_key = oRs("alg_ruimte_key").Value;
        var this_alg = alg.func_enabled(room_key, "R");

        var flexParams = { readman: this_alg.readman, readuse: this_alg.readuse};
        return flexParams;
      }

      var addurl = "appl/alg/alg_ruimte.asp?a=1";
      addurl += buildTransitParam(["locatiekey", "gebouwkey", "verdiepingkey", "ruimtekey"], params)  // TODO: welke allemaal?

      if (canAdd)
      {
        addButton = [{ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "')" }]
      }

      var rst = new ResultsetTable({ sql: sqln,
                                     keyColumn: "alg_ruimte_key",
                                     ID: "algtable",
                                     title: L("lcl_menu_alg_ruimte"),
                                     showAll: showall,
                                     rowData: fnrowData,
                                     rowActionEnabler: fnrowActionEnabler,
                                     outputmode: outputmode,
                                     flexModule: "ALG",
                                     flexId: "alg_ruimte_key",
                                     flexParams: fnrowFlexParams,
                                     filterParams: params,
                                     buttons: addButton
                                   });

      rst.addColumn(new Column({caption: L("lcl_estate_locatie_man_descr"), content: "alg_locatie_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_estate_gebouw_man_descr"), content: "alg_gebouw_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_estate_verdieping_man_descr"), content: "alg_verdieping_omschrijving"}));

      rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_nr"), content: "alg_ruimte_nr"}));

      rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_descr"), content: "alg_ruimte_omschrijving" }));
      rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_sort"), content: "alg_srtruimte_omschrijving"}));
      rst.addColumn(new Column({caption: L("lcl_estate_ruimte_bruto_vloeropp"), content: "alg_ruimte_bruto_vloeropp", datatype: "float", decimals: 2 }));

      if (S("fg_contourlayers_alt1") != "" && S("fg_labellayers_alt1") != "")
      {
        rst.addColumn(new Column({caption: L("lcl_estate_ruimte_opp_alt1"), content: "alg_ruimte_opp_alt1", datatype: "float", decimals: 2 }));
      }

      if (S("fg_contourlayers_alt2") != "" && S("fg_labellayers_alt2") != "")
      {
        rst.addColumn(new Column({caption: L("lcl_estate_ruimte_opp_alt2"), content: "alg_ruimte_opp_alt2", datatype: "float", decimals: 2 }));
      }


      if (outputmode != 0) // to be completed
      {
        rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_opmerk"), content: "alg_ruimte_opmerking"}));
        rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_omtrek"), content: "alg_ruimte_omtrek"}));
        rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_inhoud"), content: "alg_ruimte_inhoud"}));
      }

      rst.addAction({ action: "ruimteEdit", caption: L("lcl_edit"), isDefault:  true });
      rst.addAction({ action: "ruimteEditMulti", caption: L("lcl_alg_multi_edit"), multi: true, multiOnce: true, single: false, enabler: "eEditMulti"});
      rst.addAction({ action: "doDelete", caption: L("lcl_delete"), multi: true, single: !noref, enabler: "eDelete" });
      rst.addAction({ action: "doRuimteAfdeling", caption: L("lcl_alg_bezetting"), multi: true, multiOnce: true, enabler: "eBezetting" });
      if (user.has("WEB_RESMSU"))
        rst.addAction({ action: "doRESScope", caption: L("res_srtartikel_onrgoed"), multi: true, single: false, multiOnce: true });
      if (!noref)
      {
        if (S("prs_werkplek_implicit") == 0)
            rst.addAction({ action: "gotoDetails", caption: L("lcl_goto_werkplek_list") });
        rst.addAction({ action: "gotoParent", caption: L("lcl_goto_verdieping_list") });
      }
      var cnt = rst.processResultset();
%>
    </body>
  </html>
<%
}
%>

