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

    File:        workflow_graph_png.asp
    Description: Maak een mooie grafiek
    Parameters:  start_key (REQUIRED)
    Context:
    Note:        as-is

*/ %>
<!-- #include file="../api2/api2.inc" -->
<!-- #include file="../api2/model_mld_workflowstep.inc" -->
<%
DOCTYPE_Disable = 1;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->

<%
    var mld_root_key = getQParamInt("mld_key", -1);
    var rankdir = getQParamSafe("rankdir", "LR");

    var transitParam = buildTransitParam(["mld_key", "start_key"]);

    FCLTHeader.Requires({ plugins: ["jQuery", "vis-network"] });

    var nodes = [];
    var edges = [];

    var start_key = getQParamInt("start_key");
    var xxx_data = new model_mld_workflowstep(start_key).REST_GET({ filter: {}, include: [ "workflowrules" ] });

    for (var s = 0; s < xxx_data.length; s++) {
        var step = xxx_data[s];
        var label = step.issuetype.name;
        if (!label) {
            label = "<<not found>>"; // vervallen of verwijderd. Eigenlijk not-supported configuratie
        }
        var node = { id: step.id, level: parseInt(step.level, 10) };
        if (mld_root_key > 0) {
            var sql = "SELECT mld_melding_key, mld_melding_datum"
                    + "  FROM mld_melding"
                    + " WHERE mld_melding_start_key = " + mld_root_key
                    + "   AND mld_workflowstep_key = " + step.id;
            var oRs = Oracle.Execute(sql);
            if (!oRs.EoF) {
                label = L("lcl_complain") +  " {0}, {1}\n{2}".format(oRs("mld_melding_key").Value, toDateTimeString(oRs("mld_melding_datum").Value), label);
                node.key = oRs("mld_melding_key").Value;
                node.color = {
                    background: "#5cb02e" // var(--aareon-color-7)
                };
            }
            oRs.Close()
        } else {
            label = step.id + ". " + label;
        }
        node.label = label;
        nodes.push(node);
        if (step.workflowrules) {
            for (var r = 0; r < step.workflowrules.length; r++) {
                var rule = step.workflowrules[r];
                var from = step.id;
                var to = rule.nextstep_id;
                var cond = rule.condition;
                var cond2 = "";
                if (cond && !(mld_root_key > 0)) {
                    var cond2 = cond.replace(/\:flex([0-9]+)/g, function (match, capture) {
                        var sql = "SELECT mkm.mld_kenmerk_niveau"
                                + "     , COALESCE (" + lcl.xsql("mkm.mld_kenmerk_omschrijving", "mkm.mld_kenmerk_key")
                                + "              ,  " + lcl.xsql("msk.mld_srtkenmerk_omschrijving", "msk.mld_srtkenmerk_key") + ") kenmerk_omschrijving"
                                + "  FROM mld_kenmerk mkm"
                                + "     , mld_srtkenmerk msk"
                                + " WHERE mkm.mld_kenmerk_verwijder IS NULL"
                                + "   AND mkm.mld_srtkenmerk_key = msk.mld_srtkenmerk_key"
                                + "   AND mld_kenmerk_key = " + parseInt(capture, 10);
                        var oRs = Oracle.Execute(sql);
                        if (!oRs.EoF) {
                            var naam = oRs("kenmerk_omschrijving").Value;
                        } else {
                            var naam = '(Niet gevonden :flex' + capture + ")";
                        }
                        oRs.Close();
                        return match.replace(":flex"+capture, ":flex"+capture + "[" + naam + "]");
                    });
                    cond2 = cond2.replace(/ OR /g, "\n OR ");
                    cond2 = cond2.replace(/ AND /g, "\n AND ");
                }
                var edge = { from: step.id, to: rule.nextstep_id };
                if (cond2) {
                    edge.label = L("mld_workflowrule_condition");
                    edge.title = "WHEN\n" + cond2;
                }
                edges.push(edge);
            }
        }
    }
    var maxLabelLength = 200;
    for (var x in nodes) {
        if (!nodes[x].label) {
            continue;
        }
        var nodeLines = nodes[x].label.split("\n");
        for (var y in nodeLines) {
            maxLabelLength = Math.max(maxLabelLength, 6 * nodeLines[y].length);
        }
    }
%>
<html>
    <head>
<%  FCLTHeader.Generate(); %>
        <script>
            function swap_dir() {
                window.location.href = "?rankdir=<%=(rankdir === "LR" ? "UD" : "LR")%><%=transitParam%>";
            }
        </script>
    </head>
    <body>
<%
PAGE_START();
    var buttons = [];
    buttons.push({ icon: "fa-fclt-refresh", title: L("lcl_refresh"), action: "FcltMgr.reload()", id: "btn_scf_refresh" });
    buttons.push({
        icon: "fa-code-branch " + (rankdir === "UD" ? "fa-rotate-90" : "fa-flip-vertical"),
        title: (rankdir === "UD" ? L("lcl_facmgt_horizontal") : L("lcl_facmgt_vertical")),
        action: "swap_dir()"
    });
    IFRAMER_HEADER(L("lcl_mld_workflow_overview"), buttons);
%>  <div id="diagram"></div>
        <script>
            const container = $("#diagram").get(0);
            const nodes = <%=JSON.stringify(nodes)%>;
            const edges = <%=JSON.stringify(edges)%>;
            for (var x in edges) {
                if ("title" in edges[x]) {
                    const container = document.createElement("pre");
                    container.innerHTML = edges[x].title;
                    container.className = "expr-tooltip";
                    edges[x].title = container;
                }
            }
            const networkData = {
                nodes: new vis.DataSet(nodes),
                edges: new vis.DataSet(edges),
            };
            const options = visnetwork.getDefaultOptions("<%=rankdir%>");

            /* Custom options voor deze toepassing */
            options.nodes.widthConstraint = {
                "maximum": 250
            };
            options.nodes.chosen = {
                node: function (values, id, selected, hovering) {
                    if (selected) {
                        values.borderWidth = 2;
                    }
                }
            };
            options.edges.arrows = "to";
            options.layout.hierarchical.nodeSpacing = 2.5 * options.layout.hierarchical.nodeSpacing;
            options.layout.hierarchical.levelSeparation = <%=maxLabelLength%>;
            /* Einde custom options */

            const network = new vis.Network(container, networkData, options);
            visnetwork.init(network);
            network.on("doubleClick", function (params) {
                if (params?.nodes?.length === 1) {
                    var node = null;
                    for (var i in nodes) {
                        if (nodes[i].id === params.nodes[0]) {
                            node = nodes[i];
                            break;
                        }
                    }
                    if (node?.key) {
                        parent.open_melding(node.key);
                    }
                }
            });
        </script>
<%
PAGE_END();
%>
  </body>
</html>
<%
ASPPAGE_END();
%>
