<% /*
     $Revision$
     $Id$
    File:               alg_terreinsector.inc
    Description:        Show an overview of terrein items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from alg_terreinsector_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 key        = oRs("alg_terreinsector_key").Value;
  var oms        = oRs("alg_terreinsector_omschrijving").Value;

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

  return JSON.stringify(data);
}

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

  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 terreinEdit(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_terreinsector.asp?mode=viewUpdate&ter_key="+algData.key;
    FcltMgr.openDetail(url, "<%=L("lcl_terra")%>" + ' ' + algData.oms);
  }

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

function terrein_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.tmALGwritelevel < 9);  //wat moet het zijn voor terriensector

  // 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 ter_code = params.ter_code;
  var ter_func_key = params.ter_func_key;

  function fnrowActionEnabler(oRs) // klopt dit voor terreinsector?
  {
      var eDelete = false;
      if (alg.canWriteLocatie(oRs("alg_locatie_key").Value, authparams.tmALGwritelevel))
          eDelete = true;
      return ({eDelete: eDelete})
  }

  function fnrowFlexParams(oRs)
  {
    var ter_key = oRs("alg_terreinsector_key").Value;
    var this_alg = alg.func_enabled(ter_key, "T");

    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_terreinsector_key, "
       + "         g.alg_locatie_key, "
       + "         l.alg_locatie_omschrijving, "
       + "         g.alg_terreinsector_naam, "
       + "         g.alg_terreinsector_omschrijving, "
       + "         g.alg_terreinsector_code, "
       + "         g.alg_terreinsector_oppervlak, "
       + "         g.alg_terreinsector_omtrek, "
       + "         g.alg_srtterreinsector_key, "
       + lcl.xsqla('s.alg_srtterreinsec_omschrijving', 's.alg_srtterreinsector_key')
       + "    FROM alg_v_aanwezigterreinsector g, "
       + "         alg_locatie l, "
       + "         alg_srtterreinsector s "
       + "   WHERE l.alg_locatie_key(+) = g.alg_locatie_key "
       + "     AND s.alg_srtterreinsector_key(+) = g.alg_srtterreinsector_key "
       + "     AND g.alg_terreinsector_verwijder IS NULL "

   if ( authparams.ALGreadlevel > -1 )
      {  // Er is een scope-beperking van kracht
          sqln += " AND g.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", "g.alg_terreinsector_key");

   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 (ter_func_key)
      {
       sqln += " AND g.alg_srtterreinsector_key = " + ter_func_key;
      }

   if (ter_code)
     {
     sqln += " AND UPPER(g.alg_terreinsector_code) LIKE " + safe.quoted_sql_wild("%" + ter_code + "%");
     }

     sqln += " ORDER BY g.alg_terreinsector_upper ASC "

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

      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_terreinsector_key",
                                   ID: "algtable",
                                   title: L("lcl_menu_alg_terreinsector"),
                                   showAll: showall,
                                   rowActionEnabler: fnrowActionEnabler,
                                   rowData: fnrowData,
                                   outputmode: outputmode,
                                   flexModule: "ALG",
                                   flexId: "alg_terreinsector_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_terreinsector_man_code"), content: "alg_terreinsector_code"}));
    rst.addColumn(new Column({caption: L("lcl_estate_terreinsector_man_name"), content: "alg_terreinsector_naam"}));
    rst.addColumn(new Column({caption: L("lcl_estate_terreinsector_man_sort"), content: "alg_srtterreinsec_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_estate_terreinsector_man_oppervlak"), content: "alg_terreinsector_oppervlak"}));
    rst.addColumn(new Column({caption: L("lcl_estate_terreinsector_man_omtrek"), content: "alg_terreinsector_omtrek"}));

    rst.addAction({ action: "terreinEdit", 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: "gotoParent", caption: L("lcl_goto_locatie_list")});

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

