<% /*
     $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/resultset_table_v2.inc" -->
<!-- #include file="../Shared/getkenmerksql.inc" -->
<!-- #include file="alg.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"] });

var cnt_full = getQParamInt("cnt_full", 1);

%>

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

  function gotoDetails(row)
  {
    var algData = JSON.parse(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 = JSON.parse(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 = JSON.parse(row.getAttribute("ROWDATA"));
    var url = "appl/ALG/alg_verdieping.asp?mode=viewUpdate&key=" + algData.key + "&cnt_full=<%=cnt_full%>";
    FcltMgr.openDetail(url, L("lcl_floor") +" "+ algData.oms);
  }

  function verdiepingEditMulti(rowArray, isMulti)
  {
      var flrKeyString = getKeyString(rowArray);
      var subject = L("lcl_alg_multi_edit");
      var url = "../alg/alg_edit_verdieping_multi.asp?flr_keys=" + flrKeyString;
      FcltMgr.openModalDetail(url, subject, {callback: FcltCallbackRefresh});
  }

  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_arr = params.reg_key_arr;
  var dis_key_arr = params.dis_key_arr;
  var loc_key_arr = params.loc_key_arr;
  var bld_key_arr = params.bld_key_arr;
  var lvl_key_arr = params.lvl_key_arr;
  var chkgeb = params.chkgeb;
%>

  <html>
    <head>
      <% FCLTHeader.Generate({outputmode: outputmode}); %>
    </head>
    <body id="listbody">
<%
  var flexParams = alg.alg_onroerendgoed_flex_params("V");
  var sqln = "SELECT v.alg_verdieping_key, "
       + "       v.alg_gebouw_key, "
       + "       g.alg_locatie_key, "
       + "       l.alg_locatie_omschrijving, "
       + "       g.alg_gebouw_code, "
       + "       g.alg_gebouw_naam, "
       + "       v.alg_verdieping_omschrijving, "
       + "       v.alg_verdieping_code  "
       + "     , (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 = v.alg_verdieping_key"
       + "           AND sn.fac_srtnotificatie_xmlnode = 'verdieping'"
       + "       ) recentdatum"
       +       flexParams.sql
       + "  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_arr.length)
      {
      sqln += " AND v.alg_verdieping_key IN (" + lvl_key_arr.join(",") + ")";
      }

   else if (bld_key_arr.length)
      {
      sqln += " AND g.alg_gebouw_key IN (" + bld_key_arr.join(",") + ")";
      }

   else if (loc_key_arr.length)
      {
      sqln += " AND g.alg_locatie_key IN (" + loc_key_arr.join(",") + ")";
      }
    else if (dis_key_arr.length)
      sqln += " AND l.alg_district_key IN (" + dis_key_arr.join(",") + ")";
    else if (reg_key_arr.length)
      sqln += " AND l.alg_district_key IN (SELECT alg_district_key FROM alg_district WHERE alg_regio_key IN (" + reg_key_arr.join(",") +"))";

    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_arr", "bld_key_arr", "flo_key", "room_key"], params)  // TODO: welke allemaal?

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

    /**********************************
    * Callback functies ResultsetTable
     *********************************/
    var recent = new Date();

    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);
    }

    function fnrowClass(oRs) // identiek aan de andere niveaus
    {
      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;
    }

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

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

    function fncolFlexBijlagen(oRs)
    {
      var flexval = oRs("algflex").Value;
      var alg_key = oRs("alg_verdieping_key").Value;
      var result = ( flexval
                   ? safe.html(flexval.replace(/\r/g, "\n")+ "\n")
                   : ""
                   );
      if (flexParams.bijlageflex)
      {
        result += alg.alg_onroerendgoed_flex_bijlagen("V", alg_key);
      }
      return result;
    }

    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,
                                   rowClass: fnrowClass,
                                   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_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_verdieping_man_code"), content: "alg_verdieping_code"}));
    rst.addColumn(new Column({caption: L("lcl_estate_verdieping_man_descr"), content: "alg_verdieping_omschrijving" }));
    if (flexParams.anyflex)
      rst.addColumn(new Column({caption: flexParams.flexlabel, content: fncolFlexBijlagen, colName: "fncolFlexBijlagen"}));

    rst.addAction({ action: "verdiepingEdit", caption: L("lcl_edit"), isDefault:  true});
    rst.addAction({ action: "verdiepingEditMulti", caption: L("lcl_alg_multi_edit"), enabler: "eEditMulti", single:false, multi: true, multiOnce: 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>
<%}%>
