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

     File:          status_info.asp
     Beschrijving:  Toont de trackinginfo van een entiteit, waarbij entiteit wordt bepaald
                    door de naam van de de meegegeven key
                    - mld_key = melding
                    - opdr_key = opdracht
                    - res_rsv_ruimte_key = reservering
                    - afspr_key = afspraak
                    - ... etc
    Note:           Met TAMPER bescherming

*/%>
<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/status.inc"-->
<!--#include file="../Shared/resultset_table_v2.inc" -->
<%
    protectQS.verify({ allowparams: ["outputmode", "showall", "autosortby", "no_autoscroll"]}); // tamper check
    var outputmode = getQParamInt("outputmode", 0);
%>
<html>
  <head>
<%  FCLTHeader.Generate({ outputmode: outputmode }); %>
    <script>
      function persoonDetails(evt, perslid_key)
      {
        FcltMgr.stopPropagation(evt);
        FcltMgr.openDetail("appl/fac/fac_user.asp?prs_key=" + perslid_key);
      }
      function noti_forcesend(row)
      {
        var trkData = JSON.parse(row.getAttribute("ROWDATA"));

        var tracking_key = row.getAttribute("rowkey");
        FcltMgr.confirm("Notificatie {0} opnieuw sturen?\nLet op: alleen naar technische adressen, niet naar e-mail/sms/app".format(trkData.srtnoti),
            { }, // De multiactie vraagt zelf al om bevestiging
            function () {
              var url = "./noti_forcesend.asp";
              var data = {
                tracking_key: tracking_key
              };
              protectRequest.dataToken(data);
              $.post(url, data, FcltCallbackRefresh, "json");
            }
        );
      }
    </script>
  </head>

  <body class="listmode">
<%
    var mld_key         = getQParamInt('mld_key', -1);
    var opdr_key        = getQParamInt('opdr_key', -1);
    var rsv_ruimte_key  = getQParamInt('rsv_ruimte_key', -1);
    var afspr_key       = getQParamInt('afspr_key', -1);
    var cnt_key         = getQParamInt('cnt_key', -1);
    var ins_key         = getQParamInt('ins_key', -1);
    var srtdeel_key     = getQParamInt('srtdeel_key', -1);
    var insc_key        = getQParamInt('insc_key', -1);   // controle (inspectie)
    var inssc_key       = getQParamInt('inssc_key', -1);  // srtcontrole (periodieke taak)
    var faq_key         = getQParamInt('faq_key', -1);
    var fin_key         = getQParamInt('fin_key', -1);
    var bes_key         = getQParamInt('bes_key', -1);
    var ord_key         = getQParamInt('ord_key', -1);
    var prs_key         = getQParamInt('prs_key', -1);
    var kpn_key         = getQParamInt('kpn_key', -1);
    var room_key        = getQParamInt('room_key', -1);
    var reg_key         = getQParamInt('reg_key', -1);
    var dis_key         = getQParamInt('dis_key', -1);
    var loc_key         = getQParamInt('loc_key', -1);
    var bld_key         = getQParamInt('bld_key', -1);
    var flr_key         = getQParamInt('flr_key', -1);
    var wp_key          = getQParamInt('wp_key',  -1);
    var ter_key         = getQParamInt('ter_key', -1);
    var bedr_key        = getQParamInt('bedr_key', -1);
    var badr_key        = getQParamInt('badr_key', -1);
    var act_key         = getQParamInt('act_key', -1);
    var message_key     = getQParamInt('message_key', -1);

    var urole           = getQParam('urole', 'fe');
    var print           = getQParam ("print", null);

    var entkey = -1;
    var enttype ='unsupported';
    switch (true)
    {
        case mld_key != -1:         entkey = mld_key;          enttype ='melding'; break;
        case opdr_key != -1:        entkey = opdr_key;         enttype ='opdracht'; break;
        case rsv_ruimte_key != -1:  entkey = rsv_ruimte_key;   enttype =['reservering', 'xreservering']; break;
        case afspr_key != -1:       entkey = afspr_key;        enttype ='afspraak'; break;
        case cnt_key != -1:         entkey = cnt_key;          enttype ='contract'; break;
        case ins_key != -1:         entkey = ins_key;          enttype ='deel'; break;
        case srtdeel_key != -1:     entkey = srtdeel_key;      enttype ='srtdeel'; break;
        case insc_key != -1:        entkey = insc_key;         enttype ='controle'; break;
        case inssc_key != -1:       entkey = inssc_key;        enttype ='taak'; break;
        case faq_key != -1:         entkey = faq_key;          enttype ='kennisbank'; break;
        case fin_key != -1:         entkey = fin_key;          enttype ='factuur'; break;
        case bes_key != -1:         entkey = bes_key;          enttype ='bestelling'; break;
        case ord_key != -1:         entkey = ord_key;          enttype ='bestelopdr'; break;
        case prs_key != -1:         entkey = prs_key;          enttype ='perslid'; break;
        case kpn_key != -1:         entkey = kpn_key;          enttype ='kostenplaats'; break;
        case room_key != -1:        entkey = room_key;         enttype ='ruimte'; break;
        case reg_key != -1:         entkey = reg_key;          enttype ='regio'; break;
        case dis_key != -1:         entkey = dis_key;          enttype ='district'; break;
        case loc_key != -1:         entkey = loc_key;          enttype ='locatie'; break;
        case bld_key != -1:         entkey = bld_key;          enttype ='gebouw'; break;
        case flr_key != -1:         entkey = flr_key;          enttype ='verdieping'; break;
        case wp_key  != -1:         entkey = wp_key;           enttype ='werkplek'; break;
        case ter_key != -1:         entkey = ter_key;          enttype ='terreinsector'; break;
        case bedr_key != -1:        entkey = bedr_key;         enttype ='bedrijf'; break;
        case badr_key != -1:        entkey = badr_key;         enttype ='bedrijfadres'; break;
        case act_key != -1:         entkey = act_key;          enttype ='activiteit'; break;
        case message_key != -1:     entkey = message_key;      enttype ='message'; break;
    }
    if (typeof enttype == 'string')
        enttype = [enttype];

    if (entkey != -1)
    {
      // Bij een status overzicht van een reservering kunnen ook afspraak/bezoekers en meldingen historie regels voorkomen
      var res_afspr_key = -1;
      var res_mld_key = [];
      if (rsv_ruimte_key > 0 && entkey == rsv_ruimte_key)
      { // Heeft de reservering ook een afspraak?
        sql = "SELECT ba.bez_afspraak_key"
            + "  FROM bez_afspraak ba"
            + " WHERE ba.res_rsv_ruimte_key = " + rsv_ruimte_key;
        oRs = Oracle.Execute(sql);
        if (!oRs.eof)
          res_afspr_key = oRs("bez_afspraak_key").value;
        oRs.Close();
        // Heeft de reservering ook meldingen?
        sql = "SELECT m.mld_melding_key"
            + "  FROM mld_melding m"
            + " WHERE m.res_rsv_ruimte_key = " + rsv_ruimte_key;
        oRs = Oracle.Execute(sql);
        while (!oRs.eof)
        {
          res_mld_key.push(oRs("mld_melding_key").value);
          oRs.MoveNext();
        }
        oRs.Close();
      }

     var sql11 = "SELECT tr.fac_tracking_datum"
          + "     , tr.fac_tracking_key"
          + "     , str.fac_srtnotificatie_code"
          + "     , pf.prs_perslid_naam_full"
          + "     , tr.prs_perslid_key"
          + "     , tr.fac_tracking_oms"
          + "     , tr.fac_tracking_refkey"
          + "     , str.fac_srtnotificatie_xmlnode"
          + "  FROM fac_tracking tr, fac_srtnotificatie str, prs_v_perslid_fullnames_all pf"
          + " WHERE tr.fac_srtnotificatie_key = str.fac_srtnotificatie_key"
          + "   AND tr.prs_perslid_key = pf.prs_perslid_key (+)";

     sql = sql11
         + " AND (tr.fac_tracking_refkey = " + entkey + " AND str.fac_srtnotificatie_xmlnode IN (" + safe.quoted_sql_join(enttype) +")";

     if (res_afspr_key > 0)
          // Bewust: *hier* wordt niet getoond of bezoek binnen/buiten is gemeld; dat zie je alleen bij de afspraak
          // (dat is een andere dynamiek dan het wijzigen van de reservering, irrelevant hier)
          sql += " OR (tr.fac_tracking_refkey = " + res_afspr_key + " AND str.fac_srtnotificatie_xmlnode IN ('afspraak'))";
     if (res_mld_key.length > 0)
         sql += " OR (tr.fac_tracking_refkey IN (" + res_mld_key.join(",") + ") AND str.fac_srtnotificatie_xmlnode IN ('melding'))";
     sql += " )";

     // In Or-constructie komt er geen antwoord, zo wel.
     if (enttype[0] == "afspraak")
     {
        sql += " UNION "
            + sql11
            + " AND str.fac_srtnotificatie_xmlnode = 'bezoeker' "
            + " AND tr.fac_tracking_refkey IN (SELECT bez_bezoekers_key FROM bez_bezoekers WHERE bez_afspraak_key = " + entkey + ")";
     }

     // Op volgorde van invoer (key). Datum + tijd kan gelijk zijn.
     sql += " ORDER BY fac_tracking_datum DESC, fac_tracking_key DESC";

      function fncolCode(oRs)
      {
        var pretext = "";
        if (oRs("fac_srtnotificatie_xmlnode").value == "melding" && res_mld_key.length > 0)
        { // De reservering heeft ook meldingen
          pretext = L("lcl_complain") + " " + oRs("fac_tracking_refkey").value + ": ";
        }

        // Toon de tekst van de omschrijving of als die leeg is gewoon de statustekst.
        if (oRs("fac_tracking_oms").value != null)
          var tekst = pretext + safe.html(oRs("fac_tracking_oms").value);
        else // fallback voor ongedefineerde teksten
          var tekst = pretext + status.getsrtnotificatietext(oRs("fac_srtnotificatie_code").value);
        return  tekst;
      }

      function fnPerslidNaamFull(oRs)
      {
        if (oRs("prs_perslid_key").Value === null) {
          if (outputmode != 0) {
            return L("lcl_system_user");
          } else {
            return "<span class='italic'>" + L("lcl_system_user") + "</span>";
          }
        }
        if (outputmode != 0)
            return safe.html(oRs("prs_perslid_naam_full").Value);

        if (oRs("prs_perslid_key").Value == user_key) {
            var html = "<span class='self'>"
                     + safe.html(oRs("prs_perslid_naam_full").Value)
                     + "</span>";
        } else {
            var html = "<span class='details' onclick='persoonDetails(event,"+ oRs("prs_perslid_key").Value +")'>"
                     + safe.html(oRs("prs_perslid_naam_full").Value)
                     + "</span>";
        }
        return html;
      }

      function fnrowActionEnabler(oRs)
      {
        var data = {};
        var srtnoti = oRs("fac_srtnotificatie_code").Value;
        data.eResend = srtnoti && !srtnoti.match(/...TRK$/) && !srtnoti.match(/ORDSN.$/); // TRK en zo

        return data;
      }

      function fnrowData(oRs)
      {
        var data = { srtnoti: oRs("fac_srtnotificatie_code").Value };
        return JSON.stringify(data);
      }

      var buttons = [];
      buttons.push({ icon: "fa-fclt-refresh", title: L("lcl_refresh"), action: "FcltMgr.reload()" });

      var rs_params = { sql:sql,
                        keyColumn: "fac_tracking_key",
                        ID: "trackingtable",
                        outputmode: outputmode,
                        showAll: getQParamInt("showall", 0),
                        buttons: buttons
                      }
      if (user.has("WEB_FACTAB"))
      {
        rs_params.rowData = fnrowData;
        rs_params.rowActionEnabler = fnrowActionEnabler;
      }
      var rst = new ResultsetTable(rs_params);

      rst.addColumn(new Column({caption: L("lcl_date"), content: "fac_tracking_datum", datatype: "date", prettydate: true, nowrap: true }));
      rst.addColumn(new Column({caption: L("lcl_date_time"), content: "fac_tracking_datum", datatype: "time"}));
      params = {};
      if (__Logging & 1 || user.has("WEB_FACFAC"))
          params.tooltip = "fac_srtnotificatie_code";

      rst.addColumn(new Column({caption: L("lcl_mld_statuschange"), content: fncolCode, tooltip: params.tooltip}));
      if (S("mld_fe_show_statusnames") == 1 || urole != 'fe')
      {
        rst.addColumn(new Column({caption: L("lcl_mld_changedby"), content: fnPerslidNaamFull}));
        }

    if (user.has("WEB_FACTAB"))
    {
      rst.addAction({ action: "noti_forcesend", caption: "Force resend", enabler: "eResend", multi: false } );
    }


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