<% /*
     $Revision$
     $Id$
    File:               alg_locatie_list.inc
    Description:        Show an overview of alg_locatie items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Include for locatie-overzichten, like Search action from alg_locatie_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"],
                      js: []
                    })

function fnrowData(oRs)
{
  var parent_key = oRs("alg_district_key").value;
  var detail_key = oRs("alg_locatie_key").value;
  var key        = oRs("alg_locatie_key").value;
  var oms        = oRs("alg_locatie_omschrijving").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_gebouw_search.asp?locatie_key="+algData.detail_key+"&autosearch="+autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_building")%>");
  }
    function gotoDetails2(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_terreinsector_search.asp?locatie_key="+algData.detail_key+"&autosearch="+autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_terra")%>");
  }
  function gotoParent(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_district_search.asp?district_key="+algData.parent_key+"&autosearch="+autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_district")%>");
  }
    function locatieEdit(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_locatie.asp?key="+algData.key;
    FcltMgr.openDetail(url, "<%=L("lcl_location")%>" + ' ' + algData.oms);
  }

  function doDelete(rowArray, isMulti)
  {
    FcltMgr.confirm(L("lcl_alg_del_txt_locatie"), { autoconfirm: isMulti }, function() {
        var locKeyString = getKeyString(rowArray);
        var data = { key: locKeyString,
                     level: "L"
                   };
        <% protectRequest.dataToken("data"); %>
        $.post("alg_delete.asp", data, FcltCallbackRefresh, "json");
    });
  }
</script>
<%
function locatie_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 < 3);

  // 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;

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

  function fnrowFlexParams(oRs)
  {
    var loc_key = oRs("alg_locatie_key").Value;
    var this_alg = alg.func_enabled(loc_key, "L");

    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 l.alg_locatie_key"
             + "     , l.alg_district_key"
             + "     , l.alg_locatie_code"
             + "     , l.alg_locatie_omschrijving"
             + "     , d.alg_district_omschrijving"
             + "     , l.alg_locatie_adres"
             + "     , l.alg_locatie_post_adres"
             + "     , l.alg_locatie_postcode"
             + "     , l.alg_locatie_post_postcode"
             + "     , l.alg_locatie_plaats"
             + "     , l.alg_locatie_post_plaats"
             + "     , l.alg_locatie_land"
             + "     , l.alg_locatie_post_land"
             + "     , l.alg_locatie_verantw"
             + "     , l.alg_locatie_verantw_tel"
             + "     , l.alg_locatie_x"
             + "     , l.alg_locatie_y"
             + "     , (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 = l.alg_locatie_key"
             + "           AND sn.fac_srtnotificatie_xmlnode = 'locatie'"
             + "       ) recentdatum"
             + "  FROM alg_v_aanweziglocatie l"
             + "     , alg_district d "
             + " WHERE d.alg_district_key(+) = l.alg_district_key"
             + "   AND l.alg_locatie_verwijder IS NULL";

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

    sqln += getKenmerkSql("ALG", "l.alg_locatie_key");

    if (loc_key)
    {
      sqln += " AND l.alg_locatie_key = " + loc_key;
    }
    else if (dis_key)
    {
      sqln += " AND l.alg_district_key = " + dis_key;
    }
    else if (reg_key)
    {
      sqln += " AND d.alg_regio_key = " + reg_key;
    }
    
    if (params.loc_code)
        sqln += " AND UPPER(l.alg_locatie_code) LIKE " + safe.quoted_sql_wild(params.loc_code + "%");    

    sqln += " ORDER BY l.alg_locatie_upper ASC "

    var addurl = "appl/alg/alg_locatie.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_locatie_key",
                                   ID: "algtable",
                                   title: L("lcl_menu_alg_locatie"),
                                   showAll: showall,
                                   rowData: fnrowData,
                                   rowClass: fnrowClass,
                                   rowActionEnabler: fnrowActionEnabler,
                                   outputmode: outputmode,
                                   flexModule: "ALG",
                                   flexId: "alg_locatie_key",
                                   flexParams: fnrowFlexParams,
                                   filterParams: params,
                                   buttons: addButton
                                 });

    rst.addColumn(new Column({caption: L("lcl_estate_district_man_descr"), content: "alg_district_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_estate_locatie_man_code"), content: "alg_locatie_code"}));
    rst.addColumn(new Column({caption: L("lcl_estate_locatie_man_descr"), content: "alg_locatie_omschrijving" }));
    rst.addColumn(new Column({caption: L("lcl_prs_address_bezoek_adres"), content: "alg_locatie_adres"}));
    rst.addColumn(new Column({caption: L("lcl_prs_address_bezoek_postcode"), content: "alg_locatie_postcode"}));
    rst.addColumn(new Column({caption: L("lcl_prs_address_bezoek_plaats"), content: "alg_locatie_plaats"}));

    if (outputmode != 0)
    {
      rst.addColumn(new Column({caption: L("lcl_prs_address_bezoek_land"), content: "alg_locatie_land"}));
      rst.addColumn(new Column({caption: L("lcl_prs_address_post_adres"), content: "alg_locatie_post_adres"}));
      rst.addColumn(new Column({caption: L("lcl_prs_address_post_postcode"), content: "alg_locatie_post_postcode"}));
      rst.addColumn(new Column({caption: L("lcl_prs_address_post_plaats"), content: "alg_locatie_post_plaats"}));
      rst.addColumn(new Column({caption: L("lcl_prs_address_post_land"), content: "alg_locatie_post_land"}));
      rst.addColumn(new Column({caption: L("lcl_estate_locatie_man_verantw"), content: "alg_locatie_verantw"}));
      rst.addColumn(new Column({caption: L("lcl_geoxcoord"), content: "alg_locatie_x"}));
      rst.addColumn(new Column({caption: L("lcl_geoycoord"), content: "alg_locatie_y"}));
    }


    rst.addAction({ action: "locatieEdit", 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_gebouw_list")});
        rst.addAction({ action: "gotoDetails2", caption: L("lcl_goto_terreinsector_list")});
        rst.addAction({ action: "gotoParent", caption: L("lcl_goto_district_list")});
    }
    var cnt = rst.processResultset();
%>
  </body>
</html>
<%
}
%>

