<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- set output method="xml|html|text" -->
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />

    <!-- ===== inlined helpers (remplacent date.xsl et string.xsl) ===== -->

    <!-- string-upper : met une chaîne en majuscule (gère accents courants) -->
    <xsl:template name="string-upper">
        <xsl:param name="value" />
        <xsl:variable name="v" select="normalize-space(string($value))"/>
        <xsl:choose>
            <xsl:when test="string-length($v) = 0">
                <xsl:text></xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <!-- map lowercase letters (latin + accents) -> uppercase -->
                <xsl:value-of select="
                    translate(
                        $v,
                        'abcdefghijklmnopqrstuvwxyzàâäéèêëïîôöùûüçÿæœ',
                        'ABCDEFGHIJKLMNOPQRSTUVWXYZÂÀÄÉÈÊËÏÎÔÖÙÛÜÇŸÆŒ'
                    )
                " />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- date-format : très simple pour ISO dates (YYYY-MM-DD[THH:MM:SS]) -->
    <!-- param `format` supporte 'd.m.Y' (par défaut) -->
    <xsl:template name="date-format">
        <xsl:param name="value" />
        <xsl:param name="format" select="'d.m.Y'" />
        <xsl:variable name="v" select="normalize-space(string($value))" />
        <xsl:choose>
            <!-- si ressemble à ISO (YYYY-MM-DD ou YYYY-MM-DDTHH:MM:SS) -->
            <xsl:when test="string-length($v) >= 10 and substring($v,5,1)='-' and substring($v,8,1)='-'">
                <xsl:variable name="year" select="substring($v,1,4)"/>
                <xsl:variable name="month" select="substring($v,6,2)"/>
                <xsl:variable name="day" select="substring($v,9,2)"/>
                <xsl:choose>
                    <xsl:when test="$format = 'd.m.Y'">
                        <xsl:value-of select="concat($day, '.', $month, '.', $year)"/>
                    </xsl:when>
                    <!-- fallback : même rendu -->
                    <xsl:otherwise>
                        <xsl:value-of select="concat($day, '.', $month, '.', $year)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <!-- sinon, renvoyer brut -->
            <xsl:otherwise>
                <xsl:value-of select="$v" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- ===== fin des helpers inlined ===== -->

    <!-- main -->
    <xsl:template match="/">
        <table class="ecole-jci-table" style="border: none;"><tbody>
            <xsl:for-each select="//Job">
                <tr>
                    <td style="vertical-align: middle; white-space: nowrap; border: none; box-shadow: none; border-top: 1px solid #C1BDB9; border-bottom: 1px solid #C1BDB9;background-color: transparent; color: #000;font-size: 12px;">
                        <xsl:apply-templates select="./Posting" />                 
                    </td>
                    <td style="vertical-align: middle; white-space: nowrap;border: none; box-shadow: none; border-top: 1px solid #C1BDB9; border-bottom: 1px solid #C1BDB9; font-weight: 900; font-size: 14px;background-color: transparent;">
                        <xsl:call-template name="string-upper">
                            <xsl:with-param name="value" select="./Position/Title" />
                        </xsl:call-template>
                    </td>
                    <td style="vertical-align: middle; border: none; border-top: 1px solid #C1BDB9; border-bottom: 1px solid #C1BDB9; padding-right: 0;background-color: transparent;">
                        <img decoding="async" src="https://ecole.hermes.com/wp-content/uploads/2022/06/pin.svg" style="width: 12px;max-width: none;margin-top: 4px" width="12px" />
                    </td>
                    <td style="vertical-align: middle; border: none; box-shadow: none; border-top: 1px solid #C1BDB9; border-bottom: 1px solid #C1BDB9; white-space: nowrap;background-color: transparent;padding-left: 8px;max-width: calc(100% - 37px);">
                        <a target="_blank" rel="nofollow noopener" style="text-decoration: underline; line-height: 20px; display: inline-block; white-space: normal;">
                            <xsl:attribute name="href"><xsl:value-of select="concat('https://www.google.com/maps/search/?api=1&query=', ./Location/City, ', ', ./Location/StateProvince )"/></xsl:attribute>
                            <xsl:call-template name="string-upper">
                                <xsl:with-param name="value" select="./Location/City" />
                            </xsl:call-template>
                            <xsl:value-of disable-output-escaping="yes" select="concat( ', ', ./Location/StateProvince )"/>
                        </a>
                    </td>
                    <td style="vertical-align: middle; border: none; box-shadow: none; border-top: 1px solid #C1BDB9; border-bottom: 1px solid #C1BDB9;background-color: transparent;">
                        <a target="_blank" style="color:#444; text-decoration:none; font-style: normal; font-weight: normal; font-size: 12px; text-transform: uppercase;background-color: #FFEE02;border-radius: 3px;border: 0;padding: 0 16px; white-space: nowrap;height: 40px;display: block;text-align: center;vertical-align: middle;line-height: 40px;" rel="noopener">
                            <xsl:attribute name="href"><xsl:value-of select="./Posting/CandidateResponses/URL"/></xsl:attribute>
                            détail de l’offre
                        </a>
                    </td>
                </tr>
            </xsl:for-each>
        </tbody></table>
    </xsl:template>

    <!-- date nodes : use template from date.xsl to format (and shift) the value -->
    <xsl:template match="Posting">
        <xsl:call-template name="date-format">
            <xsl:with-param name="value"><xsl:value-of select="./@DateCreated" /></xsl:with-param>
            <xsl:with-param name="format">d.m.Y</xsl:with-param>
        </xsl:call-template>
    </xsl:template>

</xsl:stylesheet>