<!--                                                                  -->
<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 Process Detail</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="bodyMessage" />
    <xsl:call-template name="displayForm" />
  </xsl:template>

  <xsl:template name="displayForm" >
    <div name="body_form">
      <xsl:choose>
        <xsl:when test="Form/DistributionProcessID &gt; 0">
          <xsl:call-template name="displayDetail" />
          <xsl:call-template name="displayJobs" />
          <xsl:call-template name="displayContent" />
          <xsl:call-template name="displayActions" />
        </xsl:when>
        <xsl:when test="/Root/Params/action = 'new'">
          <xsl:call-template name="displayDetail" />
        </xsl:when>
        <xsl:otherwise>
          Distribution Process Not Found
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>

  <xsl:template name="displayDetail" >
    <form method="post" action="{$moduleURL}" >
      <input type="hidden" name="c"><xsl:attribute name="value"><xsl:value-of select="/Root/Cmd" /></xsl:attribute></input>
      <input type="hidden" name="DistributionProcessID"><xsl:attribute name="value"><xsl:value-of select="Form/DistributionProcessID" /></xsl:attribute></input>
      <input type="hidden" name="action"><xsl:attribute name="value"><xsl:value-of select="Params/action" /></xsl:attribute></input>
    <fieldset>
      <legend>Process</legend>
      <table>
        <tr>
          <xsl:call-template name="textField" >
            <xsl:with-param name="label">Distribution Process ID</xsl:with-param>
            <xsl:with-param name="value" select = "Form/DistributionProcessID" />
          </xsl:call-template>
        </tr>
        <tr>
          <xsl:call-template name="textField" >
            <xsl:with-param name="label">State</xsl:with-param>
            <xsl:with-param name="value" select = "Form/State" />
          </xsl:call-template>
        </tr>
      </table>
    </fieldset>
    </form>
  </xsl:template>

  <xsl:template name="displayJobs" >
    <fieldset>
      <legend>Jobs</legend>
      <table class="textChart">
        <tr>
          <th>Job ID</th>
          <th>End Date</th>
          <th>Host</th>
          <th>Class</th>
          <th>Subclass</th>
          <th>Exit Code</th>
        </tr>
        <xsl:for-each select="Form/Jobs/Job">
          <xsl:sort select="Class" />
          <xsl:sort select="EndDate" order="descending" />
          <xsl:call-template name="jobFinishedRow" />
        </xsl:for-each>
      </table>
    </fieldset>
  </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="HostName" /></td>
      <td><xsl:value-of select="Class" /></td>
      <td><xsl:value-of select="Subclass" /></td>
      <td><xsl:value-of select="ExitCode" /></td>
    </tr>
  </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:template name="displayContent" >
    <form name="requeue" action="/support/cm" method="POST">
      <input type="hidden" name="c" value="redeliver_job" />
      <input type="hidden" name="DistributionJobID" value="{/Root/Params/DistributionJobID}" />

    <fieldset>
      <legend>Content</legend>

      <input type="button" name="CheckAll" value="Check All" onClick="checkAll(document.requeue.AlbumID)" />
      <input type="button" name="UnCheckAll" value="Uncheck All" onClick="uncheckAll(document.requeue.AlbumID)" /><br/>

      <xsl:for-each select="Form/Contents/ProductDistribution">
        <xsl:sort select="UPC" data-type="number" />
        <input type="checkbox" id="AlbumID" name="AlbumID" value="{AlbumID}" />
        <a target="_new" href="/rps/catalog?c=show&amp;tab=distributionTab&amp;AlbumID={AlbumID}"><xsl:value-of select="UPC" /></a><br/>
      </xsl:for-each>

    <xsl:call-template name="submitButton" >
      <xsl:with-param name="label">Redeliver Selected Albums</xsl:with-param>
    </xsl:call-template>
    </fieldset>
    </form>
  </xsl:template>

  <xsl:template name="displayActions" >
    <fieldset>
      <legend>Actions</legend>
      <a href="javascript:ajaxRequest('/support/job_queue?c=requeue&amp;DistributionJobID={/Root/Params/DistributionJobID}&amp;ClientID={/Root/User/ClientID}'); alert( 'Job Requeued' )" >
          Requeue Last Job
      </a> -
      <a href="/support/job_queue?c=show_process_detail&amp;MarkFixed=1&amp;DistributionJobID={/Root/Params/DistributionJobID}">Mark As Fixed</a>
    </fieldset>
  </xsl:template>


</xsl:stylesheet>
