<% /*
     $Revision$
     $Id$

    File:           prs_perslidwerkplek_list.inc
    Description:    Lijstje werkplekken van één persoon of lijstje personen op één werkplek
    Parameters:
    Context:        prs_perslidwerkplek_search_list.asp
    Note:           Met impliciete werkplekken kunnen we hier
                      - voor flex-werkplekken: Daarvan tonen we wel de mogelijk bezetting
                        maar dat beheren we hier niet.
                      - Via het lijstje onder personenbeheer. Dan kun je via 'Add'
                        een impliciete werkplek toevoegen

*/
%>
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->

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

function perslidwerkplek_list(pautfunction, params)
{
  var authparams = user.checkAutorisation("WEB_PRSBOF");
  var canAdd = (authparams.ALGwritelevel < 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 wp_key = params.wp_key ? params.wp_key : -1;
  var prs_key = params.prs_key ? params.prs_key : -1;
  var afd_key = params.afd_key ? params.afd_key : -1;
  var embedded = params.embedded ? params.embedded : -1;

  // Bij impliciete werkplekken krijg ik alleen add/delete bij een persoon
  canAdd = canAdd && (prs_key > 0 || S("prs_werkplek_implicit") == 0);

  function fnrowActionEnabler(oRs)
    {
        eDelete = true;
        return ({eDelete: eDelete})
    }


 function fnrowData(oRs)
        {
          var key        = oRs("prs_perslidwerkplek_key").value;
          var data = {key:key};
          return JSON.stringify(data);
        }
%>
<html>
 <head>
<% FCLTHeader.Generate({outputmode:outputmode}) %>
<script type="text/javascript">

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

  var autosearch = 1;

    function bezettingEdit(row)
    {
      var algData = eval('(' + row.getAttribute("ROWDATA") + ')');
      //var url = "prs_edit_perslidwerkplek.asp?pwp_key="+algData.key;
      FcltMgr.openModalDetail("../PRS/prs_edit_perslidwerkplek.asp?pwp_key="+algData.key, L("lcl_prs_werkplekbezetting"), {callback: refreshall});
    }

    function doDelete(rowArray, isMulti)
    {
      FcltMgr.confirm(L("lcl_prs_del_txt_werkplekbez"), { autoconfirm: isMulti }, function() {
          var pwpKeyString = getKeyString(rowArray);
          var data = {
              key: pwpKeyString,
              level: "PW",
              multi: (isMulti ? 1 : 0)
          };
          <% protectRequest.dataToken("data"); %>
          $.post("../alg/alg_delete.asp",
              data,
              FcltCallbackRefresh,
              "json");
      });
    }

    function doAdd()
    {
        var addurl = "../prs/prs_edit_perslidwerkplek.asp?wp_key=<%=wp_key%>&prs_key=<%=prs_key%>";
        FcltMgr.openModalDetail(addurl, "<%=L("lcl_add")%>", { callback: refreshall });
    }

function Deleted()
{
  algtable.deleteSelectedRows();
}
</script>
    </head>
    <body id="listbody">
<%
  var sqln =   "SELECT   pw.prs_perslidwerkplek_key, "
           + "         prs_werkplek_omschrijving, "
           + "         pw.prs_perslid_key, "
           + "         pw.prs_perslidwerkplek_bezetting, "
           + "         prs_perslidwerkplek_volgnr,"
           + "         w.alg_ruimte_aanduiding || '(' || alg_locatie_omschrijving || ')' alg_ruimte_aanduiding, "
           + "         w.prs_werkplek_aanduiding, "
           + "         pf.prs_perslid_naam_full, "
           + "         prs_perslid_dienstverband "
           + "    FROM prs_v_werkplek_gegevens w, "
           + "         prs_perslidwerkplek pw, "
           + "         prs_v_aanwezigperslid p, "
           + "         prs_v_perslid_fullnames_all pf, "
           + "         alg_locatie l "
           + "   WHERE w.prs_werkplek_key = pw.prs_werkplek_key "
           + "     AND pw.prs_perslid_key = p.prs_perslid_key "
           + "     AND pf.prs_perslid_key = p.prs_perslid_key "
           + "     AND pw.prs_perslid_key IS NOT NULL "
           + "     AND w.alg_locatie_key = l.alg_locatie_key "
           + (prs_key != -1 ?"     AND pw.prs_perslid_key = " + prs_key : "")
           + (afd_key != -1 ?"     AND p.prs_afdeling_key = " + afd_key : "")
           + (wp_key != -1  ? "     AND w.prs_werkplek_key = " + wp_key : "");


// TODO: Is dit correct, is onderstaand commentaar nog steeds van toepassing
// Determine if we can edit this workplace
// Even if this is true, there may still be existing occupations's (by persons of other departments)
// we are not allowed to edit. We leave this as a leak:
// When you can edit any existing WP for a room, you can edit them for all persons!
  if ( authparams.ALGreadlevel > -1 )
    {  // Er is een scope-beperking van kracht
        sqln += " AND w.alg_ruimte_key IN "
               + "(SELECT alg_ruimte_key FROM fac_v_my_rooms "
               +   "WHERE prs_perslid_key="+user_key
               +    " AND niveau=" + authparams.ALGreadlevel + ")";
    }

    sqln += " ORDER BY prs_perslid_naam_full, prs_perslidwerkplek_volgnr, prs_werkplek_aanduiding ";

    var buttons = [];
    if (canAdd)
        buttons.push({ icon: "plus.png", title: L("lcl_add"), action: "doAdd()" });

    var rst = new ResultsetTable({sql:sqln,
                                   keyColumn: "prs_perslidwerkplek_key",
                                   ID: "algtable",
                                   showAll: showall,
                                   rowData: fnrowData,
                                  // rowActionEnabler: fnrowActionEnabler,
                                   outputmode: outputmode,
                                   filterParams: params,
                                   title: L("lcl_occupation"),
                                   buttons: buttons
                                  });

    // Indien embedded: onderdruk de kolom waarvoor de key is gegeven
    rst.addColumn(new Column({caption: L("lcl_estate_pwp_seq"), content: "prs_perslidwerkplek_volgnr" }));
    if (!embedded || wp_key == -1)
        rst.addColumn(new Column({caption: ( S("prs_werkplek_implicit")==1 ? L("lcl_room") : L("lcl_workplace")),
                                  content: ( S("prs_werkplek_implicit")==1 ? "alg_ruimte_aanduiding": "prs_werkplek_aanduiding"),
                                  hasActions: true}));
    if (!embedded || prs_key == -1)
        rst.addColumn(new Column({caption: L("lcl_prs_name"), content: "prs_perslid_naam_full", hasActions: true}));
    rst.addColumn(new Column({caption: L("lcl_occupation_perc"), content: "prs_perslidwerkplek_bezetting"}));

    rst.addAction({ action: "bezettingEdit", caption: L("lcl_edit"), isDefault:  true});
    if (canAdd)
        rst.addAction({ action: "doDelete", caption: L("lcl_delete"), multi: !embedded, multiOnce: true});

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

