<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" version="1.0">


<xsl:template name="collectionList">
  <xsl:param name="addButtonText" />
  <xsl:param name="addCommand" />
  <xsl:param name="editCommand" />
  <xsl:param name="resetCommand" />
  <xsl:param name="deleteCommand" />
  <xsl:param name="sortFields" />
  <xsl:param name="keyField" />
  <xsl:param name="columns" />

  <div id="list_result">
    <xsl:choose>

      <xsl:when test="count(exsl:node-set($columns)/Column) &lt; 1">
        <xsl:call-template name="collectionListMissingColumns" />
      </xsl:when>

      <xsl:when test="string-length($keyField) &lt; 1">
        <xsl:call-template name="collectionListMissingKeyField" />
      </xsl:when>

      <xsl:when test="Collection/List/Item">
        <xsl:call-template name="collectionListBody" >
          <xsl:with-param name="addButtonText" select="$addButtonText" />
          <xsl:with-param name="addCommand" select="$addCommand" />
          <xsl:with-param name="editCommand" select="$editCommand" />
          <xsl:with-param name="resetCommand" select="$resetCommand" />
          <xsl:with-param name="deleteCommand" select="$deleteCommand" />
          <xsl:with-param name="keyField" select="$keyField" />
          <xsl:with-param name="sortFields" select="$sortFields" />
          <xsl:with-param name="columns" select="$columns" />
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <xsl:call-template name="collectionListEmpty" />
      </xsl:otherwise>

    </xsl:choose>
  </div>

</xsl:template>

<xsl:template name="collectionListMissingKeyField" >
  No keyField specified
</xsl:template>

<xsl:template name="collectionListMissingColumns" >
  No column nodes specified
</xsl:template>

<xsl:template name="collectionListEmpty" >
  <p>No matching records found</p>
</xsl:template>

<xsl:template name="collectionListBody" >
  <xsl:param name="addButtonText" />
  <xsl:param name="addCommand" />
  <xsl:param name="editCommand" />
  <xsl:param name="resetCommand" />
  <xsl:param name="deleteCommand" />
  <xsl:param name="sortFields" />
  <xsl:param name="keyField" />
  <xsl:param name="columns" />

  <xsl:call-template name="collectionListPagination" />

  <table class="dataset">
    <tbody>
      <xsl:call-template name="collectionListHeaderRow" >
        <xsl:with-param name="editCommand" select="$editCommand" />
        <xsl:with-param name="resetCommand" select="$resetCommand" />
        <xsl:with-param name="deleteCommand" select="$deleteCommand" />
        <xsl:with-param name="columns" select="$columns" />
      </xsl:call-template>

      <xsl:for-each select="Collection/List/Item" >
        <xsl:for-each select="exsl:node-set($sortFields)/Sort" >
          <xsl:sort select="ServiceID" />
        </xsl:for-each>
        <xsl:call-template name="collectionListData" >
          <xsl:with-param name="editCommand" select="$editCommand" />
          <xsl:with-param name="resetCommand" select="$resetCommand" />
          <xsl:with-param name="deleteCommand" select="$deleteCommand" />
          <xsl:with-param name="keyField" select="$keyField" />
          <xsl:with-param name="columns" select="$columns" />
          <xsl:with-param name="row" select="*" />
        </xsl:call-template>
      </xsl:for-each>
    </tbody>
  </table>

  <xsl:call-template name="collectionListPagination" />

  <xsl:if test="$addCommand">
    <xsl:call-template name="addButton" >
      <xsl:with-param name="addCommand"><xsl:value-of select="$addCommand" /></xsl:with-param>
      <xsl:with-param name="buttonText"><xsl:value-of select="$addButtonText" /></xsl:with-param>
    </xsl:call-template>
  </xsl:if>

</xsl:template>

<xsl:template name="collectionListHeaderRow" >
  <xsl:param name="editCommand" />
  <xsl:param name="resetCommand" />
  <xsl:param name="deleteCommand" />
  <xsl:param name="columns" />

  <tr class="header">
    <xsl:for-each select="exsl:node-set($columns)/Column" >
      <th>
        <xsl:if test="@class" >
          <xsl:attribute name="class"><xsl:value-of select="@class" /></xsl:attribute>
        </xsl:if>
        <xsl:if test="@align" >
          <xsl:attribute name="align"><xsl:value-of select="@align" /></xsl:attribute>
        </xsl:if>
        <xsl:value-of select="." />
      </th>
    </xsl:for-each>

    <xsl:if test="$editCommand"><th></th></xsl:if>
    <xsl:if test="$resetCommand"><th></th></xsl:if>
    <xsl:if test="$deleteCommand"><th></th></xsl:if>
  </tr>
</xsl:template>

<xsl:template name="collectionListData" >
  <xsl:param name="editCommand" />
  <xsl:param name="resetCommand" />
  <xsl:param name="deleteCommand" />
  <xsl:param name="keyField" />
  <xsl:param name="columns" />
  <xsl:param name="row" />

  <tr class="row{position() mod 2}">

    <xsl:for-each select="exsl:node-set($columns)/Column" >
      <td>
        <xsl:if test="@class" >
          <xsl:attribute name="class"><xsl:value-of select="@class" /></xsl:attribute>
        </xsl:if>
        <xsl:if test="@align" >
          <xsl:attribute name="align"><xsl:value-of select="@align" /></xsl:attribute>
        </xsl:if>
        <xsl:call-template name="collectionListNode" >
          <xsl:with-param name="nodename" select="@field" />
          <xsl:with-param name="link">
            <xsl:if test="normalize-space(@link)">
              <xsl:call-template name="prependHostname" >
                <xsl:with-param name="link" select="@link" />
                <xsl:with-param name="row" select="$row" />
              </xsl:call-template>
            </xsl:if>
          </xsl:with-param>
          <xsl:with-param name="row" select="$row" />
        </xsl:call-template>
      </td>
    </xsl:for-each>

    <xsl:variable name="id">
      <xsl:call-template name="collectionListNodeValue" >
        <xsl:with-param name="nodename" select="$keyField" />
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:variable>


    <xsl:if test="$resetCommand">
      <td>
        <xsl:if test="MFAType != 0">
          <a href="javascript:confirmSupportResetMFA('{$id}','{/Root/Params/query}','{/Root/Params/page}')">Reset MFA</a>
        </xsl:if>
      </td>
    </xsl:if>
    <xsl:if test="$editCommand">
      <td><a href="{$moduleURL}?c={$editCommand}&amp;{$keyField}={$id}">Edit</a></td>
    </xsl:if>
    <xsl:if test="$deleteCommand">
      <td><a href="{$moduleURL}?c={$deleteCommand}&amp;id=ID">Delete</a></td>
    </xsl:if>

  </tr>

</xsl:template>

<xsl:template name="collectionListNode" >
  <xsl:param name="nodename" />
  <xsl:param name="link" />
  <xsl:param name="row" />
  <xsl:choose>
    <xsl:when test="$nodename = 'client'" >
      <xsl:call-template name="collectionListNodeClient" >
        <xsl:with-param name="nodename" select="'client_id'" />
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$nodename = 'album_id'" >
      <xsl:call-template name="collectionListNodeAlbumID" >
        <xsl:with-param name="nodename" select="$nodename" />
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$nodename = 'MFAType'" >
      <xsl:call-template name="collectionListNodeMFAType" >
        <xsl:with-param name="nodename" select="$nodename" />
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$nodename = 'UserName'" >
      <xsl:call-template name="collectionListNodeUserName" >
        <xsl:with-param name="nodename" select="$nodename" />
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="$nodename = 'status'" >
      <xsl:call-template name="collectionListNodeRunStatus" >
        <xsl:with-param name="nodename" select="$nodename" />
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="collectionListNodeValue" >
        <xsl:with-param name="nodename" select="$nodename" />
        <xsl:with-param name="link" select="$link" />
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="collectionListNodeValue" >
  <xsl:param name="nodename" />
  <xsl:param name="link" />
  <xsl:param name="row" />

  <xsl:variable name="payorID" ><xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = 'payor_id'">&amp;PayorID=<xsl:value-of select="." /></xsl:if>
  </xsl:for-each></xsl:variable>

  <xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = $nodename" >
      <xsl:choose>
        <xsl:when test="normalize-space($link)">
          <a>
            <xsl:attribute name="href"><xsl:value-of select="$link" /><xsl:value-of select="." /><xsl:value-of select="$payorID" /></xsl:attribute>
            <xsl:value-of select="." />
          </a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="." />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template name="prependHostname">
  <xsl:param name="link" />
  <xsl:param name="row" />
  <xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = 'client_id'" >
      https://<xsl:call-template name="clientCleanFromID" ><xsl:with-param name="clientID" select="." /></xsl:call-template>.royaltyshare.com<xsl:value-of select="$link" />
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template name="collectionListNodeRunStatus" >
  <xsl:param name="nodename" />
  <xsl:param name="row" />

  <xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = 'status'" >
      <xsl:choose>
        <xsl:when test=". = 0">Running</xsl:when>
        <xsl:when test=". = 1">Complete</xsl:when>
        <xsl:when test=". = 2">Committed</xsl:when>
        <xsl:when test=". = 3">Aborted</xsl:when>
        <xsl:when test=". = 4">Error</xsl:when>
        <xsl:when test=". = 5">Closed</xsl:when>
        <xsl:when test=". = 6">Waiting to Run</xsl:when>
        <xsl:when test=". = 7">Committing</xsl:when>
        <xsl:when test=". = 8">Deleting</xsl:when>
        <xsl:when test=". = 9">Invalid</xsl:when>
        <xsl:otherwise>FF</xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:for-each>

</xsl:template>


<xsl:template name="collectionListNodeMFAType" >
  <xsl:param name="nodename" />
  <xsl:param name="row" />

  <xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = 'MFAType'" >
      <xsl:choose>
        <xsl:when test=". = 0">None</xsl:when>
        <xsl:when test=". = 1">Email</xsl:when>
        <xsl:when test=". = 2">Authenticator</xsl:when>
        <xsl:when test=". = 3">SMS</xsl:when>
        <xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:for-each>

</xsl:template>

<xsl:template name="collectionListNodeUserName" >
  <xsl:param name="nodename" />
  <xsl:param name="row" />

  <xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = 'FirstName'" >
        <xsl:value-of select="." /><xsl:text> </xsl:text>
    </xsl:if>
    <xsl:if test="local-name() = 'LastName'" >
        <xsl:value-of select="." />
    </xsl:if>
  </xsl:for-each>

</xsl:template>

<xsl:template name="collectionListNodeAlbumID" >
  <xsl:param name="nodename" />
  <xsl:param name="row" />

    <xsl:variable name="albumID">
      <xsl:call-template name="collectionListNodeValue" >
        <xsl:with-param name="nodename">album_id</xsl:with-param>
        <xsl:with-param name="row" select="$row" />
      </xsl:call-template>
    </xsl:variable>

  <xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = 'client_id'" >
      <xsl:variable name="cleanName" ><xsl:call-template name="clientCleanFromID" ><xsl:with-param name="clientID" select="." /></xsl:call-template></xsl:variable>
      <a href="https://{$cleanName}.royaltyshare.com/rps/catalog?c=show&amp;AlbumID={$albumID}" target="_NEW"><xsl:value-of select="$albumID" /></a>
    </xsl:if>
  </xsl:for-each>

</xsl:template>

<xsl:template name="collectionListNodeClient" >
  <xsl:param name="nodename" />
  <xsl:param name="row" />
  <xsl:for-each select="exsl:node-set($row)">
    <xsl:if test="local-name() = $nodename" >
      <xsl:call-template name="clientNameFromID" ><xsl:with-param name="clientID" select="." /></xsl:call-template>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template name="collectionListPagination" >
  <xsl:if test="Collection/IsPaginated &gt; 0">
    <xsl:variable name="url"><xsl:value-of select="$moduleURL" />?c=<xsl:value-of select="/Root/Cmd" />&amp;client=<xsl:value-of select="/Root/Params/client" />&amp;limit=<xsl:value-of select="/Root/Params/limit" />&amp;submit=1</xsl:variable>
    <xsl:variable name="prev" select="Collection/PageIndex - 1" />
    <xsl:variable name="next" select="Collection/PageIndex + 1" />

    <xsl:variable name="extraArgs">
      <xsl:if test="/Root/Params/query">&amp;query=<xsl:value-of select="/Root/Params/query" /></xsl:if>
    </xsl:variable>

    <table class="old_pagination">
      <tr>
        <td align="left" width="20%">
          <xsl:if test="Collection/PageIndex &gt; 1">
            <a href="{$url}{$extraArgs}&amp;page={$prev}">prev</a>
          </xsl:if>
        </td>
        <td align="center">
          <xsl:value-of select="Collection/PageIndex" /> of <xsl:value-of select="Collection/PageCount" />
        </td>
        <td align="right" width="20%">
          <xsl:if test="Collection/PageIndex &lt; Collection/PageCount">
            <a href="{$url}{$extraArgs}&amp;page={$next}">next</a>
          </xsl:if>
        </td>
      </tr>
    </table>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>
