<%@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">

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

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

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

		</script>
		<style>
		   table { margin: 10px; }
		   tr.sk { border-left: 5px solid #fafafa; border-right: 5px solid #fafafa; }
		   tr.border { border-left: 5px solid #f00; border-right: 5px 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>
  <form id="wf" name="wf" action="workflow_overview.asp" method="post">
<%
    BLOCK_START("mldWFOverview", L("lcl_mld_workflow_overview") );
      //RWCHECKBOXTR("color", "fldcheck", "Kleurtjes", (colorise==1?" checked":""))
      makeOverview();
    BLOCK_END();
    var buttons =  [ {title: L("lcl_refresh"), icon: "refresh.png", action: "mld_refresh()"}
                   , {title: L("lcl_cancel"),  icon: "cancel.png",  action: "mld_cancel()" } ];
    CreateButtons(buttons);
    IFACE.FORM_END();
    var graphurl = "./workflow_graph.asp?start_key=" + start_key;
%>
    <img src="<%=safe.htmlattr(graphurl)%>">
  </form>
</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,"
               + "       ws.mld_workflowstep_omschrijving,"
               + "       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"
               + " ORDER BY mld_workflowstep_omschrijving";

       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_omschrijving,"
            + "       mld_workflowstep_jointype,"
            + "       ms.mld_stdmelding_omschrijving,"
            + "       ms.mld_stdmelding_key,"
            + "       md.ins_discipline_key,"
            + "       md.ins_srtdiscipline_key,"
            + "       FLOOR( (SELECT MAX(mld_srtkenmerk_key) FROM mld_srtkenmerk)/100 + 1)*100 maxclr"
            + "  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
            + " ORDER BY mld_workflowstep_omschrijving";

    var oRs = Oracle.Execute(sql);

    var maxClr = oRs("maxclr").value;
    var desc = "<p>Step: <b>"+oRs("mld_workflowstep_omschrijving").Value+"</b>"
    if (isMerge)
    {
        var jointype = oRs("mld_workflowstep_jointype").Value;
        desc += "<br>JOIN: " + (jointype?"OR":"AND");
    }
    desc += "<br/>"+L('lcl_complain')+": "+oRs("mld_stdmelding_omschrijving").Value;
    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,"
             + "          mld_srtkenmerk_omschrijving,"
             + "          mld_srtkenmerk_kenmerktype,"
             + "          mkm.mld_srtkenmerk_key"
             + "     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) s"
             + "    WHERE     mkm.mld_kenmerk_verwijder IS NULL"
             + "          AND mkm.mld_stdmelding_key = ref_mld_stdmelding_key"
             + "          AND mkm.mld_kenmerk_niveau = s.mld_kenmerk_niveau"
             + "          AND mkm.mld_srtkenmerk_key = msk.mld_srtkenmerk_key"
             + "          AND 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("mld_srtkenmerk_omschrijving").Value;
       if (colorise != -1) {
          clr = Math.round(oRs1("mld_srtkenmerk_key").value / maxClr * 255*255*255).toString(16);
          clr = pad.substring(0, pad.length - clr.length) + clr;
       }
       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("mld_srtkenmerk_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;
}
%>