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

    File:               workflow_overview.asp
    Description:        Toont een workflow met flexkenmerken wat overzichtelijker
    Parameters:         start_key (REQUIRED)
    Context:
    Note:               as-is

*/ %>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<head>
<%
  var start_key = getQParamInt("start_key");
  var colorise = getQParam("color", 1);
  var authparams = user.checkAutorisation("WEB_PRSSYS");
%>
<%	FCLTHeader.Generate(); %>
		<script type="text/javascript">

			async function mld_refresh()
			{
				if (!await validateForm("wf"))
					return false;
        FcltMgr.reload();
			}

			function mld_cancel()
			{
				FcltMgr.closeDetail(window, { cancel: true } );
			}

      $(function()
      {
        $(".sk").hover(function(){
           $("."+this.className.split(' ').join('.')).toggleClass('border');
        });$(".sk").click(function(){
           $("."+this.className.split(' ').join('.')).toggleClass('border');
        });
      });

		</script>
		<style>
		   tr.sk { border-left: 5px solid #fafafa; border-right: 5px solid #fafafa; }
		   tr.border td { border-left: 2px solid #f00; border-right: 2px solid #f00; }
		   tr.hdr { border-left: 5px solid #fff; border-right: 5px solid #fff; }
		   tr.hdr th {border-right: 1px solid #ddd}
		   tr.sk td { color: #ffffff; padding: 3px 6px; border-right: 1px solid #ddd; }
		   td.nr { text-align: center;}
		</style>
</head>
<body>
    <%SUBFRAME_START();
    var buttons = []
    buttons.push({ icon: "fa-fclt-refresh", title: L("lcl_refresh"), action: "FcltMgr.reload()", id: "btn_scf_refresh" });
    IFRAMER_HEADER(L("lcl_mld_workflow_overview") , buttons);

    BLOCK_START("mldWFOverview", L("lcl_mld_workflow_overview"), {wide: true, icon: "fa-th"});
      //RWCHECKBOXTR("color", "fldcheck", "Kleurtjes", (colorise == 1? " checked" : ""))
      makeOverview();
    BLOCK_END();
    SUBFRAME_END();
%>
</body>
</html>

<%
function makeOverview()
{
  var keys = [];
  var desc;
  var allkenmerken = {}; // een rule condition heeft altijd betrekking op de kenmerken van de
                         // vorige step. Die onthouden we in allkenmerken

  // start_key wordt verondersteld een root_key te zijn
  var desc = buildStepHTML(start_key, allkenmerken);
  Response.Write("<tr><td>" + desc + "</td></tr>");
  keys.push(start_key);

  for (i = 0; i < 10 && keys.length; i++)  // Maximaal 10 stappen?
  {
    var isMerge = keys.length > 1;
    var sql = "SELECT DISTINCT ws.mld_workflowstep_key,"
            + "       wr.mld_workflowrule_condition"
            + "  FROM mld_workflowrule wr,"
            + "       mld_workflowstep ws"
            + " WHERE wr.mld_workflowstep_key IN (" + keys.join(",") + ")"
            + "   AND wr.mld_workflowstep_next_key NOT IN (" + keys.join(",") + ")"
            + "   AND ws.mld_workflowstep_key = wr.mld_workflowstep_next_key";

    var oRs = Oracle.Execute(sql);

    Response.Write("<tr>");
    keys.length = 0; // Opnieuw opbouwen voor volgende niveau
    while (!oRs.Eof)
    {
      step_key = oRs("mld_workflowstep_key").Value;
      keys.push(oRs("mld_workflowstep_key").Value );
      desc = buildStepHTML(step_key, allkenmerken, isMerge)
      var condition = oRs("mld_workflowrule_condition").Value;
      var condition_friendly_safe = condition || "";

      if (condition)
      {
        for (var key in allkenmerken)
          condition_friendly_safe = condition_friendly_safe.replace(new RegExp(":flex" + key, 'g'), "<em>" + safe.html(allkenmerken[key]) + "</em>");

        desc = "<strong>WHEN:</strong> " + safe.html(condition) + "<br><strong>FRIENDLY:</strong> " + condition_friendly_safe + "<br>" + desc;
      }
      Response.Write("<td style='vertical-align:top;border:1px solid grey;'>" + desc + "</td>");
      oRs.MoveNext();
    }
    Response.Write("</tr>");
  }
}

function buildStepHTML(step_key, allkenmerken, isMerge)
{
  var pad = 'ffffff';
  var sql = "SELECT mld_workflowstep_jointype,"
          + "       ms.mld_stdmelding_omschrijving,"
          + "       ms.mld_stdmelding_key,"
          + "       md.ins_discipline_key,"
          + "       md.ins_srtdiscipline_key"
          + "  FROM mld_workflowstep ws,"
          + "       mld_stdmelding ms,"
          + "       mld_discipline md"
          + " WHERE ws.mld_stdmelding_key = ms.mld_stdmelding_key"
          + "   AND md.ins_discipline_key = ms.mld_ins_discipline_key"
          + "   AND ws.mld_workflowstep_key = " + step_key;

  var oRs = Oracle.Execute(sql);

  var desc = ("<p>{0}: <b>" + oRs("mld_stdmelding_omschrijving").Value + "</b>").format(L("mld_workflowstep"))
  if (isMerge)
  {
    var jointype = oRs("mld_workflowstep_jointype").Value;
    desc += "<br>JOIN: " + (jointype? "OR" : "AND");
  }
  desc += "</p>";

  var sql1 = "SELECT mkm.mld_kenmerk_key"
           + "     , mkm.mld_kenmerk_niveau"
           + "     , mkm.mld_kenmerk_verplicht"
           + "     , mkm.mld_kenmerk_groep"
           + "     , mld_kenmerk_volgnummer"
           + "     , mld_kenmerk_toonbaar"
           + "     , COALESCE (" + lcl.xsql("mkm.mld_kenmerk_omschrijving", "mkm.mld_kenmerk_key")
           + "              ,  " + lcl.xsql("msk.mld_srtkenmerk_omschrijving", "msk.mld_srtkenmerk_key") + ") kenmerk_omschrijving"
           + "     , mld_srtkenmerk_kenmerktype"
           + "     , mkm.mld_srtkenmerk_key"
           + "     , TO_CHAR(dbms_utility.get_hash_value(mkm.mld_srtkenmerk_key,0,power(2,24)), '0XXXXXX') clr"
           + "  FROM mld_kenmerk mkm,"
           + "       mld_srtkenmerk msk,"
           + "       (SELECT 'S' mld_kenmerk_niveau"
           + "             , mld_stdmelding_key ref_mld_stdmelding_key"
           + "             , mld_stdmelding_key"
           + "             , ins_discipline_key"
           + "             , m2.ins_srtdiscipline_key"
           + "          FROM mld_stdmelding md, mld_discipline m2"
           + "         WHERE md.mld_ins_discipline_key = m2.ins_discipline_key"
           + "        UNION ALL"
           + "        SELECT 'D' mld_kenmerk_niveau"
           + "             , ins_discipline_key ref_mld_stdmelding_key"
           + "             , NULL mld_stdmelding_key"
           + "             , ins_discipline_key ins_discipline_key"
           + "             , ins_srtdiscipline_key"
           + "          FROM mld_discipline m2"
           + "        UNION ALL"
           + "        SELECT 'T' mld_kenmerk_niveau"
           + "             , ins_srtdiscipline_key ref_mld_stdmelding_key"
           + "             , NULL mld_stdmelding_key"
           + "             , NULL ins_discipline_key"
           + "             , ins_srtdiscipline_key"
           + "          FROM ins_srtdiscipline m2"
           + "        UNION"
           + "        SELECT 'A' mld_kenmerk_niveau"
           + "             , NULL ref_mld_stdmelding_key"
           + "             , NULL mld_stdmelding_key"
           + "             , NULL ins_discipline_key"
           + "             , NULL ins_srtdiscipline_key"
           + "          FROM DUAL) s"
           + " WHERE     mkm.mld_kenmerk_verwijder IS NULL"
           + "       AND (mkm.mld_stdmelding_key = ref_mld_stdmelding_key OR mkm.mld_stdmelding_key IS NULL)"
           + "       AND mkm.mld_kenmerk_niveau = s.mld_kenmerk_niveau"
           + "       AND mkm.mld_srtkenmerk_key = msk.mld_srtkenmerk_key"
           + "       AND (('A' = mkm.mld_kenmerk_niveau AND mkm.mld_stdmelding_key IS NULL)"
           + "         OR (s.ins_srtdiscipline_key = " + oRs("ins_srtdiscipline_key").Value
           + "         AND (s.ins_discipline_key = " + oRs("ins_discipline_key").Value
           + "           OR ('T' = mkm.mld_kenmerk_niveau AND s.ins_srtdiscipline_key = " + oRs("ins_srtdiscipline_key").Value + "))"
           + "         AND (s.mld_stdmelding_key = " + oRs("mld_stdmelding_key").Value
           + "           OR ('D' = mkm.mld_kenmerk_niveau AND s.ins_discipline_key = " + oRs("ins_discipline_key").Value + ")"
           + "           OR ('T' = mkm.mld_kenmerk_niveau AND s.ins_srtdiscipline_key = " + oRs("ins_srtdiscipline_key").Value + "))"
           + "            )"
           + "           )"
           + " ORDER BY mld_kenmerk_volgnummer";

  var oRs1 = Oracle.Execute(sql1);
  desc += "<table><tr class='hdr'><th>key</th><th>nr</th><th>kenmerk</th><th>grp</th><th>verpl</th><th>ntw</th><th>niv</th></tr>";
  while (!oRs1.Eof) // over alle kenmerken van deze melding
  {
    allkenmerken[oRs1("mld_kenmerk_key").value] = oRs1("kenmerk_omschrijving").Value;
    if (colorise != -1) {
       clr = oRs1("clr").Value.substr(2);
    }
    desc += "<tr class='sk sk" + oRs1("mld_srtkenmerk_key").value + "' style='background-color: #" + clr + ";'>"
          + "<td class='nr'>" + oRs1("mld_kenmerk_key").Value + "</td>"
          + "<td class='nr'>" + oRs1("mld_kenmerk_volgnummer").Value + "</td>"
          + "<td>" + oRs1("kenmerk_omschrijving").Value + "</td>"
          + "<td class='nr'>" + (oRs1("mld_kenmerk_groep").Value >= 1? oRs1("mld_kenmerk_groep").Value : "") + "</td>"
          + "<td class='nr'>" + (oRs1("mld_kenmerk_verplicht").Value>=1? oRs1("mld_kenmerk_verplicht").Value : "") + "</td>"
          + "<td class='nr'>" + (oRs1("mld_kenmerk_toonbaar").Value == 1? "1" : "") + "</td>"
          + "<td class='nr'>" + oRs1("mld_kenmerk_niveau").Value + "</td>"
          + "</tr>";
    oRs1.MoveNext();
  }
  desc +="</table>";
  oRs.Close();

  return desc;
}
%><% ASPPAGE_END(); %>
