<!--                                                                  -->
<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" >
    <xsl:call-template name="bodyTitle" >
      <xsl:with-param name="title">Distribution Processes</xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="displayProcessFilters" />
    <xsl:call-template name="displayPendingProcesses" />
    <xsl:call-template name="displayRunningProcesses" />
    <xsl:call-template name="displayCompleteProcesses" />
  </xsl:template>

  <xsl:template name="displayPendingProcesses" >
    <xsl:call-template name="displayProcesses" >
      <xsl:with-param name="jobCriteria" select="Form/DistributionProcess[State = 'pending' or State = 'packaged']" />
      <xsl:with-param name="jobLabel">Pending</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="displayRunningProcesses" >
    <xsl:call-template name="displayProcesses" >
      <xsl:with-param name="jobCriteria" select="Form/DistributionProcess[State = 'running']" />
      <xsl:with-param name="jobLabel">Running</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="displayCompleteProcesses" >
    <xsl:call-template name="displayProcesses" >
      <xsl:with-param name="jobCriteria" select="Form/DistributionProcess[State = 'done' or State = 'failed' or State = 'fixed']" />
      <xsl:with-param name="jobLabel">Complete</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="displayProcesses" >
    <xsl:param name="jobCriteria" />
    <xsl:param name="jobLabel" />

    <fieldset>
      <legend><xsl:value-of select="$jobLabel" /></legend>

      <xsl:choose>
        <xsl:when test="$jobCriteria">
          <table class="textChart">
          <xsl:call-template name="distributionJobHeader" >
            <xsl:with-param name="showRequeue">
              <xsl:choose>
                <xsl:when test="$jobLabel = 'Complete'">1</xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
              </xsl:choose>
            </xsl:with-param>
          </xsl:call-template>

            <xsl:for-each select="$jobCriteria">
              <xsl:sort select="State" />
              <xsl:sort select="QueueDate" order="descending" />
              <xsl:call-template name="distributionJobRow" />
            </xsl:for-each>
          </table>
        </xsl:when>

        <xsl:otherwise>
          <p class="emptyMessage">No matching distribution processes are currently in the queue.</p>
          <br />
        </xsl:otherwise>
      </xsl:choose>
    </fieldset>
  </xsl:template>

  <xsl:template name="distributionJobHeader" >
    <xsl:param name="showRequeue" />
    <tr class="header">
      <th></th>
      <th>Distribution ID</th>
      <th>Client</th>
      <th>Service</th>
      <th>Status</th>
      <th>Queued</th>
      <th>Ended</th>
      <th>Job Count</th>
      <xsl:if test="$showRequeue &gt; 0" ><th></th></xsl:if>
    </tr>
  </xsl:template>

  <xsl:template name="distributionJobRow" >
  <xsl:variable name="url">https://<xsl:call-template name="clientCleanFromID" ><xsl:with-param name="clientID" select="ClientID" /></xsl:call-template>.royaltyshare.com</xsl:variable>
    <tr class="row{position() mod 2}">
      <td></td>
      <td>
        <a href="{$url}/support/job_queue?c=show_process_detail&amp;DistributionJobID={DistributionProcessID}" >
          <xsl:value-of select="DistributionProcessID" />
        </a>
      </td>
      <td>
        <xsl:call-template name="clientNameFromID" >
          <xsl:with-param name="clientID" select="ClientID" />
        </xsl:call-template>
      </td>
      <td><xsl:value-of select="Service" /></td>
      <td><xsl:value-of select="State" /></td>
      <td><xsl:value-of select="QueueDate" /></td>
      <td><xsl:value-of select="EndDate" /></td>
      <td><xsl:value-of select="JobCount" /></td>
      <xsl:if test="State = 'failed' or State = 'done' or State = 'fixed'" >
        <td>
          <!--
          <a href="javascript:ajaxRequest('/support/job_queue?c=requeue&amp;DistributionJobID={DistributionProcessID}&amp;ClientID={Jobs/Job/ClientID}', function() { alert( 'Ajax Request Complete' ) } )" >
            -->
          <a href="javascript:ajaxRequest('/support/job_queue?c=requeue&amp;DistributionJobID={DistributionProcessID}&amp;ClientID={ClientID}'); alert( 'Job Requeued' )" >
            Requeue
          </a>
        </td>
      </xsl:if>
    </tr>
  </xsl:template>

  <xsl:template name="displayProcessFilters" >
    <fieldset>
    <legend>Filters</legend>
    <form name="showProcesses" id="showProcesses" action="/support/job_queue" method="get">
      <input type="hidden" name="c" value="show_processes" />
      <table>
        <tr>
          <th>Client</th>
          <th>Service</th>
          <th>Completed in last...</th>
        </tr>

        <tr>

          <xsl:call-template name="listField" >
            <xsl:with-param name="name">client</xsl:with-param>
            <xsl:with-param name="value" select = "Params/client" />
            <xsl:with-param name="empty_label">All</xsl:with-param>
            <xsl:with-param name="options" >
              <xsl:for-each select="Clients/Client" >
                <option name="{ClientID}"><xsl:value-of select="ClientName" /></option>
              </xsl:for-each>
            </xsl:with-param>
          </xsl:call-template>

          <xsl:call-template name="listField" >
            <xsl:with-param name="name">service</xsl:with-param>
            <xsl:with-param name="value" select = "Params/service" />
            <xsl:with-param name="empty_label">All</xsl:with-param>
            <xsl:with-param name="options" >
              <xsl:for-each select="Services/Service" >
                <option name="{ServiceNameClean}"><xsl:value-of select="ServiceName" /></option>
              </xsl:for-each>
            </xsl:with-param>
          </xsl:call-template>

          <xsl:call-template name="listField" >
            <xsl:with-param name="name">age</xsl:with-param>
            <xsl:with-param name="value" select = "Params/age" />
            <xsl:with-param name="empty_label">All</xsl:with-param>
            <xsl:with-param name="options" >
              <option name="24">Day</option>
              <option name="168">Week</option>
              <option name="720">Month</option>
              <option name="8760">Year</option>
            </xsl:with-param>
          </xsl:call-template>

          <td>
            <xsl:call-template name="submitButton" >
              <xsl:with-param name="name">submit</xsl:with-param>
              <xsl:with-param name="label">Update</xsl:with-param>
            </xsl:call-template>
          </td>
        </tr>
      </table>
    </form>
    </fieldset>
  </xsl:template>

</xsl:stylesheet>
