<% /*
     $Revision$
     $Id$
    File:               alg_district.inc
    Description:        Show an overview of district items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from alg_district_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_regio_key").value;
  var detail_key = oRs("alg_district_key").value;
  var key        = oRs("alg_district_key").value;
  var oms        = oRs("alg_district_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; // automatisch laden van resultaat op search pagina.

  function gotoDetails(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_locatie_search.asp?district_key=" + algData.detail_key + "&autosearch=" + autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_location")%>");
  }
  function gotoParent(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_regio_search.asp?regio_key=" + algData.parent_key + "&autosearch=" + autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_region")%>");
  }
    function districtEdit(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_district.asp?mode=viewUpdate&key=" + algData.key;
    FcltMgr.openDetail(url, "<%=L("lcl_district")%>" + ' ' + algData.oms);
  }

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

</script>
<%

function district_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var authparams = alg.checkAutorisation();
  var canAdd = (authparams.mALGwritelevel < 2);
  var addButton = [];

  // 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 reg_key = params.reg_key;
  var dis_key = params.dis_key;

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

  function fnrowFlexParams(oRs)
  {
    var dis_key = oRs("alg_district_key").Value;
    var this_alg = alg.func_enabled(dis_key, "D");

    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   d.alg_district_key, "
         + "         d.alg_regio_key, "
         + "         d.alg_district_omschrijving, "
         + "         r.alg_regio_omschrijving "
         + "    FROM alg_district d, "
         + "         alg_regio r "
         + "   WHERE r.alg_regio_key = d.alg_regio_key "
         + "         AND d.alg_district_verwijder IS NULL ";

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

     sqln += getKenmerkSql("ALG", "d.alg_district_key");

   if (dis_key)
      {
      sqln += " AND d.alg_district_key = " + dis_key;
      }

   else if (reg_key)
      {
      sqln += " AND d.alg_regio_key = " + reg_key;
      }

     sqln += " ORDER BY d.alg_district_upper ASC "

    var addurl = "appl/alg/alg_district.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_district_key",
                                   ID: "algtable",
                                   showAll: showall,
                                   rowData: fnrowData,
                                   rowActionEnabler: fnrowActionEnabler,
                                   flexModule: "ALG",
                                   flexId: "alg_district_key",
                                   flexParams: fnrowFlexParams,
                                   filterParams: params,
                                   outputmode: outputmode,
                                   buttons: addButton
                                  });

    rst.addColumn(new Column({caption: L("lcl_estate_regio_descr"), content: "alg_regio_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_estate_district_man_descr"), content: "alg_district_omschrijving", hasActions: true}));

    rst.addAction({ action: "districtEdit", caption: L("lcl_edit"), isDefault:  true});
    rst.addAction({ action: "doDelete", caption: L("lcl_delete"), enabler: "eDelete", multi: true, multiOnce: true});
    rst.addAction({ action: "gotoDetails", caption: L("lcl_goto_locatie_list")});
    rst.addAction({ action: "gotoParent", caption: L("lcl_goto_regio_list")});

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

