<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="sortIndex">-1</xsl:param>
    <xsl:param name="sortOrder">ascending</xsl:param>

    <xsl:decimal-format name="decimalformat" decimal-separator="," grouping-separator=" " digit="#" pattern-separator=";" NaN="NaN" minus-sign="-"/>
    
    <xsl:variable name="newSortOrder">
        <xsl:if test="$sortOrder='ascending'">descending</xsl:if>
        <xsl:if test="$sortOrder='descending'">ascending</xsl:if>
    </xsl:variable>
    <xsl:template match="page">
        <xsl:variable name="darkColor" select="'#DDDDEE'"/>
        <xsl:variable name="lightColor" select="'##EEEEFF'"/>
        <xsl:variable name="columns" select="columns"/>
        <xsl:variable name="navigation" select="./additional-data/navigation/@value"/>
        <xsl:variable name="elementId" select="./additional-data/element/@id"/>
        <xsl:variable name="etatId" select="./additional-data/etat/@id"/>
        <xsl:variable name="documentId" select="./additional-data/document/@id"/>
        <xsl:variable name="coll-code" select="./additional-data/collectivite/@id"/>
        <xsl:variable name="budg-code" select="./additional-data/budget/@id"/>
        <xsl:variable name="coll-lib" select="./additional-data/collectivite/@lib"/>
        <xsl:variable name="budg-lib" select="./additional-data/budget/@lib"/>
        <xsl:variable name="searchSessionId" select="./additional-data/searchSessionId/text()"/>
<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        <table>
            <thead>
                <tr>
                    <!--th class="First"-->
                    <xsl:for-each select="columns/column">
                        <xsl:variable name="triStyle">
                            <xsl:choose>
                                <xsl:when test="position() = $sortIndex and $sortOrder = 'ascending'">enteteListeTrieeAsc</xsl:when>
                                <xsl:when test="position() = $sortIndex and $sortOrder = 'descending'">enteteListeTrieeDesc</xsl:when>
                                <xsl:otherwise>enteteListeTriable</xsl:otherwise>
                            </xsl:choose>
                            
                        </xsl:variable>
                        <th>
                            <!--xsl:attribute name="class">enteteListePaire <xsl:value-of select="$triStyle"/></xsl:attribute-->
                            <xsl:if test="position() = 1"><xsl:attribute name="class">First</xsl:attribute></xsl:if>
                            <xsl:if test="position() = last()"><xsl:attribute name="class">Last</xsl:attribute></xsl:if>
                            <xsl:if test="not(@display = 'true')">
                                <xsl:attribute name="style">display: none;</xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="@libelle"/>
                            <xsl:if test="./@aggregate='true'">
                                <xsl:variable name="idCol" select = "./@id"/>
                                <img src="img/tm_math2.png" onmouseover="return displayMenu('{$idCol}','{./@datatype}','{$searchSessionId}');" onmouseout="return nd();" style="cursor:pointer;"/>
<!--                                <div style="display:none; position: absolute; top:0px; left:-7px; z-index:2000; border: #333333 solid 1px; background-color:#CCCCCC; overflow: visible; color: black;" id="menu-{$idCol}">
                                    <xsl:variable name="dt" select="./@datatype"/>
                                    <ul style="list-style-type: none;">
                                        <xsl:for-each select="/page/aggregates/operators[@datatype=$dt]/op">
                                            <xsl:element name="li">
                                                <xsl:attribute name="onclik">javascript:alert('<xsl:value-of select="$idCol"/> / <xsl:value-of select="./@key"/>');</xsl:attribute>
                                                <xsl:attribute name="style">cursor:pointer;</xsl:attribute>
                                                <xsl:value-of select="./@lib"/>
                                            </xsl:element>
                                        </xsl:for-each>
                                    </ul>
                                </div> -->
                            </xsl:if>
                            <div class="Sort">
                                <a title="Tri décroissant">
                                    <xsl:attribute name="href">
                                        <xsl:text>javascript:sort(</xsl:text>
                                        <xsl:value-of select="position()"/>
                                        <xsl:text>, '</xsl:text>
                                        <xsl:value-of select="'descending'"/>
                                        <xsl:text>')</xsl:text>
                                    </xsl:attribute>
                                    <img alt="" src="img/down-arrow.gif"/>
                                </a>
                                <a title="Tri croissant">
                                    <xsl:attribute name="href">
                                        <xsl:text>javascript:sort(</xsl:text>
                                        <xsl:value-of select="position()"/>
                                        <xsl:text>, '</xsl:text>
                                        <xsl:value-of select="'ascending'"/>
                                        <xsl:text>')</xsl:text>
                                    </xsl:attribute>
                                    <img alt="" src="img/up-arrow.gif"/>
                                </a>
                            </div>
                        </th>
                    </xsl:for-each>
                    <!--/th-->
                </tr>
            </thead>
            <!--tbody-->
            <xsl:variable name="sortDatatype">
                <xsl:choose>
                    <xsl:when test="$sortIndex = -1">nosort</xsl:when>
                    <xsl:when test="$columns/column[$sortIndex]/@datatype='integer' or $columns/column[$sortIndex]/@datatype='amount'">number</xsl:when>
                    <xsl:otherwise>text</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:choose>
                <xsl:when test="$sortIndex = -1">
                    <xsl:for-each select="values/line">
                        <xsl:call-template name="paintRow">
                            <xsl:with-param name="row" select="."/>
                            <xsl:with-param name="rowNum" select="position()"/>
                            <xsl:with-param name="documentId" select="$documentId"/>
                            <xsl:with-param name="etatId" select="$etatId"/>
                            <xsl:with-param name="elementId" select="$elementId"/>
                            <xsl:with-param name="coll-code" select="$coll-code"/>
                            <xsl:with-param name="coll-lib" select="$coll-lib"/>
                            <xsl:with-param name="budg-code" select="$budg-code"/>
                            <xsl:with-param name="budg-lib" select="$budg-lib"/>
                            <xsl:with-param name="navigation" select="$navigation"/>
                            <xsl:with-param name="columns" select="$columns"/>
                        </xsl:call-template>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:for-each select="values/line">
                        <xsl:sort select="cell[position()=$sortIndex]" order="{$sortOrder}" data-type="{$sortDatatype}"/>
                        <xsl:call-template name="paintRow">
                            <xsl:with-param name="row" select="."/>
                            <xsl:with-param name="rowNum" select="position()"/>
                            <xsl:with-param name="documentId" select="$documentId"/>
                            <xsl:with-param name="etatId" select="$etatId"/>
                            <xsl:with-param name="elementId" select="$elementId"/>
                            <xsl:with-param name="coll-code" select="$coll-code"/>
                            <xsl:with-param name="coll-lib" select="$coll-lib"/>
                            <xsl:with-param name="budg-code" select="$budg-code"/>
                            <xsl:with-param name="budg-lib" select="$budg-lib"/>
                            <xsl:with-param name="navigation" select="$navigation"/>
                            <xsl:with-param name="columns" select="$columns"/>
                        </xsl:call-template>
                    </xsl:for-each>
                </xsl:otherwise>
            </xsl:choose>
            <!--/tbody-->
        </table>
        <div class="TopLeft"><span style="display: none;">&amp;nbsp;</span></div>
        <div class="TopRight"><span style="display: none;">&amp;nbsp;</span></div>
    </body>
</html>
    </xsl:template>
    
    <xsl:template name="paintRow">
        <xsl:param name="row"/>
        <xsl:param name="rowNum"/>
        <xsl:param name="documentId"/>
        <xsl:param name="etatId"/>
        <xsl:param name="elementId"/>
        <xsl:param name="coll-code"/>
        <xsl:param name="coll-lib"/>
        <xsl:param name="budg-code"/>
        <xsl:param name="budg-lib"/>
        <xsl:param name="navigation"/>
        <xsl:param name="columns"/>
        
        <xsl:variable name="backColor">
            <xsl:choose>
                <xsl:when test="($rowNum mod 2) = 0">#EFF4F8</xsl:when>
                <xsl:otherwise>#FFFFFF</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="backStyle">
            <xsl:choose>
                <xsl:when test="($rowNum mod 2) = 0">Odd</xsl:when>
                <xsl:otherwise>Even</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="onclick">
            <xsl:choose>
                <xsl:when test="$navigation = 'true'">javascript:doClickOnRow(this.parentNode,'<xsl:value-of select="$documentId"/>','<xsl:value-of select="$etatId"/>','<xsl:value-of select="$elementId"/>','<xsl:value-of select="$coll-code"/>','<xsl:value-of select="$coll-lib"/>','<xsl:value-of select="$budg-code"/>','<xsl:value-of select="$budg-lib"/>')</xsl:when>
                <xsl:otherwise>javascript:void(0)</xsl:otherwise>
            </xsl:choose>
            
        </xsl:variable>
        <tr rownum="{$rowNum}" onmouseover="setPointer (this, '#CCFFCC')" onmouseout="setPointer (this, '{$backColor}')" class="{$backStyle}">
            <xsl:for-each select="cell">
                <xsl:variable name="currentPosition" select="position()"/>
                <xsl:variable name="display">
                    <xsl:choose>
                        <xsl:when test="$columns/column[$currentPosition]/@display='false'">display: none;</xsl:when>
                        <xsl:otherwise> </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="background">background-color: <xsl:value-of select="$backColor"/>;</xsl:variable>
                <xsl:variable name="wrap">
                    <xsl:choose>
                        <xsl:when test="$columns/column[$currentPosition]/@datatype='integer' or $columns/column[$currentPosition]/@datatype='amount'">text-wrap: none; white-space: nowrap;</xsl:when>
                        <xsl:otherwise></xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="align">text-align: 
                    <xsl:choose>
                        <xsl:when test="$columns/column[$currentPosition]/@datatype='integer' or $columns/column[$currentPosition]/@datatype='amount'">right</xsl:when>
                        <xsl:when test="$columns/column[$currentPosition]/@datatype='pj-list'">center</xsl:when>
                        <xsl:otherwise>left</xsl:otherwise>
                    </xsl:choose>;
                </xsl:variable>
                <td class="{$backStyle}" name="{$columns/column[$currentPosition]/@id}" style="{$display} {$background} {$wrap} {$align}">
                    <xsl:attribute name="onclick">
                        <xsl:choose>
                            <xsl:when test="$columns/column[$currentPosition]/@datatype='pj-list'">javascript:showPjInfos('<xsl:value-of select="$coll-code"/>','<xsl:for-each select="./pj-ref-list/pj"><xsl:value-of select="./@id"/><xsl:if test="not(position() = last())">|</xsl:if></xsl:for-each>','<xsl:for-each select="./pj-ref-list/pj"><xsl:value-of select="./@nom"/><xsl:if test="not(position() = last())">|</xsl:if></xsl:for-each>','<xsl:for-each select="./pj-ref-list/pj"><xsl:value-of select="./@support"/><xsl:if test="not(position() = last())">|</xsl:if></xsl:for-each>');this.parentNode.onmouseout()</xsl:when>
                            <xsl:otherwise><xsl:value-of select="$onclick"/></xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    <xsl:choose>
                        <xsl:when test="$columns/column[$currentPosition]/@datatype='amount'">
                            <xsl:call-template name="number"><xsl:with-param name="num" select="."/></xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$columns/column[$currentPosition]/@datatype='pj-list'">...</xsl:when>
                        <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
                    </xsl:choose>
                </td>
            </xsl:for-each>
        </tr>
        
    </xsl:template>

    <xsl:template name="number">
        <xsl:param name="num"/>
        <xsl:choose>
            <xsl:when test="count($num)=0"><xsl:value-of select="format-number('0','# ##0,00','decimalformat')"/><!--++NULL++--></xsl:when>
            <xsl:when test="count($num) &gt; 1"><!--xsl:value-of select="format-number('0','# ##0,00','decimalformat')"/-->+++ MANY +++</xsl:when>
            <xsl:when test="string(number($num)) = 'NaN'"><xsl:value-of select="format-number('0','# ##0,00','decimalformat')"/><!--++NaN++--></xsl:when>
            <xsl:when test="$num=0"><xsl:value-of select="format-number('0','# ##0,00','decimalformat')"/></xsl:when>
            <xsl:when test="$num = ''"><xsl:value-of select="format-number('0','# ##0,00','decimalformat')"/><!--++EMPTY++--></xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="format-number($num,'# ##0,00;-# ##0,00','decimalformat')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>
