<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<xsl:template match="Root">

		<xsl:call-template name="jobsInQueueSection" />
		<xsl:call-template name="jobsRunningSection" />
		<xsl:call-template name="jobsFinishedSection" />
		<br />
	</xsl:template>

	<xsl:template name="jobsInQueueSection">
		<xsl:variable name="jobsInQueue" select="JobQueue/Jobs[StartDate = '0000-00-00 00:00:00']" />
		<fieldset>
			<legend>In Queue</legend>
			<xsl:choose>
				<xsl:when test="$jobsInQueue">
					<table class="textChart">
						<tr>
							<th>Priority</th>
							<th>Job ID</th>
							<th>Parent ID</th>
							<th>Queued</th>
							<th>Min Start Date</th>
							<th>Host</th>
							<th>Client</th>
							<th>Class</th>
							<th>Subclass</th>
                            <th>On Hold</th>
						</tr>
						<xsl:for-each select="$jobsInQueue">
							<xsl:sort data-type="number" select="Priority" />
							<xsl:call-template name="jobInQueueRow" />
						</xsl:for-each>
					</table>
				</xsl:when>
				<xsl:otherwise>
					<p class="emptyMessage">No matching jobs are currently in the queue.</p>
					<br />
				</xsl:otherwise>
			</xsl:choose>
		</fieldset>
		<br />
	</xsl:template>

	<xsl:template name="jobInQueueRow">
		<tr class="row{position() mod 2}">
			<td><xsl:value-of select="Priority" /></td>
			<td>
              <xsl:call-template name="jobDetailLink">
                <xsl:with-param name="job_id" select = "JobID" />
			  </xsl:call-template>
            </td>
			<td>
              <xsl:if test="ParentJobID &gt; 0">
                <xsl:call-template name="jobDetailLink">
				  <xsl:with-param name="job_id" select = "ParentJobID" />
			    </xsl:call-template>
			  </xsl:if>
            </td>
			<td><xsl:value-of select="QueueDate" /></td>
			<td><xsl:value-of select="MinStartDate" /></td>
			<td><xsl:value-of select="HostName" /></td>
            <td>
				<xsl:call-template name="clientIDToName">
					<xsl:with-param name="clientID" select="ClientID" />
				</xsl:call-template>
            </td>
			<td><xsl:value-of select="Class" /></td>
			<td><xsl:value-of select="Subclass" /></td>
            <xsl:call-template name="onHoldCheckBox" />
		</tr>
		<!--
        <tr class="row{position() mod 2}">
            <td>Command:</td>
            <td colspan='8'><xsl:value-of select="CommandLine" /></td>
        </tr>
        -->
	</xsl:template>

    <xsl:template name="onHoldCheckBox">
    <xsl:variable name="url">/support/job_queue?c=togglehold&amp;job_id=<xsl:value-of select="JobID" /></xsl:variable>
    <xsl:choose>
		<xsl:when test="ParentPaused = 1">
            <td class="error">PAUSED</td>
		</xsl:when>
		<xsl:otherwise>
            <td>
    		    <input type="checkbox" name="onHold" id="onHold">
                    <xsl:attribute name="onclick">ajaxRequest( '<xsl:value-of select="$url" />' );</xsl:attribute>
                    <xsl:if test="OnHold = 1">
                        <xsl:attribute name="checked">checked</xsl:attribute>
                    </xsl:if>
                </input>
            </td>
		</xsl:otherwise>
	</xsl:choose>
<!--		    <xsl:if test="OnHold = 1"><td class="error">ON HOLD</td></xsl:if>
		    <xsl:if test="OnHold = 0"><td> </td></xsl:if> -->
    </xsl:template>

	<xsl:template name="pauseCheckBox">
    <xsl:variable name="pauseurl">/support/job_queue?c=togglepaused&amp;job_id=<xsl:value-of select="JobID" /></xsl:variable>
    <td>
        <xsl:if test="ChildCount &gt; 0" >
		  <input type="checkbox" name="paused" id="paused">
            <xsl:attribute name="onclick">ajaxRequest( '<xsl:value-of select="$pauseurl" />' );</xsl:attribute>
              <xsl:if test="Paused = 1">
                  <xsl:attribute name="checked">checked</xsl:attribute>
              </xsl:if>
          </input>
        </xsl:if>
    </td>
    </xsl:template>

	<xsl:template name="jobsRunningSection">
		<xsl:variable name="jobsRunning" select="JobQueue/Jobs[StartDate != '0000-00-00 00:00:00' and EndDate = '0000-00-00 00:00:00']" />
		<fieldset>
			<legend>Running</legend>
			<xsl:choose>
				<xsl:when test="$jobsRunning">
					<table class="textChart">
						<tr>
							<th>Job ID</th>
							<th>Host</th>
							<th>Client</th>
							<th>Class</th>
							<th>Subclass</th>
							<th>Started</th>
							<th>Last Heartbeat</th>
							<th>Paused</th>
						</tr>
						<xsl:for-each select="$jobsRunning">
							<xsl:call-template name="jobRunningRow" />
						</xsl:for-each>
					</table>
				</xsl:when>
				<xsl:otherwise>
					<p class="emptyMessage">No matching jobs are currently running.</p>
					<br />
				</xsl:otherwise>
			</xsl:choose>
		</fieldset>
		<br />
	</xsl:template>

	<xsl:template name="jobRunningRow">
		<tr class="row{position() mod 2}">
			<td>
              <xsl:call-template name="jobDetailLink">
                <xsl:with-param name="job_id" select = "JobID" />
			  </xsl:call-template>
            </td>
			<td><xsl:value-of select="RunHostName" /></td>
            <td>
				<xsl:call-template name="clientIDToName">
					<xsl:with-param name="clientID" select="ClientID" />
				</xsl:call-template>
            </td>
			<td><xsl:value-of select="Class" /></td>
			<td><xsl:value-of select="Subclass" /></td>
			<td><xsl:value-of select="StartDate" /></td>
			<td><xsl:value-of select="Heartbeat" /></td>
            <xsl:call-template name="pauseCheckBox" />
		</tr>
	</xsl:template>

	<xsl:template name="jobsFinishedSection">
		<xsl:variable name="jobsFinished" select="JobQueue/Jobs[EndDate != '0000-00-00 00:00:00']" />
		<fieldset>
			<legend>Completed</legend>
			<xsl:choose>
				<xsl:when test="$jobsFinished">
					<table class="textChart">
						<tr>
							<th>Job ID</th>
							<th>End Date</th>
							<th>Host</th>
							<th>Client</th>
							<th>Class</th>
							<th>Subclass</th>
							<th>Exit Code</th>
						</tr>
						<xsl:for-each select="$jobsFinished">
							<xsl:sort select="EndDate" order="descending" />
							<xsl:call-template name="jobFinishedRow" />
						</xsl:for-each>
					</table>
				</xsl:when>
				<xsl:otherwise>
					<p class="emptyMessage">No jobs have been completed in the selected time frame.</p>
					<br />
				</xsl:otherwise>
			</xsl:choose>
		</fieldset>
		<br />
	</xsl:template>

	<xsl:template name="jobFinishedRow">
		<tr class="row{position() mod 2}">
			<td>
              <xsl:call-template name="jobDetailLink">
                <xsl:with-param name="job_id" select = "JobID" />
			  </xsl:call-template>
            </td>
			<td><xsl:value-of select="EndDate" /></td>
			<td><xsl:value-of select="RunHostName" /></td>
            <td>
				<xsl:call-template name="clientIDToName">
					<xsl:with-param name="clientID" select="ClientID" />
				</xsl:call-template>
            </td>
			<td><xsl:value-of select="Class" /></td>
			<td><xsl:value-of select="Subclass" /></td>
			<td>
				<xsl:choose>
					<xsl:when test="normalize-space(ExitCode)">
						<xsl:value-of select="ExitCode" />
					</xsl:when>
					<xsl:otherwise>Aborted</xsl:otherwise>
				</xsl:choose>
			</td>
		</tr>
	</xsl:template>

	<xsl:template name="clientIDToName">
		<xsl:param name="clientID" />
        <xsl:value-of select="/Root/Clients/Client[ClientID = $clientID]/ClientName" />
	</xsl:template>

  <xsl:template name="jobDetailLink">
    <xsl:param name="job_id" />
    <a href="/support/job_queue?c=show_job&amp;JobID={$job_id}" >
      <xsl:value-of select="$job_id" />
    </a>
  </xsl:template>

</xsl:stylesheet>
