<!--  $Id$  -->
<!--                                                                  -->
<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 Client 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/ClientID &gt; 0">
          <xsl:call-template name="displayDetail" />
          <xsl:call-template name="displayServices" />
        </xsl:when>
        <xsl:otherwise>
          Client 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="ClientID"><xsl:attribute name="value"><xsl:value-of select="Form/ClientID" /></xsl:attribute></input>
      <input type="hidden" name="action"><xsl:attribute name="value"><xsl:value-of select="Params/action" /></xsl:attribute></input>
    <fieldset>
      <legend>Client</legend>
      <table>

        <tr>
          <xsl:call-template name="textField" >
            <xsl:with-param name="label">Client ID</xsl:with-param>
            <xsl:with-param name="value" select = "Form/ClientID" />
          </xsl:call-template>
        </tr>

        <tr>
          <xsl:call-template name="textField" >
            <xsl:with-param name="label">Name</xsl:with-param>
            <xsl:with-param name="value" select = "Form/ClientName" />
          </xsl:call-template>
        </tr>

      </table>
    </fieldset>
    </form>
  </xsl:template>

  <xsl:template name="displayServices" >
    <fieldset>
      <legend>Services</legend>
        <table class="dataset" >
          <tr class="header">
            <th>ID</th>
            <th>Service</th>
            <th>Status</th>
            <th>Service Account</th>
            <th></th>
          </tr>

          <xsl:for-each select="Form/DistributionServices/Service" >
          <xsl:sort select="Configured" data-type="number" order="descending" />
          <xsl:sort select="Status" />

          <tr class="row{position() mod 2}" >
            <xsl:call-template name="serviceRow" />
          </tr>
          </xsl:for-each>

        </table>

      <xsl:call-template name="backButton" >
        <xsl:with-param name="buttonText">Add Service Account</xsl:with-param>
        <xsl:with-param name="command">service_account_detail</xsl:with-param>
        <xsl:with-param name="args">
          <ClientID><xsl:value-of select="Form/ClientID" /></ClientID>
          <action>new</action>
        </xsl:with-param>
      </xsl:call-template>
    </fieldset>
  </xsl:template>

  <xsl:template name="serviceRow" >
    <td><xsl:value-of select="ServiceID" /></td>
    <td><xsl:value-of select="ServiceName" /></td>
    <td><xsl:value-of select="Status" /></td>
    <td><xsl:call-template name="serviceAccountSelect" /></td>
    <td><a href="javascript:void(0);" onClick="selectLink( '/support/cm?c=service_account_detail&amp;ServiceID={ServiceID}&amp;ClientID={/Root/Form/ClientID}&amp;ServiceAccountID=', 'serviceAccountID_{position()}' )">view/edit</a></td>
  </xsl:template>

  <xsl:template name="serviceAccountSelect" >
    <xsl:variable name="sURL" >/support/cm?c=assign_service_account&amp;ServiceID=<xsl:value-of select="ServiceID" />&amp;ClientID=<xsl:value-of select="/Root/Form/ClientID" /></xsl:variable>
    <xsl:variable name="sPosition" ><xsl:value-of select="position()" /></xsl:variable>

    <select name="serviceAccountID_{$sPosition}" id="serviceAccountID_{position()}" onChange="ajaxToggleSelect( '{$sURL}', this, function() {{}}, 'ServiceAccountID' )" >
      <option value=""></option>
      <xsl:for-each select="ServiceAccounts/ServiceAccount" >
        <option value="{ServiceAccountID}">
          <xsl:if test="ClientID &gt; 0" >
            <xsl:attribute name="selected">selected</xsl:attribute>
          </xsl:if>
          <xsl:value-of select="Description" />
        </option>
      </xsl:for-each>
    </select>
  </xsl:template>

  <xsl:template name="displayButtons" >
    <xsl:if test="/Root/Params/ClientID" >
      <xsl:call-template name="backButton" >
        <xsl:with-param name="buttonText">Return to Client</xsl:with-param>
        <xsl:with-param name="command" >client_detail</xsl:with-param>
        <xsl:with-param name="args" >
          <ClientID><xsl:value-of select="/Root/Params/ClientID" /></ClientID>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    <xsl:call-template name="submitButton" />
  </xsl:template>

</xsl:stylesheet>
