<% /*
     $Revision$
     $Id$
    File:               alg_gebouw.inc
    Description:        Show an overview of gebouw items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from alg_gebouw_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_locatie_key").value;
  var detail_key = oRs("alg_gebouw_key").value;
  var key        = oRs("alg_gebouw_key").value;
  var oms        = oRs("alg_gebouw_naam").value;

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

  return JSON.stringify(data);
}

var recent = new Date();
function fnrowClass(oRs)
{
  var lclass = "";
  // Tracking: hoe lang geleden voor het laatst gewijzigd?
  if (oRs.Fields("recentdatum").Value != null)
  {
    var ltrackdate1 = new Date();
    var ltrackdate10 = new Date();
    var ltrackdate100 = new Date();
    var alg_onrgoed_recent = S("alg_onrgoed_recent");
    ltrackdate1.setMinutes(recent.getMinutes() - alg_onrgoed_recent);
    ltrackdate10.setMinutes(recent.getMinutes() - alg_onrgoed_recent * 12);
    ltrackdate100.setMinutes(recent.getMinutes() - alg_onrgoed_recent*12*2);

    var fac_recent = new Date(oRs.Fields("recentdatum").Value);
    if (fac_recent > ltrackdate1)
    {
      lclass += " updated";
    }
    else if (fac_recent > ltrackdate10)
    {
      lclass += " updated10";
    }
    else if (fac_recent > ltrackdate100)
    {
      lclass += " updated100";
    }
  }
  return lclass;
}
%>
<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_verdieping_search.asp?gebouw_key=" + algData.detail_key + "&autosearch=" + autosearch + "&chkgeb=1";
    FcltMgr.openDetail(url, "<%=L("lcl_floor")%>");
  }
  function gotoParent(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_locatie_search.asp?locatie_key=" + algData.parent_key + "&autosearch=" + autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_location")%>");
  }
    function gebouwEdit(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_gebouw.asp?mode=viewUpdate&key=" + algData.key;
    FcltMgr.openDetail(url, "<%=L("lcl_building")%>" + ' ' + algData.oms);
  }

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

function gebouw_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 < 4);

  // 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 srtgebouw_key = params.srtgebouw_key;
  var verantw_key = params.verantw_key;
  var expalgincl = params.expalgincl;

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

  function fnrowFlexParams(oRs)
  {
    var bld_key = oRs("alg_gebouw_key").Value;
    var this_alg = alg.func_enabled(bld_key, "G");

    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 g.alg_gebouw_key"
           + "     , g.alg_locatie_key"
           + "     , g.alg_locatie_key"
           + "     , g.alg_srtgebouw_key"
           + "     , l.alg_locatie_omschrijving"
           + "     , " + lcl.xsqla('s.alg_srtgebouw_omschrijving', 's.alg_srtgebouw_key')
           + "     , g.alg_gebouw_code"
           + "     , g.alg_gebouw_naam"
           + "     , g.alg_gebouw_opmerking"
           + "     , g.alg_gebouw_bruto_vloeropp"
           + "     , g.alg_gebouw_omtrek"
           + "     , g.alg_gebouw_inhoud"
           + "     , g.alg_gebouw_x"
           + "     , g.alg_gebouw_y"
           + "     , g.alg_gebouw_vervaldatum"
           + "     , (SELECT MAX(t.fac_tracking_datum)"
           + "          FROM fac_tracking t"
           + "             , fac_srtnotificatie sn"
           + "         WHERE t.fac_srtnotificatie_key = sn.fac_srtnotificatie_key"
           + "           AND t.fac_tracking_refkey = g.alg_gebouw_key"
           + "           AND sn.fac_srtnotificatie_xmlnode = 'gebouw'"
           + "       ) recentdatum"
           + "  FROM alg_gebouw g"
           + "     , alg_locatie l"
           + "     , alg_srtgebouw s"
           + " WHERE l.alg_locatie_key(+) = g.alg_locatie_key"
           + "   AND s.alg_srtgebouw_key(+) = g.alg_srtgebouw_key"
           + "   AND g.alg_gebouw_verwijder IS NULL";

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

    sqln += getKenmerkSql("ALG", "g.alg_gebouw_key");

    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 (params.bld_code)
        sqln += " AND UPPER(g.alg_gebouw_code) LIKE " + safe.quoted_sql_wild(params.bld_code + "%");
    if (params.bld_name)
        sqln += " AND UPPER(g.alg_gebouw_naam) LIKE " + safe.quoted_sql_wild(params.bld_name + "%");

    if (srtgebouw_key)
      sqln += " AND g.alg_srtgebouw_key = " + srtgebouw_key;

    if (verantw_key)
      sqln += " AND (g.prs_perslid_key_verantw = " + verantw_key + " OR g.prs_perslid_key_verantw2 = " + verantw_key + ")";

    if (!expalgincl)
      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  ";

    var addurl = "appl/alg/alg_gebouw.asp";

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

//     addurl += buildTransitParam(["loc_key", "bld_key", "flo_key", "room_key"], params)  // TODO: welke allemaal?

    var rst = new ResultsetTable({ sql:sqln,
                                   keyColumn: "alg_gebouw_key",
                                   ID: "algtable",
                                   title: L("lcl_menu_alg_gebouw"),
                                   showAll: showall,
                                   rowData: fnrowData,
                                   rowClass: fnrowClass,
                                   rowActionEnabler: fnrowActionEnabler,
                                   flexModule: "ALG",
                                   flexId: "alg_gebouw_key",
                                   flexParams: fnrowFlexParams,
                                   filterParams: params,
                                   outputmode: outputmode,
                                   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_code"), content: "alg_gebouw_code"}));
    rst.addColumn(new Column({caption: L("lcl_estate_gebouw_man_name"), content: "alg_gebouw_naam" }));
    rst.addColumn(new Column({caption: L("lcl_estate_gebouw_srtgebouw"), content: "alg_srtgebouw_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_estate_gebouw_man_bruto_vloeropp"), content: "alg_gebouw_bruto_vloeropp", datatype: "float", decimals: 2}));
    rst.addColumn(new Column({caption: L("lcl_alg_vervaldatum"), content: "alg_gebouw_vervaldatum", datatype: "date"}));

    if (outputmode != 0)
    {
      rst.addColumn(new Column({caption: L("lcl_estate_gebouw_man_opmerk"), content: "alg_gebouw_opmerking"}));
      rst.addColumn(new Column({caption: L("lcl_estate_gebouw_man_omtrek"), content: "alg_gebouw_omtrek"}));
      rst.addColumn(new Column({caption: L("lcl_estate_gebouw_man_inhoud"), content: "alg_gebouw_inhoud"}));
      rst.addColumn(new Column({caption: L("lcl_geoxcoord"), content: "alg_gebouw_x"}));
      rst.addColumn(new Column({caption: L("lcl_geoycoord"), content: "alg_gebouw_y"}));
    }

    rst.addAction({ action: "gebouwEdit", 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_verdieping_list")});
      rst.addAction({ action: "gotoParent", caption: L("lcl_goto_locatie_list")});
    }
    var cnt = rst.processResultset();
%>
    </body>
  </html>
<%
} %>

