<% /*
    $Revision$
    $Id$

    File:               alg_werkplek_list.inc
    Description:        Show an overview of werkplek items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from alg_werkplek_search_list.asp form
    Note:
*/
%>
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="alg.inc" -->

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: ["jQuery-ui.js"]
                    })

 function fnrowData(oRs)
        {
          var parent_key = oRs("alg_ruimte_key").value;
          var key        = oRs("prs_werkplek_key").value;
          var oms        = oRs("prs_werkplek_omschrijving").value;

          var data = {parent_key:parent_key, key:key, oms:oms,
                        wp_type: oRs("prs_werkplek_type").Value
                     };

          return JSON.stringify(data);
        }
%>
<script type="text/javascript">
  var autosearch = 1;

  function gotoParent(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
    var url = "appl/ALG/alg_ruimte_search.asp?ruimte_key="+algData.parent_key+"&autosearch="+autosearch;
    FcltMgr.openDetail(url, "<%=L("lcl_room")%>");
  }

  function werkplekEdit(row)
  {
    var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
<% if (S("prs_werkplek_implicit") == 1) { %>
    if (algData.wp_type == 0) // Een vaste. Bewerk de bezetting van de ene persoon erop
    {
        var url = "../prs/prs_edit_perslidwerkplek.asp?wp_key=" + algData.key;
        FcltMgr.openModalDetail(url, "<%=L("lcl_change")%>", {callback: refreshall });
    }
    else
<% } %>
    {
        var url = "appl/ALG/alg_wp.asp?mode=viewUpdate&key="+algData.key;
        FcltMgr.openDetail(url, "<%=L("lcl_workspace")%>" + ' ' + algData.oms);
    }
  }

    function refreshall(data)
    {
      if (data.refresh)
          window.location.reload();
    }

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

    function doAddWP(room_key) // als impliciet==0
    {
        addurl = "appl/alg/alg_wp.asp?room_key=" + room_key;
        FcltMgr.openDetail(addurl, "<%=L("lcl_add")%>");
    }

    function doAddWPBezeting(room_key) // als impliciet==1
    {
        var addurl = "../prs/prs_edit_perslidwerkplek.asp?room_key=" + room_key;
        FcltMgr.openModalDetail(addurl, "<%=L("lcl_add")%>", {callback: refreshall });
    }

</script>
<%

function werkplek_list(pautfunction, params)
{
  // De functie checkAutorisation(autfunction) geeft een Response.End indien er geen autorisatie voor autfunction wordt gevonden
  var buttons = [];
  var authparams = alg.checkAutorisation();
  var canAdd = (authparams.rmALGwritelevel < 6); // TODO: fijner, ik denk 3. Ik moet voor deze locatie, dit district of deze regio schrijfrechten hebben

  // 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 loc_key = params.loc_key;
  var bld_key = params.bld_key;
  var lvl_key = params.lvl_key;
  var room_key = params.room_key;
  var wp_descr = params.wp_descr;
  var tiny = params.tiny;

  function fnrowActionEnabler(oRs)
    {
        var eDelete = false;
        if (alg.canWriteRuimte(oRs("alg_ruimte_key").value, authparams.rmALGwritelevel))
            eDelete = true;
        return ({eDelete: eDelete})
    }

%>
  <html>
    <head>
      <% FCLTHeader.Generate({outputmode:outputmode}) %>
    </head>
    <body id="listbody">
<%
  var sqln =   "SELECT   p.prs_werkplek_key, "
       + "         p.alg_ruimte_key, "
       + "         p.alg_verdieping_key, "
       + "         p.alg_gebouw_key, "
       + "         p.alg_locatie_key, "
       + "            l.alg_locatie_code "
       + "         || '-' "
       + "         || p.alg_gebouw_code "
       + "         || '-' "
       + "         || p.alg_verdieping_code "
       + "         || '-' "
       + "         || p.alg_ruimte_nr samengesteld, "
       + "         p.alg_gebouw_omschrijving, "
       + "         p.alg_verdieping_omschrijving, "
       + "         p.alg_verdieping_code, "
       + "         p.alg_ruimte_nr, "
       + "         w.alg_ruimte_omschrijving, "
       + "         p.prs_werkplek_volgnr, "
       + "         p.prs_werkplek_omschrijving, "
       + "         CASE COALESCE (w.prs_werkplek_vastopp, -1) "
       + "            WHEN -1 "
       + "               THEN 'nee' "
       + "            ELSE 'ja' "
       + "         END decode_vastopp, "
       + "         p.prs_werkplek_opp, "
       + "         p.prs_perslidwerkplek_bezetting, "
       + "         p.prs_afd_perslid_naam, "
       + "         w.prs_werkplek_type, "
       + "         w.prs_werkplek_virtueel "
       + "    FROM prs_v_perslidwerkplek_gegevens p, "
       + "         alg_locatie l, "
       + "         prs_v_werkplek_gegevens w "
       + "   WHERE p.alg_locatie_key = l.alg_locatie_key "
       + "     AND w.prs_werkplek_key = p.prs_werkplek_key ";

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

    if (room_key >= 0)
      {
      sqln += " AND p.alg_ruimte_key = " + room_key;
      }

   else if (lvl_key)
      {
      sqln += " AND p.alg_verdieping_key = " + lvl_key;
      }

   else if (bld_key)
      {
      sqln += " AND p.alg_gebouw_key = " + bld_key;
      }

   else if (loc_key)
      {
      sqln += " AND p.alg_locatie_key = " + loc_key;
      }

  if (wp_descr)
     {
     sqln += " AND UPPER(p.prs_werkplek_omschrijving) LIKE " + safe.quoted_sql_wild("%" + wp_descr + "%");
     }


     sqln += " ORDER BY w.alg_locatie_key ASC, "
          + "         p.alg_gebouw_code ASC, "
          + "         p.alg_verdieping_volgnr ASC, "
          + "         p.alg_ruimte_nr ASC, "
          + "         p.prs_werkplek_volgnr ";


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

      if (canAdd)
        {
          buttons.push({ icon: "page_refresh.png", title: L("lcl_refresh"), action: "FcltMgr.reload()" });
          if (S("prs_werkplek_implicit") == 1)
          {
            buttons.push({ icon: "plus.png", title: L("lcl_add")+  " " + L("lcl_occupation"), action:  "doAddWPBezeting("+room_key+")"});
            buttons.push({ icon: "plus.png", title: L("lcl_add") + " " + L("lcl_prs_workplace"), action:  "doAddWP("+room_key+")"});
          }
          else // Altijd werkplek knop
            buttons.push({ icon: "plus.png", title: L("lcl_add"), action:  "doAddWP("+room_key+")"});
        }

    function fnWPType(oRs)
    {
        switch (oRs("prs_werkplek_type").Value)
        {
            case 0: return L("lcl_wptype_0");
            case 1: return L("lcl_wptype_1") + (S("prs_werkplek_implicit")==1?" "+safe.html(oRs("prs_werkplek_omschrijving")):""); // Impliciet flex de code erachter
        };
    }
    function fnBezetting(oRs)
    {
        switch (oRs("prs_werkplek_type").Value)
        {
            case 0: return oRs("prs_perslidwerkplek_bezetting");
            case 1: return "";
        };
    }

    var rst = new ResultsetTable({sql:sqln,
                                   keyColumn: "prs_werkplek_key",
                                   ID: "algtable",
                                   showAll: showall,
                                   rowData: fnrowData,
                                   rowActionEnabler: fnrowActionEnabler,
                                   noPrint: tiny,
                                   outputmode: outputmode,
                                   filterParams: params,
                                   title: L("lcl_workplace") + "/" + L("lcl_prs_werkplekbezetting"),
                                   buttons: buttons
                                  });

    rst.addColumn(new Column({caption: L("lcl_room"), content: "Samengesteld", nowrap: true}));
    rst.addColumn(new Column({caption: L("lcl_estate_ruimte_man_descr"), content: "alg_ruimte_omschrijving"}));
    rst.addColumn(new Column({caption: L("lcl_estate_wp_seq"), content: "prs_werkplek_volgnr"}));
    if (S("prs_werkplek_implicit")==0)
    {
        rst.addColumn(new Column({caption: L("lcl_workplace"), content: "prs_werkplek_omschrijving" }));
        rst.addColumn(new Column({caption: L("lcl_estate_wp_fixed"), content: "decode_vastopp"}));
        rst.addColumn(new Column({caption: L("lcl_estate_wp_area"), content: prepareArea}));
    }
    rst.addColumn(new Column({caption: L("lcl_wptype"), content: fnWPType}));
    rst.addColumn(new Column({caption: L("lcl_occupation")+" (%)", content: fnBezetting }));
    rst.addColumn(new Column({caption: L("lcl_name"), content: "prs_afd_perslid_naam"}));

    function prepareArea(oRs)
    {
      return safe.curr(oRs("prs_werkplek_opp").Value);
    }

    if (outputmode != 0)
    {
    rst.addColumn(new Column({caption: L("lcl_estate_wp_seq"), content: "prs_werkplek_volgnr"}));
    rst.addColumn(new Column({caption: L("lcl_estate_wp_virt"), content: "prs_werkplek_virtueel"}));
    }

    rst.addAction({ action: "werkplekEdit", caption: L("lcl_edit"), isDefault:  true});
    rst.addAction({ action: "doDelete", caption: L("lcl_delete"), enabler: "eDelete", single: !tiny, multi: !tiny, multiOnce: true});
    if (!tiny)
        rst.addAction({ action: "gotoParent", caption: L("lcl_goto_ruimte_list")});

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

