<% /*
     $Revision$
     $Id$
    File:               alg_verdieping.inc
    Description:        Show an overview of verdieping items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from alg_verdieping_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"] });

function fnrowData(oRs)
{
  var parent_key = oRs("alg_gebouw_key").value;
  var detail_key = oRs("alg_verdieping_key").value;
  var key        = oRs("alg_verdieping_key").value;
  var oms        = oRs("alg_verdieping_omschrijving").value

  var data = {parent_key: parent_key, detail_key: detail_key, key: key, oms: oms};

  return JSON.stringify(data);
}
%>

<script type="text/javascript">
  var autosearch = 1; //automatische laden van resultaat op search pagina

  function gotoDetails(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_ruimte_search.asp?verdieping_key=" + algData.detail_key + "&autosearch=" + autosearch + "&chkgeb=1";
    FcltMgr.openDetail(url, "<%=L("lcl_room")%>");
  }
  function gotoParent(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_gebouw_search.asp?gebouw_key=" + algData.parent_key + "&autosearch=" + autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_building")%>");
  }
    function verdiepingEdit(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_verdieping.asp?mode=viewUpdate&key=" + algData.key;
    FcltMgr.openDetail(url, "<%=L("lcl_floor")%>" + ' ' + algData.oms);
  }

  function doDelete(rowArray, isMulti)
  {
    FcltMgr.confirm(L("lcl_alg_del_txt_verdieping"), { autoconfirm: isMulti }, function() {
      var flrKeyString = getKeyString(rowArray);
      var data = { key: flrKeyString,
                   level: "V"
                 };
      <% protectRequest.dataToken("data"); %>
      $.post("alg_delete.asp", data, FcltCallbackRefresh, "json");
    });
  }

</script>
<%

function verdiepingen_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();
  var canAdd = (authparams.mALGwritelevel < 5);

  // 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 chkgeb = params.chkgeb;

  function fnrowActionEnabler(oRs)
  {
    var eDelete = false;
    if (alg.canWriteVerdieping(oRs("alg_verdieping_key").value, authparams.mALGwritelevel))
        eDelete = true;
    return ({eDelete: eDelete})
  }

  function fnrowFlexParams(oRs)
  {
    var ver_key = oRs("alg_verdieping_key").Value;
    var this_alg = alg.func_enabled(ver_key, "V");

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

  <html>
    <head>
      <% FCLTHeader.Generate({outputmode: outputmode}); %>
    </head>
    <body id="listbody">
<%
  var sqln = "SELECT v.alg_verdieping_key, "
       + "       v.alg_gebouw_key, "
       + "       g.alg_locatie_key, "
       + "       l.alg_locatie_omschrijving, "
       + "       g.alg_gebouw_omschrijving, "
       + "       v.alg_verdieping_omschrijving, "
       + "       v.alg_verdieping_code  "
       + "  FROM alg_v_aanwezigverdieping v, "
       + "       alg_gebouw g, "
       + "       alg_locatie l  "
       + " WHERE g.alg_gebouw_key = v.alg_gebouw_key "
       + "   AND l.alg_locatie_key(+) = g.alg_locatie_key "
       + "   AND v.alg_verdieping_verwijder IS NULL ";

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

   sqln += getKenmerkSql("ALG", "v.alg_verdieping_key");

   if (lvl_key)
      {
      sqln += " AND v.alg_verdieping_key = " + lvl_key;
      }

   else if (bld_key)
      {
      sqln += " AND g.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 (!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 ";

    var addurl = "appl/alg/alg_verdieping.asp";
//    addurl += buildTransitParam(["loc_key", "bld_key", "flo_key", "room_key"], 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_verdieping_key",
                                   ID: "algtable",
                                   title: L("lcl_menu_alg_verdieping"),
                                   showAll: showall,
                                   outputmode: outputmode,
                                   flexModule: "ALG",
                                   flexId: "alg_verdieping_key",
                                   flexParams: fnrowFlexParams,
                                   filterParams: params,
                                   rowData: fnrowData,
                                   rowActionEnabler: fnrowActionEnabler,
                                   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_code"), content: "alg_verdieping_code"}));
    rst.addColumn(new Column({caption: L("lcl_estate_verdieping_man_descr"), content: "alg_verdieping_omschrijving" }));

    rst.addAction({ action: "verdiepingEdit", caption: L("lcl_edit"), isDefault:  true});
    rst.addAction({ action: "doDelete", caption: L("lcl_delete"), enabler: "eDelete", single: !noref, multi: true, multiOnce: true});
    if (!noref) {
        rst.addAction({ action: "gotoDetails", caption: L("lcl_goto_ruimte_list")});
        rst.addAction({ action: "gotoParent", caption: L("lcl_goto_gebouw_list")});
    }
    var cnt = rst.processResultset();
%>
    </body>
  </html>
<%}%>

