<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:include href="/app/tools/support/templates/layout.xsl" />

<xsl:template name="mainBody" >
    <style type="text/css" media="screen">
    	@import url(/production/css/rps_main.css?v=8);
    </style>
    <script type="text/javascript" src="/production/javascript/jquery/jquery-3.6.0.min.js"></script>
    <script type="text/javascript" src="/production/javascript/jquery/jquery-ui.min.1.13.0.js"></script>

    <script type="text/javascript" src="/production/javascript/rps_main.js?v=2"></script>

    <script type="text/javascript" src="/production/javascript/autocomplete/rsAutoCompleteNew.js"></script>
    <script type="text/javascript" src="/production/javascript/rsHTMLObject.js?v=6"></script>
    <script type="text/javascript" src="/production/javascript/rsPeriodicUpdateObject.js?v=6"></script>
    <script type="text/javascript" src="/production/javascript/rsUpdateWhenStateChanges.js?v=6"></script>

    <script type="text/javascript">
            // Declare this in the 'outer' scope.  The job pane will populate this when it loads.
            //
            var gSubclassSelected;
            var gJobClasses = [];
            function updateSubclassMenu(source) {
                var selectedOption = source.options[source.selectedIndex];
                var subclasses = gJobClasses[selectedOption.value];
                var subMenu = document.getElementById("jobsubclass");
                subMenu.options.length = 0;
                subMenu.add(new Option('ALL', 0, 1, 1), null);
                if (subclasses)
                {
                    for (var i = 0; i &lt; subclasses.length; i++)
                    {
                        var name = subclasses[i];
                        subMenu.add(new Option(name, name, 0, 0), null);
                    }
                }
            }

            function populateClassMenus() {
                var classMenu = document.getElementById("jobclass");
                updateSubclassMenu(classMenu);

                if (gSubclassSelected)
                {
                    var subMenu = document.getElementById("jobsubclass");
                    for (var i = 0; i &lt; subMenu.length; i++)
                    {
                        var theOption = subMenu.options[i];
                        if (theOption.value == gSubclassSelected)
                        {
                            subMenu.selectedIndex = i;
                        }
                    }
                }
            }
    </script>

    <xsl:call-template name="bodyTitle" >
        <xsl:with-param name="title">Job Queue</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="filterSection" />
    <xsl:call-template name="displayHostStatus" />
    <xsl:call-template name="displayQueue" />
    <xsl:call-template name="setupClassMenus" />
</xsl:template>

<xsl:template name="displayHostStatus" >
    <xsl:variable name="host">
        <xsl:choose>
            <xsl:when test="Params/host"><xsl:value-of select="Params/host" /></xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <div class="section">
        <div class="RSUpdateWhenStateChanges" id="hostStatus">
            <img src="/production/images/loading.gif" width="100" height="100" alt="Loading..." />
        </div>

        <script type="text/javascript">
            var myStatusPane = new RSUpdateWhenStateChanges("hostStatus",
            {
                "state_url" : "/support/job_queue?c=hoststatustoken",
                "url"       : "/support/job_queue?c=gethoststatus&amp;host=<xsl:value-of select="$host" />",
                "interval"  : "30000"
            }
            );

        </script>

    </div>
</xsl:template>

<xsl:template name="displayQueue" >
    <xsl:variable name="age">
        <xsl:choose>
            <xsl:when test="Params/age"><xsl:value-of select="Params/age" /></xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="jobclass">
        <xsl:choose>
            <xsl:when test="Params/jobclass"><xsl:value-of select="Params/jobclass" /></xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="jobsubclass">
        <xsl:choose>
            <xsl:when test="Params/jobsubclass"><xsl:value-of select="Params/jobsubclass" /></xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="client">
        <xsl:choose>
            <xsl:when test="Params/client"><xsl:value-of select="Params/client" /></xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="host">
        <xsl:choose>
            <xsl:when test="Params/host"><xsl:value-of select="Params/host" /></xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="hideChildren">
        <xsl:choose>
            <xsl:when test="Params/hideChildren"><xsl:value-of select="Params/hideChildren" /></xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <div class="section">
        <div class="RSUpdateWhenStateChanges" id="jobWaitingQueue">
            <img src="/production/images/loading.gif" width="100" height="100" alt="Loading..." />
        </div>

        <script type="text/javascript">
            var myWaitingQueuePane = new RSUpdateWhenStateChanges("jobWaitingQueue",
            {
                "state_url" : "/support/job_queue?c=queuetoken",
                "url"       : "/support/job_queue?c=getwaitingqueue&amp;age=<xsl:value-of select="$age" />&amp;client=<xsl:value-of select="$client" />&amp;jobclass=<xsl:value-of select="$jobclass" />&amp;jobsubclass=<xsl:value-of select="$jobsubclass" />&amp;host=<xsl:value-of select="$host" />&amp;hideChildren=<xsl:value-of select="$hideChildren" />",
                "interval"  : "10000"
            }
            );
        </script>

    </div>
    <div class="section">
        <div class="RSUpdateWhenStateChanges" id="jobRunningQueue">
            <img src="/production/images/loading.gif" width="100" height="100" alt="Loading..." />
        </div>

        <script type="text/javascript">
            var myRunningQueuePane = new RSUpdateWhenStateChanges("jobRunningQueue",
            {
                "state_url" : "/support/job_queue?c=queuetoken",
                "url"       : "/support/job_queue?c=getrunningqueue&amp;age=<xsl:value-of select="$age" />&amp;client=<xsl:value-of select="$client" />&amp;jobclass=<xsl:value-of select="$jobclass" />&amp;jobsubclass=<xsl:value-of select="$jobsubclass" />&amp;host=<xsl:value-of select="$host" />&amp;hideChildren=<xsl:value-of select="$hideChildren" />",
                "interval"  : "10000"
            }
            );
        </script>

    </div>
    <div class="section">
        <div class="RSUpdateWhenStateChanges" id="jobFinishedQueue">
            <img src="/production/images/loading.gif" width="100" height="100" alt="Loading..." />
        </div>

        <script type="text/javascript">
            var myFinishedQueuePane = new RSUpdateWhenStateChanges("jobFinishedQueue",
            {
                "state_url" : "/support/job_queue?c=queuetoken",
                "url"       : "/support/job_queue?c=getfinishedqueue&amp;age=<xsl:value-of select="$age" />&amp;client=<xsl:value-of select="$client" />&amp;jobclass=<xsl:value-of select="$jobclass" />&amp;jobsubclass=<xsl:value-of select="$jobsubclass" />&amp;host=<xsl:value-of select="$host" />&amp;hideChildren=<xsl:value-of select="$hideChildren" />",
                "interval"  : "10000"
            }
            );
        </script>

    </div>
</xsl:template>
	<xsl:template name="setupClassMenus">
        <script type="text/javascript">
            // Parse the income JobClass data, and construct some 'associative arrays'
            // we can use to dynamically populate the JobSubClass menu.
            gJobClasses = [];
            var tmpRef;
            <xsl:for-each select="/Root/Classes">
                <xsl:sort select="Class/Name" />
                tmpRef = [];
                <xsl:for-each select="Class/Subclass">
                    tmpRef.push("<xsl:value-of select="." />");
                </xsl:for-each>
                gJobClasses["<xsl:value-of select="Class/Name" />"] = tmpRef;
            </xsl:for-each>

            gSubclassSelected = "<xsl:value-of select="/Root/Params/jobsubclass" />";
            populateClassMenus();

        </script>
	</xsl:template>

	<xsl:template name="filterSection">
    <div class="section">
		<fieldset>
        <legend>Filters</legend>
		<form name="queueFilter" id="queueFilter" action="/support/job_queue" method="get">
            <table>
                <tr>
                    <th>Host</th>
                    <th>Client</th>
                    <th>Class</th>
                    <th>Subclass</th>
                    <th>Hide Children</th>
                    <th>Completed in last...</th>
                </tr>
                <tr>
                    <td>
			            <select name="host" id="host">
				            <option value="0">
					            <xsl:if test="Params/host = 0">
						            <xsl:attribute name="selected">selected</xsl:attribute>
					            </xsl:if>
                                ALL
                            </option>
                            <xsl:for-each select="/Root/Hosts">
                                <xsl:sort select="." />
                                <xsl:if test="normalize-space(.)">
                                    <xsl:variable name="hostName" select="." />
                                    <option value="{$hostName}">
                                        <xsl:if test="/Root/Params/host= $hostName">
                                            <xsl:attribute name="selected">selected</xsl:attribute>
                                        </xsl:if>
                                        <xsl:value-of select="." />
                                    </option>
                                </xsl:if>
                            </xsl:for-each>
                        </select>
                    </td>
                    <td>
			            <select name="client" id="client">
				            <option value="0">
					            <xsl:if test="/Root/Params/client = 0">
						            <xsl:attribute name="selected">selected</xsl:attribute>
					            </xsl:if>
                                ALL
                            </option>
						    <xsl:for-each select="/Root/Clients/Client">
							    <xsl:sort select="ClientName" />
		                        <xsl:variable name="clientID" select="ClientID" />
                                <option value="{$clientID}">
					                <xsl:if test="/Root/Params/client = $clientID">
						                <xsl:attribute name="selected">selected</xsl:attribute>
					                </xsl:if>
                                    <xsl:value-of select="ClientName" />
                                </option>
						    </xsl:for-each>
                        </select>
                    </td>
                    <td>
			            <select name="jobclass" id="jobclass" onChange="updateSubclassMenu(this)">
				            <option value="0">
					            <xsl:if test="/Root/Params/jobclass = 0">
						            <xsl:attribute name="selected">selected</xsl:attribute>
					            </xsl:if>
                                ALL
                            </option>
						    <xsl:for-each select="/Root/Classes">
                                <xsl:sort select="Class/Name" />
		                        <xsl:variable name="className" select="Class/Name" />
                                <option value="{$className}">
					                <xsl:if test="/Root/Params/jobclass= $className">
						                <xsl:attribute name="selected">selected</xsl:attribute>
					                </xsl:if>
                                    <xsl:value-of select="Class/Name" />
                                </option>
						    </xsl:for-each>
                        </select>
                    </td>
                    <td>
			            <select name="jobsubclass" id="jobsubclass">
				            <option value="0">ALL</option>
                        </select>
                    </td>
                    <td>
			            <select name="hideChildren" id="hideChildren">
				            <option value="0">No</option>
				            <option value="1">
					            <xsl:if test="/Root/Params/hideChildren &gt; 0">
						            <xsl:attribute name="selected">selected</xsl:attribute>
								</xsl:if>
								Yes
							</option>
                        </select>
                    </td>
                    <td>
			        <select name="age" id="age">
				        <option value="24">
					        <xsl:if test="Params/age = 24">
						        <xsl:attribute name="selected">selected</xsl:attribute>
					        </xsl:if>
					        Day
				        </option>
				        <option value="168">
					        <xsl:if test="Params/age = 168">
						        <xsl:attribute name="selected">selected</xsl:attribute>
					        </xsl:if>
					        Week
				        </option>
				        <option value="720">
					        <xsl:if test="Params/age = 720">
						        <xsl:attribute name="selected">selected</xsl:attribute>
					        </xsl:if>
					        Month
				        </option>
				        <option value="8760">
					        <xsl:if test="Params/age = 8760">
						        <xsl:attribute name="selected">selected</xsl:attribute>
					        </xsl:if>
					        Year
				        </option>
			        </select>
                    </td>
			        <td><input type="submit" value="Update" /></td>
                    <td><input type="button" value="Reset Filters" onClick="location.href='/support/job_queue?host=0&amp;client=0&amp;jobclass=0&amp;jobsubclass=0&amp;hideChildren=0&amp;age=24'"/></td>
                </tr>
            </table>
		</form>
        </fieldset>
		<br />
    </div>
	</xsl:template>

</xsl:stylesheet>
