<%@ language = "JavaScript" %>
<% /*
     $Revision$
     $Id$

    File:        fac_list.asp
    Description: Toont verplichtingen etc van user
    Parameters:  prs_key optioneel (anders user_key)
    Context:
    Note:        De urole (voor vervolgacties) wordt impliciet bepaald door
                 de prs_key: is het de lijst van mezelf, dan ga ik met fe verder,
                 is het de lijst van een ander, dan ga ik met fo verder.

    Note:        mld.inc en res.inc wordt geinclude tbv single source. Afgewogen.
                 tussen: includen, getmldstatustext hierin kopieren, getmldstatustext naar shared.inc moven.
                 Gekozen voor de eerste, de derde zou beter zijn (later) TODO
*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/data_recordset.inc" -->
<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="../shared/rater.inc" -->
<!-- #include file="../mld/mld.inc" -->
<!-- #include file="../res/res.inc" -->
<!-- #include file="fac.inc" -->
<%

var pkey = getQParamInt("prs_key", user_key);
var outputmode = getQParamInt("outputmode", 0); //  0 = screen, 1 = print, 2 = excel etc
var showall = getQParamInt("showall", 0) == 1;
var urole = "?";
if (pkey == user_key)
  urole = "fe";
else
  urole = "fo";

// TODO: Moet dit niet met behulp van bes.getbesbestellingstatustext
function bes_getstatustext (p)
{
  var statustekst = "??";
  var s = parseInt(p, 10);
  switch (s) {
      case 1: { statustekst = L("lcl_bes_afgewezen"); break; }
      case 2: { statustekst = L("lcl_bes_new"); break; }
      case 3: { statustekst = L("lcl_bes_gefiatteerd"); break; }
      case 4: { statustekst = L("lcl_bes_geaccepteerd"); break; }
      case 5: { statustekst = L("lcl_bes_besteld"); break; }
      case 6: { statustekst = L("lcl_bes_geleverd"); break; }
      case 7: { statustekst = L("lcl_bes_verwerkt"); break; }
      case 8: { statustekst = L("lcl_bes_mixed"); break; }
  }
  return statustekst;
}

function fnstatus(oRs)
{ // Soms is deze statustekst een lcl, soms moet je de status_key gebruiken voor vertaling
  switch (oRs.Fields("xmlnode").Value)
  {
    case "melding":       statusstr = mld.getmldstatustext(oRs.Fields("status_key").Value); break
    case "reservering":   statusstr = (oRs.Fields("bo_status_key").Value == 5 || oRs.Fields("bo_status_key").Value == 6)? res.getbostatustext(oRs.Fields("bo_status_key").Value) : res.getfostatustext(oRs.Fields("status_key").Value); break;
    case "afspraak":      statusstr = fac.localstring("" + oRs.Fields("status").Value); break;  // als lcl beschikbaar
    case "bestelling":    statusstr = bes_getstatustext(oRs.Fields("status_key").Value); break;
    case "deel":          statusstr = L("lcl_geleend_object"); break; // nvt
  }
   return statusstr;
}


function fntime(oRs)
{
  var timestr = "";
  switch (oRs.Fields("xmlnode").Value)
  {
    case "bestelling":
    case "melding":     var dt = new Date(oRs.Fields("datum").Value);
                        if (dt.getTime() != dt.midnight().getTime())
                            timestr = toTimeString(dt);
                        break;
    case "reservering":
    case "afspraak":    timestr = toTimeString(oRs.Fields("datum").Value);
                        break;
    case "deel":        timestr = "";
                        break;
  }

   return timestr;
}

function fncolType(oRs)
{
  var txt= "??";
  switch (oRs.Fields("xmlnode").Value) {
     case "melding":     txt = L("lcl_complain");  break;
     case "reservering": txt = L("lcl_reservation"); break;
     case "bestelling":  txt = L("lcl_bes_bestelling");  break;
     case "afspraak":    txt = L("lcl_vis_appointment");  break;
     case "deel":        txt = "";  break;  // status vertelt L("lcl_geleend_object")
  }
  return txt + " " + oRs.Fields("item").Value;
}

function isGereed(pxmlnode, pstatuskey)
{
  var isgereed = false;
  switch (pxmlnode)
  {
    case "bestelling" : var isgereed = (pstatuskey == 1 || pstatuskey == 6 || pstatuskey == 7 || pstatuskey == 8)
                        break;
    case "melding"    : var isgereed = (pstatuskey == 1 || pstatuskey == 5 || pstatuskey == 6)
                        break;
    case "reservering": var isgereed = (pstatuskey == 5 || pstatuskey == 6)
                        break;
  }
  return isgereed;
}

function fnLike(oRs, processParams)
{
  var isRemote = (processParams && processParams.disc_key);

  var likesymbol = "";
  var xmlnode = oRs.Fields("xmlnode").Value;
  var statuskey = oRs.Fields("status_key").Value;
  var bostatuskey = oRs.Fields("bo_status_key").Value;
  // Als xmlmode niet "reservering" is, dan is het zetten van het likesybol alleen afhankelijk van isGereed().
  // Voor xmlmode "reservering" moet het likesymbol worden gezet als (isGereed || v_like).
  var v_like = false;

  if (xmlnode == "reservering")
  {
    // Alleen voor reserveringen extra controle.
    var vandaag = new Date();
    var dteinde = new Date(oRs.Fields("datum_eind").value);
    var flike_days = vandaag.getTime() - dteinde.getTime();
    var flike_past = S("facilities_flike_past") * 1000 * 60 * 60 * 24;
    flike_days = (flike_days < 0 ? flike_past+1 : flike_days); // Als flike_days < 0 dan is de reservering nog niet afgelopen, dus mag nog niet worden beoordeeld
    v_like = (flike_days < flike_past);
  }

  // Wat al beoordeeld is in ieder geval tonen.
  if (oRs.Fields("satisfaction").Value)
    likesymbol = rating.stars(oRs.Fields("satisfaction").Value, {shownone: true, rating: {note: oRs.Fields("satisfaction_op").Value}});
  if (isGereed(xmlnode, (xmlnode == "reservering"? bostatuskey : statuskey)) || v_like)
  {
    if (pkey == user_key)
    {
      likesymbol = rating.stars(oRs.Fields("satisfaction").Value, {shownone: true, rating: {note: oRs.Fields("satisfaction_op").Value}});
      likesymbol = "<div onclick='fLike(event, \"" + oRs.Fields("item_key").Value + "\", \"" + oRs.Fields("xmlnode").Value +"\", "+(isRemote?1:0)+")'>" + likesymbol + "</div>";
    }
  }
  return likesymbol;
}

function fnrowData(oRs, processParams)
{
  var data = {module:oRs.Fields("xmlnode").Value, item: oRs.Fields("item").Value};
  if (processParams && processParams.disc_key)
  {
    data.disc_key = processParams.disc_key;
    data.deepurl = oRs.Fields("deepurl").Value;
  }

  return JSON.stringify(data);
}
var recent = new Date();
function fnrowClass(oRs)
{
  var lclass = "";
  var lnow = new Date();
  var lthisdate = new Date(oRs.Fields("datum").Value);
  var xmlnode = oRs.Fields("xmlnode").Value;
  var statuskey = oRs.Fields("status_key").Value;
  var bostatuskey = oRs.Fields("bo_status_key").Value;

  if (lthisdate.toDateString() == lnow.toDateString())
    lclass += " today";
  else if (lthisdate < lnow)
    lclass += " past";
  else
    lclass += " future";

  if (isGereed(xmlnode, (xmlnode == "reservering"? bostatuskey : statuskey)))
    lclass += " isready";

  if (oRs.Fields("status_key").Value == -1)
     lclass = " listfatal";  // ja, deze overruled de vorige classes

  // Tracking
  if (oRs.Fields("recentdatum").Value != null)
  {
    var ltrackdate = new Date();
    var module = oRs.Fields("xmlnode").Value;
    switch (module)
    {
      case "melding":     ltrackdate.setMinutes(recent.getMinutes() - S("mld_melding_recent"));  break;
      case "reservering": ltrackdate.setMinutes(recent.getMinutes() - S("res_reservering_recent")); break;
      case "bestelling":  break;
      case "afspraak":    break;
      case "deel":        break;
    }
    var fac_recent = new Date(oRs.Fields("recentdatum").Value);
    if (fac_recent > ltrackdate)
    {
      lclass += " updated";
    }
  }
  return lclass;
}

function sqlTracking(refkey, node)
{ // Voeg laatste tracking datum toe
  if (node == 'melding' || node == 'reservering')
  {
    sqltrack = ", (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 = " + refkey
             + "      AND sn.fac_srtnotificatie_xmlnode = " + safe.quoted_sql(node)
             + "  ) recentdatum";
  }
  else
	{
	  sqltrack = ", null recentdatum"
	}
  return sqltrack;
}
%>
<html>
  <head>
<%  FCLTHeader.Generate({outputmode: outputmode}) %>
    <script type='text/javascript'>
      function facDetail(row, autolike)
      {
        var facData = eval('(' + row.getAttribute("ROWDATA") + ')');
        var item_key = row.getAttribute("ROWKEY");
        var module = facData.module;
        switch (module)
        {
          case "melding":
              FcltMgr.openDetail("appl/mld/mld_melding.asp?urole=<%=urole%>&mld_key=" + item_key, { reuse: true });
              break;
          case "reservering":
              if (facData.disc_key)
              {
                var deepurl = facData.deepurl;
                if (autolike)
                    deepurl += "&autolike=1";
                url = "appl/res/res_goto_extern.asp?res_disc_key=" + facData.disc_key + "&deepurl=" + escape(deepurl);
                FcltMgr.openDetail(url, { reuse: true, is_extern: true, titel: L("lcl_reservation_extern") + " " + facData.item });
              }
              else // Gewoon intern
                FcltMgr.openDetail("appl/res/res_reservering.asp?urole=<%=urole%>&rsv_ruimte_key=" + item_key, { reuse: true });
              break;
          case "bestelling":
              FcltMgr.openDetail("appl/bes/bes_bestelling.asp?urole=<%=urole%>&bes_key=" + item_key, { reuse: true });
              break;
          case "afspraak":
              FcltMgr.openDetail("appl/bez/bez_afspraak.asp?urole=<%=urole%>&afspr_key=" + item_key, { reuse: true });
              break;
        }
      }

       function fLike (event, itemkey, node, isRemote)
       {
         FcltMgr.stopPropagation(event);
         if (!isRemote)
         {
            FcltMgr.openModalDetail("fac_like.asp?key=" + itemkey + "&node=" + node, L("lcl_fac_like"), {callback: FcltCallbackRefresh});
         }
         else
         {
            var deze = event.srcElement || event.target;
            facDetail($(deze).closest("tr")[0], true);
         }

       }
    </script>
  </head>

  <body id="listbody">
<%
    var xfunc = user.func_enabled("*", null, null, pkey);
    var modules = [];
    if (xfunc.canRead("WEB_MLDFOF") || xfunc.canRead("WEB_MLDBOF"))
      modules.push("melding");
    if (xfunc.canRead("WEB_RESFOF") || xfunc.canRead("WEB_RESBOF"))
      modules.push("reservering");
    if (xfunc.canRead("WEB_BESFOF") || xfunc.canRead("WEB_BESBOF"))
      modules.push("bestelling");
    if (xfunc.canRead("WEB_BEZFOF") || xfunc.canRead("WEB_BEZBOF"))
      modules.push("afspraak");
    if (xfunc.canRead("WEB_INSMAN"))
      modules.push("deel");

    var module_filter;
    if (pkey == user_key)
      module_filter = "";
    else if (modules.length == 0)
      module_filter = " AND 0 = 1";
    else
      module_filter = " AND xmlnode IN (" + safe.quoted_sql_join(modules) + ")";

    var sqln = "SELECT m.prs_perslid_key prs_perslid_key"   // melding van jou
             + "     , isd.ins_srtdiscipline_prefix || TO_CHAR (m.mld_melding_key) item"
             + "     , m.mld_melding_key item_key"
             + "     , 'melding' xmlnode"
             + "     , CASE WHEN (m.mld_melding_status = 0 OR (m.mld_melding_status >= 2 AND m.mld_melding_status <= 4) OR m.mld_melding_status = 7)" // m.mld_melding_status IN (0, 2, 3, 4, 7)
             + "          THEN m.mld_melding_einddatum"
             + "          ELSE tr.fac_tracking_datum"
             + "          END datum"
             + "   , NULL datum_eind"
             + (S("mld_selector_mode") == 0
               ? "   ,   " + lcl.xsql('id.ins_discipline_omschrijving', 'id.ins_discipline_key')
               : "   ,   " + lcl.xsql('sm.mld_stdmelding_omschrijving', 'sm.mld_stdmelding_key'))
                + "||DECODE(mld_melding_onderwerp, NULL, '' , ': '||mld_melding_onderwerp) detail"
             + "     , ' ' status"
             + "     , m.mld_melding_satisfaction satisfaction"
             + "     , m.mld_melding_satisfaction_op satisfaction_op"
             + "     , m.mld_melding_status status_key"
             + "     , NULL bo_status_key"
             + sqlTracking('m.mld_melding_key', 'melding')
             + "  FROM mld_melding m"
             + "     , ins_srtdiscipline isd"
             + "     , ins_tab_discipline id"
             + "     , mld_stdmelding sm"
             + "     , fac_tracking tr"
             + "     , fac_srtnotificatie str"
             + " WHERE id.ins_srtdiscipline_key = isd.ins_srtdiscipline_key"
             + "   AND sm.mld_ins_discipline_key = id.ins_discipline_key"
             + "   AND m.mld_stdmelding_key = sm.mld_stdmelding_key"
             + "   AND m.fac_activiteit_key IS NULL"
             + "   AND m.mld_melding_key = tr.fac_tracking_refkey(+)"
             + "   AND tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key(+)"
             + "   AND (m.mld_workflowstep_key IS NULL OR mld_melding_start_key = mld_melding_key)"
             + "   AND (m.mld_melding_status IN (0, 2, 3, 4, 7)"
             + "      OR (m.mld_melding_status IN (1, 5, 6)"
             + "          AND tr.fac_tracking_datum > SYSDATE - " + S("facilities_flike_past")
             + "          AND str.fac_srtnotificatie_code = 'MLDAFM'))"
             + "   AND (m.mld_melding_einddatum BETWEEN SYSDATE - " + S("facilitiespast_mld") + " AND SYSDATE + " + S("facilitiesfuture_mld") + " OR m.mld_melding_einddatum IS NULL)"
             + " UNION ALL "
             + "SELECT m.prs_perslid_key_voor prs_perslid_key"  // melding voor jou
             + "     , isd.ins_srtdiscipline_prefix || TO_CHAR (m.mld_melding_key) item"
             + "     , m.mld_melding_key item_key"
             + "     , 'melding' xmlnode"
             + "     , CASE WHEN (m.mld_melding_status = 0 OR (m.mld_melding_status >= 2 AND m.mld_melding_status <= 4) OR m.mld_melding_status = 7)" // m.mld_melding_status IN (0, 2, 3, 4, 7)
             + "          THEN m.mld_melding_einddatum"
             + "          ELSE tr.fac_tracking_datum"
             + "          END datum"
             + "   , NULL datum_eind"
             + (S("mld_selector_mode") == 0
               ? "   ,   " + lcl.xsql('id.ins_discipline_omschrijving', 'id.ins_discipline_key') + " detail"
               : "   ,   " + lcl.xsql('sm.mld_stdmelding_omschrijving', 'sm.mld_stdmelding_key') + " detail")
             + "     , ' ' status"
             + "     , m.mld_melding_satisfaction satisfaction"
             + "     , m.mld_melding_satisfaction_op satisfaction_op"
             + "     , m.mld_melding_status status_key"
             + "     , NULL bo_status_key"
             + sqlTracking('m.mld_melding_key', 'melding')
             + "  FROM mld_melding m"
             + "     , ins_srtdiscipline isd"
             + "     , ins_tab_discipline id"
             + "     , mld_stdmelding sm"
             + "     , fac_tracking tr"
             + "     , fac_srtnotificatie str"
             + " WHERE id.ins_srtdiscipline_key = isd.ins_srtdiscipline_key"
             + "   AND sm.mld_ins_discipline_key = id.ins_discipline_key"
             + "   AND m.mld_stdmelding_key = sm.mld_stdmelding_key"
             + "   AND m.fac_activiteit_key IS NULL"
             + "   AND m.prs_perslid_key_voor <> m.prs_perslid_key"  // die had ik in het vorige stuk ook al
             + "   AND m.mld_melding_key = tr.fac_tracking_refkey(+)"
             + "   AND tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key(+)"
             + "   AND (m.mld_workflowstep_key IS NULL OR mld_melding_start_key = mld_melding_key)"
             + "   AND (m.mld_melding_status IN (0, 2, 3, 4, 7)"
             + "      OR (m.mld_melding_status IN (1, 5, 6)"
             + "          AND tr.fac_tracking_datum > SYSDATE - " + S("facilities_flike_past")
             + "          AND str.fac_srtnotificatie_code = 'MLDAFM'))"
             + "   AND (m.mld_melding_einddatum BETWEEN SYSDATE - " + S("facilitiespast_mld") + " AND SYSDATE + " + S("facilitiesfuture_mld") + " OR m.mld_melding_einddatum IS NULL)"
             + " UNION ALL "
             + "SELECT b.prs_perslid_key prs_perslid_key" // Bestelling door jou
             + "     , TO_CHAR (bes_bestelling_key)"
             + "     , bes_bestelling_key"
             + "     , 'bestelling'"
             + "     , CASE WHEN b.bes_bestelling_status >= 2 AND b.bes_bestelling_status <= 5" // b.bes_bestelling_status IN (5, 4, 3, 2)
               + "          THEN b.bes_bestelling_leverdatum"
               + "          ELSE tr.fac_tracking_datum"
               + "          END datum"
             + "     , bes_bestelling_leverdatum datum_eind"
             + "     , (SELECT MIN (" + lcl.xsql('ins_discipline_omschrijving', 'ins_discipline_key') + ") "
             + "          FROM bes_discipline "
             + "         WHERE ins_discipline_key IN (SELECT ins_discipline_key "
             + "                                        FROM bes_srtgroep sg, bes_srtdeel sd, bes_bestelling_item bi "
             + "                                       WHERE sd.bes_srtgroep_key = sg.bes_srtgroep_key "
             + "                                         AND bi.bes_srtdeel_key = sd.bes_srtdeel_key "
             + "                                         AND bi.bes_bestelling_key = b.bes_bestelling_key)) oms"
             + "     , ' ' status"
             + "     , b.bes_bestelling_satisfaction satisfaction"
             + "     , b.bes_bestelling_satisfaction_op satisfaction_op"
             + "     , b.bes_bestelling_status status_key"
             + "     , NULL bo_status_key"
             + sqlTracking('bes_bestelling_key', 'bestelling')
             + "  FROM bes_bestelling b"
             + "     , fac_tracking tr"
             + "     , fac_srtnotificatie str"
             + " WHERE (b.bes_bestelling_status IN (5, 4, 3, 2)"
               + "      OR (b.bes_bestelling_status IN (1, 6, 7, 8)"
               + "          AND tr.fac_tracking_datum > SYSDATE - " + S("facilities_flike_past")
               + "          AND str.fac_srtnotificatie_code = 'BESOTV'))"
             + "   AND b.fac_activiteit_key IS NULL"
             + "   AND b.bes_bestelling_key = tr.fac_tracking_refkey(+)"
             + "   AND tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key(+)"
             + "   AND (b.bes_bestelling_leverdatum BETWEEN SYSDATE - " + S("facilitiespast_bes") + " AND SYSDATE + " + S("facilitiesfuture_bes") + " OR b.bes_bestelling_leverdatum IS NULL)"
             + " UNION "
             + "SELECT b.prs_perslid_key_voor prs_perslid_key" // Bestelling voor jou
             + "     , TO_CHAR (bes_bestelling_key)"
             + "     , bes_bestelling_key"
             + "     , 'bestelling'"
             + "     , CASE WHEN b.bes_bestelling_status >= 2 AND b.bes_bestelling_status <= 5" // b.bes_bestelling_status IN (5, 4, 3, 2)
               + "          THEN b.bes_bestelling_leverdatum"
               + "          ELSE tr.fac_tracking_datum"
               + "          END datum"
             + "     , bes_bestelling_leverdatum datum_eind"
             + "     , (SELECT MIN (" + lcl.xsql('ins_discipline_omschrijving', 'ins_discipline_key') + ") "
             + "          FROM bes_discipline "
             + "         WHERE ins_discipline_key IN (SELECT ins_discipline_key"
             + "                                        FROM bes_srtgroep sg"
             + "                                           , bes_srtdeel sd"
             + "                                           , bes_bestelling_item bi"
             + "                                       WHERE sd.bes_srtgroep_key = sg.bes_srtgroep_key"
             + "                                         AND bi.bes_srtdeel_key = sd.bes_srtdeel_key"
             + "                                         AND bi.bes_bestelling_key = b.bes_bestelling_key)) oms"
             + "     , ' ' status"
             + "     , b.bes_bestelling_satisfaction"
             + "     , b.bes_bestelling_satisfaction_op satisfaction_op"
             + "     , b.bes_bestelling_status status_key"
             + "     , NULL bo_status_key"
             + sqlTracking('bes_bestelling_key', 'bestelling')
             + "  FROM bes_bestelling b"
             + "     , fac_tracking tr"
             + "     , fac_srtnotificatie str"
             + " WHERE (b.bes_bestelling_status IN (5, 4, 3, 2)"
               + "      OR (b.bes_bestelling_status IN (1, 6, 7, 8)"
               + "          AND tr.fac_tracking_datum > SYSDATE - " + S("facilities_flike_past")
               + "          AND str.fac_srtnotificatie_code = 'BESOTV'))"
             + "   AND b.bes_bestelling_key = tr.fac_tracking_refkey(+)"
             + "   AND tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key(+)"
             + "   AND b.prs_perslid_key_voor <> b.prs_perslid_key"  // die had ik in het vorige stuk ook al
             + "   AND (b.bes_bestelling_leverdatum BETWEEN SYSDATE - " + S("facilitiespast_bes") + " AND SYSDATE + " + S("facilitiesfuture_bes") + " OR b.bes_bestelling_leverdatum IS NULL)"
             + " UNION ALL "
             + "SELECT ins_alg_ruimte_key prs_perslid_key"
             + "     , ins_deel_omschrijving"
             + "     , ins_deel_key"
             + "     , 'deel'"
             + "     , NULL datum"         // TODO checken juiste datum zo?
             + "     , NULL datum_tot"
             + "     , " + lcl.xsql('sd.ins_srtdeel_omschrijving', 'sd.ins_srtdeel_key')  //tt
             + "     , ' ' status"
             + "     , to_number(null) satisfaction"
             + "     , NULL satisfaction_op"
             + "     , NULL status_key"
             + "     , NULL bo_status_key"
             + sqlTracking('ins_deel_key', 'deel')
             + "  FROM ins_v_aanwezigdeel d"
             + "     , ins_srtdeel sd"
             + " WHERE d.ins_alg_ruimte_type = 'P' AND sd.ins_srtdeel_key = d.ins_srtdeel_key AND ins_alg_ruimte_key_org IS NOT NULL" // geleend
             + " UNION ALL " // 1 Host ruimtereservering
             + "SELECT rr.res_rsv_ruimte_host_key prs_perslid_key"
             + "     , TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr"
             + "     , rr.res_rsv_ruimte_key"
             + "     , 'reservering'"
             + "     , rr.res_rsv_ruimte_van datum"
             + "     , rr.res_rsv_ruimte_tot datum_tot"
             + "     , COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT " + lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
             + "                                                    FROM res_activiteit ra "
             + "                                                   WHERE res_activiteit_key = rr.res_activiteit_key)) oms"
             + "     , ' ' status"
             + "     , res_rsv_ruimte_satisfaction satisfaction"
             + "     , res_rsv_ruimte_satisfaction_op satisfaction_op"
             + "     , DECODE (res_rsv_ruimte_dirtlevel, 0, rr.res_status_fo_key, -1) status_key"
             + "     , rr.res_status_bo_key bo_status_key"
             + sqlTracking("rr.res_rsv_ruimte_key","reservering")
             + "  FROM res_v_aanwezigrsv_ruimte rr"
             + "     , fac_tracking tr"
             + "     , fac_srtnotificatie str"
             + " WHERE (   (    rr.res_status_bo_key IN (4, 3, 2, 7))"
             + "        OR (    rr.res_status_bo_key IN (5, 6)"
             + "            AND tr.fac_tracking_datum > SYSDATE - " + S("facilities_flike_past")
             + "            AND str.fac_srtnotificatie_code = 'RESAFM')"
             + "        OR (    rr.res_status_bo_key NOT IN (5, 6) "
             + "            AND rr.res_rsv_ruimte_tot > SYSDATE - " + S("facilities_flike_past") + ")"
             + "       )"
             + "   AND rr.res_rsv_ruimte_key = tr.fac_tracking_refkey(+)"
             + "   AND tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key(+)"
             + "   AND (rr.res_rsv_ruimte_van BETWEEN SYSDATE - " + S("facilitiespast_res") + " AND SYSDATE + " + S("facilitiesfuture_res") + " OR rr.res_rsv_ruimte_van IS NULL)"
             + " UNION " // 2 Contact ruimtereservering
             + "SELECT rr.res_rsv_ruimte_contact_key prs_perslid_key"
             + "     , TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr"
             + "     , rr.res_rsv_ruimte_key"
             + "     , 'reservering'"
             + "     , rr.res_rsv_ruimte_van datum"
             + "     , rr.res_rsv_ruimte_tot datum_tot"
             + "     , COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT " + lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
             + "                                                    FROM res_activiteit ra "
             + "                                                   WHERE res_activiteit_key = rr.res_activiteit_key)) oms"
             + "     , ' ' status"
             + "     , res_rsv_ruimte_satisfaction satisfaction"
             + "     , res_rsv_ruimte_satisfaction_op satisfaction_op"
             + "     , DECODE (res_rsv_ruimte_dirtlevel, 0, rr.res_status_fo_key, -1) status_key"
             + "     , rr.res_status_bo_key bo_status_key"
             + sqlTracking("rr.res_rsv_ruimte_key","reservering")
             + "  FROM res_v_aanwezigrsv_ruimte rr"
             + " WHERE rr.res_status_bo_key IN (4, 3, 2, 7)"
             + "   AND (rr.res_rsv_ruimte_van BETWEEN SYSDATE - " + S("facilitiespast_res") + " AND SYSDATE + " + S("facilitiesfuture_res") + " OR rr.res_rsv_ruimte_van IS NULL)"
             + " UNION ALL " // 3 Host voorzieningreservering, dirtlevel van de onderliggenden bepalen?
             + "SELECT rr.res_rsv_ruimte_host_key prs_perslid_key"
             + "     , TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr"
             + "     , rr.res_rsv_ruimte_key"
             + "     , 'reservering'"
             + "     , rr.res_rsv_ruimte_van datum"
             + "     , rr.res_rsv_ruimte_tot datum_tot"
             + "     , COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT " + lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
             + "                                                    FROM res_activiteit ra"
             + "                                                   WHERE res_activiteit_key = rr.res_activiteit_key)) oms"
             + "     , ' ' status"
             + "     , res_rsv_ruimte_satisfaction satisfaction"
             + "     , res_rsv_ruimte_satisfaction_op satisfaction_op"
             + "     , DECODE (res_rsv_ruimte_dirtlevel + res_rsv_deel_dirtlevel, 0, rr.res_status_fo_key, -1) status_key"
             + "     , rr.res_status_bo_key bo_status_key"
             + sqlTracking("rr.res_rsv_ruimte_key","reservering")
             + "  FROM res_v_aanwezigrsv_ruimte rr"
             + "     , res_v_aanwezigrsv_deel dd"
             + " WHERE rr.res_status_bo_key IS NULL AND dd.res_rsv_ruimte_key = rr.res_rsv_ruimte_key"
             + " AND (rr.res_rsv_ruimte_van BETWEEN SYSDATE - " + S("facilitiespast_res") + " AND SYSDATE + " + S("facilitiesfuture_res") + " OR rr.res_rsv_ruimte_van IS NULL)"
             + " UNION " // 4 Contact voorzieningreservering dirtlevel van de onderliggenden bepalen?
             + "SELECT rr.res_rsv_ruimte_contact_key prs_perslid_key"
             + "     , TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr"
             + "     , rr.res_rsv_ruimte_key"
             + "     , 'reservering'"
             + "     , rr.res_rsv_ruimte_van datum"
             + "     , rr.res_rsv_ruimte_tot datum_tot"
             + "     , COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT " + lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
             + "                                                    FROM res_activiteit ra"
             + "                                                   WHERE res_activiteit_key = rr.res_activiteit_key)) oms"
             + "     , ' ' status"
             + "     , res_rsv_ruimte_satisfaction satisfaction"
             + "     , res_rsv_ruimte_satisfaction_op satisfaction_op"
             + "     , DECODE (res_rsv_ruimte_dirtlevel + res_rsv_deel_dirtlevel, 0, rr.res_status_fo_key, -1) status_key"
             + "     , rr.res_status_bo_key bo_status_key"
             + sqlTracking("rr.res_rsv_ruimte_key","reservering")
             + "  FROM res_v_aanwezigrsv_ruimte rr"
             + "     , res_v_aanwezigrsv_deel dd"
             + " WHERE rr.res_status_bo_key IS NULL AND dd.res_rsv_ruimte_key = rr.res_rsv_ruimte_key"
             + "   AND (rr.res_rsv_ruimte_van BETWEEN SYSDATE - " + S("facilitiespast_res") + " AND SYSDATE + " + S("facilitiesfuture_res") + " OR rr.res_rsv_ruimte_van IS NULL)"
             + " UNION ALL " // 5 Host cateringreservering dirtlevel van de onderliggenden bepalen?
             + "SELECT rr.res_rsv_ruimte_host_key prs_perslid_key"
             + "     , TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr"
             + "     , rr.res_rsv_ruimte_key"
             + "     , 'reservering'"
             + "     , rr.res_rsv_ruimte_van datum"
             + "     , rr.res_rsv_ruimte_tot datum_tot"
             + "     , COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT " + lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
             + "                                                    FROM res_activiteit ra"
             + "                                                   WHERE res_activiteit_key = rr.res_activiteit_key)) oms"
             + "     , ' ' status"
             + "     , res_rsv_ruimte_satisfaction satisfaction"
             + "     , res_rsv_ruimte_satisfaction_op satisfaction_op"
             + "     , DECODE (res_rsv_ruimte_dirtlevel + res_rsv_artikel_dirtlevel, 0, rr.res_status_fo_key, -1) status_key"
             + "     , rr.res_status_bo_key bo_status_key"
             + sqlTracking("rr.res_rsv_ruimte_key","reservering")
             + "  FROM res_v_aanwezigrsv_ruimte rr"
             + "     , res_v_aanwezigrsv_artikel dd"
             + " WHERE rr.res_status_bo_key IS NULL AND dd.res_rsv_ruimte_key = rr.res_rsv_ruimte_key "
             + "   AND (rr.res_rsv_ruimte_van BETWEEN SYSDATE - " + S("facilitiespast_res") + " AND SYSDATE + " + S("facilitiesfuture_res") + " OR rr.res_rsv_ruimte_van IS NULL)"
             + " UNION " // 6 Contact cateringreservering dirtlevel van de onderliggenden bepalen?
             + "SELECT rr.res_rsv_ruimte_contact_key prs_perslid_key"
             + "     , TO_CHAR (rr.res_reservering_key) || '/' || rr.res_rsv_ruimte_volgnr"
             + "     , rr.res_rsv_ruimte_key"
             + "     , 'reservering'"
             + "     , rr.res_rsv_ruimte_van datum"
             + "     , rr.res_rsv_ruimte_tot datum_tot"
             + "     , COALESCE (rr.res_rsv_ruimte_omschrijving, (SELECT   "+ lcl.xsql('ra.res_activiteit_omschrijving', 'res_activiteit_key')
             + "                                                    FROM   res_activiteit ra "
             + "                                                   WHERE   res_activiteit_key = rr.res_activiteit_key)) oms"
             + "     , ' ' status"
             + "     , res_rsv_ruimte_satisfaction satisfaction"
             + "     , res_rsv_ruimte_satisfaction_op satisfaction_op"
             + "     , DECODE (res_rsv_ruimte_dirtlevel + res_rsv_artikel_dirtlevel, 0, rr.res_status_fo_key, -1) status_key"
             + "     , rr.res_status_bo_key bo_status_key"
             + sqlTracking("rr.res_rsv_ruimte_key","reservering")
             + "  FROM   res_v_aanwezigrsv_ruimte rr"
             + "     , res_v_aanwezigrsv_artikel dd"
             + " WHERE rr.res_status_bo_key IS NULL AND dd.res_rsv_ruimte_key = rr.res_rsv_ruimte_key"
             + "   AND (rr.res_rsv_ruimte_van BETWEEN SYSDATE - " + S("facilitiespast_res") + " AND SYSDATE + " + S("facilitiesfuture_res") + " OR rr.res_rsv_ruimte_van IS NULL)"
             + " UNION ALL "
             + "SELECT a.bez_afspraak_contact_key prs_perslid_key"
             + "     , TO_CHAR (b.bez_afspraak_key)"
             + "     , b.bez_afspraak_key"
             + "     , 'afspraak'"
             + "     , a.bez_afspraak_datum datum"
             + "     , a.bez_afspraak_eind  datum_tot"
             + "     , b.bez_afspraak_naam || (CASE WHEN bez_afspraak_bedrijf IS NOT NULL THEN ' (' || bez_afspraak_bedrijf || ')' END) oms"
             + "     , (CASE WHEN bez_bezoekers_done IS NOT NULL THEN 'lcl_vis_inside_st' ELSE 'lcl_vis_expected_st' END) status"
             + "     , NULL satisfaction"
             + "     , NULL satisfaction_op"
             + "     , NULL status_key"
             + "     , NULL bo_status_key"
             + sqlTracking('b.bez_afspraak_key', 'afspraak')
             + "  FROM bez_bezoekers b"
             + "     , bez_afspraak a"
             + " WHERE b.bez_afspraak_key = a.bez_afspraak_key AND bez_bezoekers_out IS NULL"
             + "   AND (a.bez_afspraak_datum BETWEEN SYSDATE - " + S("facilitiespast_bez") + " AND SYSDATE + " + S("facilitiesfuture_bez") + " OR a.bez_afspraak_datum IS NULL)"
             + " UNION "
             + "SELECT a.bez_afspraak_host_key prs_perslid_key"
             + "     , TO_CHAR (b.bez_afspraak_key)"
             + "     , b.bez_afspraak_key"
             + "     , 'afspraak'"
             + "     , a.bez_afspraak_datum datum"
             + "     , a.bez_afspraak_eind  datum_tot"
             + "     , b.bez_afspraak_naam || (CASE WHEN bez_afspraak_bedrijf IS NOT NULL THEN ' (' || bez_afspraak_bedrijf || ')' END) oms"
             + "     , (CASE WHEN bez_bezoekers_done IS NOT NULL THEN 'lcl_vis_inside_st' ELSE 'lcl_vis_expected_st' END) status"
             + "     , NULL satisfaction"
             + "     , NULL satisfaction_op"
             + "     , NULL status_key"
             + "     , NULL bo_status_key"
             + sqlTracking('b.bez_afspraak_key', 'afspraak')
             + "  FROM bez_bezoekers b"
             + "     , bez_afspraak a"
             + " WHERE b.bez_afspraak_key = a.bez_afspraak_key AND bez_bezoekers_out IS NULL"
             + " AND (a.bez_afspraak_datum BETWEEN SYSDATE - " + S("facilitiespast_bez") + " AND SYSDATE + " + S("facilitiesfuture_bez") + " OR a.bez_afspraak_datum IS NULL)";

    sqln = "SELECT prs_perslid_key"
         + "     , item"
         + "     , item_key"
         + "     , xmlnode"
         + "     , datum"
         + "     , datum_eind"
         + "     , detail"
         + "     , status"
         + "     , satisfaction"
         + "     , satisfaction_op"
         + "     , status_key"
         + "     , recentdatum"
         + "     , bo_status_key"
         + "  FROM (" + sqln + ")"
         + " WHERE prs_perslid_key = " + pkey
         + " AND xmlnode IS NOT NULL"
         + module_filter
         + " ORDER by datum, xmlnode";

    var buttons = [];
    var calendarurl = 'appl/shared/fullcalendar.asp?worker=faclist';
    buttons = [{ icon: "calendar_1.png", title: L("lcl_calendar"), action: "FcltMgr.openDetail('" + calendarurl + "', '" + L("lcl_prs_frame_facilities") + "')" }]


    var rst = new ResultsetTable({ sql: sqln,
                                   keyColumn: "item_key",
                                   ID: "factable",
                                   noPrint: true,
                                   rowData: fnrowData,
                                   rowClass: fnrowClass,
                                   title: L("lcl_prs_frame_facilities"),
                                   showAll: showall,
                                   outputmode: outputmode,
                                   buttons: buttons,
                                   summaryShow: function(){},
                                   emptySetString: L("lcl_geenfaciliteiten")
                                  });
    rst.addColumn(new Column({caption: L("lcl_myfacilities_date"), content: "datum", datatype: "date", prettydate: true }));
    rst.addColumn(new Column({caption: L("lcl_myfacilities_time"), content: fntime}));
    rst.addColumn(new Column({caption: L("lcl_faciliteit"), content: fncolType}));
    rst.addColumn(new Column({caption: L("lcl_descr"), content: "detail"}));
    rst.addColumn(new Column({caption: L("lcl_status"), content: fnstatus}));
    rst.addColumn(new Column({caption: L("lcl_fac_oordeel"), content: fnLike}));

    rst.addAction({ action: "facDetail", caption:L("lcl_edit"), isDefault: true });

    var cnt = rst.processResultset();

    // Overzicht externe ruimtes
    var mayberemote = Session("has_no_remote_res") != 1;
    if (mayberemote)
    {
        var cnt_remote = 0;
        autfunction = "WEB_RESUSE";
        var sql = res.getremotes_sql(autfunction);

        var oRs = Oracle.Execute(sql);
        // Als je hier weet dat er intern niks was zou je deze externe opties initieel opengeklapt willen?
        while( !oRs.eof )
        {
            var url = oRs("res_disc_params_remoteurl").Value + "&API=RESINFO";
            var disc_key = oRs("ins_discipline_key").Value;
            var xparams = { request: "user_lopend_info",
                            prs_externId: customerId + "_" + String(user_key)
                          }

            var json = HTTP.getJSON(url, { data: xparams });
            if (!json)
            {
                __DoLog("user_lopend_info is onverwacht geen Array bij opvragen van:", "#FF0000");
                __DoLog(xparams);
            }
            else
            {
                if (json.user_lopend_info.length)
                {
                    cnt_remote += json.user_lopend_info.length;
            %>
                      <div class="resexternlist">
                           <span class="resexterntitle">
                             <%= L("lcl_res_extern_prefix") + safe.html(oRs("ins_discipline_omschrijving").Value)%>
                           </span>
                        <div class="resexterndiv">
            <%
                    rst.buttons = [];
                    rst.sql = null;
                    rst.ID = "factable2";
                    rst.dataset = json.user_lopend_info;
                    var cnt = rst.processResultset({ disc_key: disc_key });
                %>
                      </div>
                  </div>
                <%
                }
            }
            oRs.MoveNext();
        }
        oRs.close();
        if (!cnt_remote)
        {
            __Log("No remotes found. Setting Session('has_no_remote_res')");
            Session("has_no_remote_res") = 1; // Wordt eventueel in res_goto_extern.asp gereset
        }
    }
    else
        __Log("Session('has_no_remote_res') is set. Skipping remotes check.");
%>
  </body>
</html>
