<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
        xmlns:egonp="http://schemas.gov.sk/form/00151866.ZapisDokladuOAbsolvovaniPreskumania/1.1"
        				xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java">
        	<xsl:template match="/">
		<fo:root font-size="11pt" font-family="Arial, Narrow">
			<fo:layout-master-set>
				<fo:simple-page-master master-name="my-page" page-height="297mm" page-width="210mm" margin="1in">
						<fo:region-body margin-top="0mm" margin-bottom="15mm"/>
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="my-page">
				<fo:flow flow-name="xsl-region-body">
					<xsl:apply-templates/>
					<fo:block font-size="14pt"  font-weight="bold" font-family="Arial, Narrow" text-decoration="underline" margin-bottom="30pt">
					Zápis dokladu o absolvovaní odborného preskúmania vodiča
					</fo:block>
					<fo:table  font-family="Arial, Narrow" font-size="11pt" border-collapse="separate" border-spacing="0pt 10pt">
						<fo:table-column />
						<fo:table-column />
							<fo:table-body>
				<xsl:for-each select="/egonp:ZapisDokladuOAbsolvovaniPreskumania"><xsl:if test="egonp:Lekar"><xsl:call-template name="heading"><xsl:with-param name="label" select="&#39;Lekár&#39;"></xsl:with-param></xsl:call-template><xsl:for-each select="egonp:Lekar"><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Titul&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:Titul"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Meno&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:Meno"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Priezvisko&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:Priezvisko"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;IČO&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:ICO"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template></xsl:for-each></xsl:if><xsl:if test="egonp:PosudzovanaOsoba"><xsl:call-template name="heading"><xsl:with-param name="label" select="&#39;Informácie o priebehu preskúmania&#39;"></xsl:with-param></xsl:call-template><xsl:for-each select="egonp:PosudzovanaOsoba"><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Meno&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:Meno"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Priezvisko&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:Priezvisko"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="rowDate"><xsl:with-param name="label" select="&#39;Dátum narodenia&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:DatumNarodenia"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Rodné číslo&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:RodneCislo"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Druh dokladu&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:DruhDokladu"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="rowDate"><xsl:with-param name="label" select="&#39;Dátum vyhotovenia dokladu&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:DatumVydania"></xsl:with-param></xsl:call-template><xsl:call-template name="rowDate"><xsl:with-param name="label" select="&#39;Dátum platnosti dokladu&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:DatumPlatnosti"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Miesto vyhotovenia dokladu&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:MiestoVyhotovenia"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Stanovisko lekára/psychológa&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:Stanovisko"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Číslo rozhodnutia&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:CisloRozhodnutia"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Vyhovel&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:Vyhovel"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template><xsl:call-template name="row"><xsl:with-param name="label" select="&#39;Odporúčam preskúmať zdravotnú spôsobilosť&#39;"></xsl:with-param><xsl:with-param name="value" select="egonp:OdporucamPreskumat"></xsl:with-param><xsl:with-param name="hidden_label"></xsl:with-param></xsl:call-template></xsl:for-each></xsl:if></xsl:for-each>
						</fo:table-body>
					</fo:table>
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>  
       <xsl:template name="formatToSkDateTime">
    <xsl:param name="dateTime" />
    <xsl:variable name="dateSubstring" select="substring($dateTime, 1, 10)" />
    <xsl:variable name="timeSubstring" select="substring($dateTime, 12, 8)" />
    <xsl:variable name="timezoneSubstring" select="substring($dateTime, 20, 6)" />
    <xsl:variable name="timezoneSubstringUTC" select="substring($dateTime, 20, 7)" />
    <xsl:variable name="timezoneSubstringChar" select="substring($dateTime, 20, 1)" />
    <xsl:variable name="timeZoneString">
        <xsl:choose>
            <xsl:when test="contains($timezoneSubstringChar, 'Z') or contains($timezoneSubstringUTC, '+/-0:00')">
                <xsl:text>Časové pásmo UTC</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="contains($timezoneSubstring, '+02:00')">
                        <xsl:text>Časové pásmo +02:00</xsl:text>
                    </xsl:when>
                    <xsl:when test="contains($timezoneSubstring, '+01:00')">
                        <xsl:text>Časové pásmo +01:00</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>Časové pásmo </xsl:text>
                        <xsl:value-of select="$timezoneSubstring"></xsl:value-of>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="dateString">
        <xsl:choose>
            <xsl:when test="$dateSubstring != '' and string-length($dateSubstring)=10 and string(number(substring($dateSubstring, 1, 4))) != 'NaN' ">
                <xsl:value-of select="concat(substring($dateSubstring, 9, 2), '.', substring($dateSubstring, 6, 2), '.', substring($dateSubstring, 1, 4))" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$dateSubstring"></xsl:value-of>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="timeString">
        <xsl:if test="$timeSubstring != ''">
            <xsl:value-of select="substring($timeSubstring, 1, 5)" />
        </xsl:if>
    </xsl:variable>
    <!--xsl:value-of select="concat($dateString,' ',$timeString)"/-->
    <xsl:value-of select="$dateString"/>
</xsl:template>
<!-- koľko znakov spracovať v jednom bloku (bezpečná hĺbka rekurzie) -->
<!-- Chunk size for safe recursion depth -->
<xsl:variable name="CHUNK" select="30"/>
<!-- Main template: wraps text at word boundaries, max CHUNK chars per line -->
<xsl:template name="intersperse-with-zero-spaces">
    <xsl:param name="str"/>
    <xsl:call-template name="wrap-lines">
        <xsl:with-param name="str" select="$str"/>
        <xsl:with-param name="currentLine" select="''"/>
        <xsl:with-param name="currentLength" select="0"/>
    </xsl:call-template>
</xsl:template>
<!-- Wraps text into lines respecting word boundaries -->
<xsl:template name="wrap-lines">
    <xsl:param name="str"/>
    <xsl:param name="currentLine"/>
    <xsl:param name="currentLength"/>
    <xsl:choose>
        <xsl:when test="string-length($str) = 0">
            <!-- Output remaining line -->
            <xsl:value-of select="$currentLine"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="chunkSlice" select="substring($str, 1, $CHUNK)"/>
            <!-- True when the first CHUNK chars contain no whitespace and more chars follow:
                 get-next-word would recurse beyond safe depth, so force-break instead -->
            <xsl:variable name="noSpaceInChunk"
					select="
						string-length($str) &gt; $CHUNK and
						not(contains($chunkSlice, ' ')) and
						not(contains($chunkSlice, '&#x9;')) and
						not(contains($chunkSlice, '&#xA;'))"/>
            <xsl:choose>
                <!-- Long word, non-empty line: flush first, then force-break on next call -->
                <xsl:when test="$noSpaceInChunk and $currentLength &gt; 0">
                    <xsl:value-of select="$currentLine"/>
                    <xsl:text>&#xA;</xsl:text>
                    <xsl:call-template name="wrap-lines">
                        <xsl:with-param name="str" select="$str"/>
                        <xsl:with-param name="currentLine" select="''"/>
                        <xsl:with-param name="currentLength" select="0"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- Long word, empty line: take exactly CHUNK chars and break -->
                <xsl:when test="$noSpaceInChunk">
                    <xsl:value-of select="$chunkSlice"/>
                    <xsl:text>&#xA;</xsl:text>
                    <xsl:call-template name="wrap-lines">
                        <xsl:with-param name="str" select="substring($str, $CHUNK + 1)"/>
                        <xsl:with-param name="currentLine" select="''"/>
                        <xsl:with-param name="currentLength" select="0"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <!-- A word boundary exists within CHUNK chars: safe to call get-next-word
                         (recursion depth bounded by CHUNK = 64) -->
                    <xsl:variable name="nextWord">
                        <xsl:call-template name="get-next-word">
                            <xsl:with-param name="str" select="$str"/>
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:variable name="wordLength" select="string-length($nextWord)"/>
                    <xsl:variable name="wouldFit" select="$currentLength + $wordLength &lt;= $CHUNK"/>
                    <xsl:choose>
                        <xsl:when test="$wouldFit or $currentLength = 0">
                            <xsl:call-template name="wrap-lines">
                                <xsl:with-param name="str" select="substring($str, $wordLength + 1)"/>
                                <xsl:with-param name="currentLine" select="concat($currentLine, $nextWord)"/>
                                <xsl:with-param name="currentLength" select="$currentLength + $wordLength"/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$currentLine"/>
                            <xsl:text>&#xA;</xsl:text>
                            <xsl:call-template name="wrap-lines">
                                <xsl:with-param name="str" select="$str"/>
                                <xsl:with-param name="currentLine" select="''"/>
                                <xsl:with-param name="currentLength" select="0"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<!-- Extracts the next word (including trailing whitespace) -->
<xsl:template name="get-next-word">
    <xsl:param name="str"/>
    <xsl:param name="word" select="''"/>
    <xsl:param name="foundNonSpace" select="false()"/>
    <xsl:param name="inTrailingSpaces" select="false()"/>
    <xsl:choose>
        <xsl:when test="string-length($str) = 0">
            <xsl:value-of select="$word"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="c" select="substring($str, 1, 1)"/>
            <xsl:variable name="spacechars">
                &#x9;&#xA;
                &#x2000;&#x2001;&#x2002;&#x2003;&#x2004;&#x2005;
                &#x2006;&#x2007;&#x2008;&#x2009;&#x200A;&#x200B;&#x20;
            </xsl:variable>
            <xsl:variable name="isSpace" select="contains($spacechars, $c)"/>
            <xsl:choose>
                <!-- Stop before first non-space after a run of leading spaces -->
                <xsl:when test="not($foundNonSpace) and string-length($word) &gt; 0 and not($isSpace)">
                    <xsl:value-of select="$word"/>
                </xsl:when>
                <!-- Stop before first non-space after collecting trailing spaces -->
                <xsl:when test="$foundNonSpace and $inTrailingSpaces and not($isSpace)">
                    <xsl:value-of select="$word"/>
                </xsl:when>
                <!-- Continue collecting trailing spaces after a word -->
                <xsl:when test="$foundNonSpace and $isSpace">
                    <xsl:call-template name="get-next-word">
                        <xsl:with-param name="str" select="substring($str, 2)"/>
                        <xsl:with-param name="word" select="concat($word, $c)"/>
                        <xsl:with-param name="foundNonSpace" select="true()"/>
                        <xsl:with-param name="inTrailingSpaces" select="true()"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- Keep collecting non-space characters -->
                <xsl:when test="not($isSpace)">
                    <xsl:call-template name="get-next-word">
                        <xsl:with-param name="str" select="substring($str, 2)"/>
                        <xsl:with-param name="word" select="concat($word, $c)"/>
                        <xsl:with-param name="foundNonSpace" select="true()"/>
                        <xsl:with-param name="inTrailingSpaces" select="$inTrailingSpaces"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- Preserve runs of spaces as their own token -->
                <xsl:otherwise>
                    <xsl:call-template name="get-next-word">
                        <xsl:with-param name="str" select="substring($str, 2)"/>
                        <xsl:with-param name="word" select="concat($word, $c)"/>
                        <xsl:with-param name="foundNonSpace" select="$foundNonSpace"/>
                        <xsl:with-param name="inTrailingSpaces" select="$inTrailingSpaces"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template name="heading">
    <xsl:param name="label" />
    <fo:table-row>
        <fo:table-cell padding-end="5pt">
            <fo:block font-weight="bold" font-size="12pt" >
                <xsl:value-of select = "$label" />
            </fo:block>
        </fo:table-cell>
        <fo:table-cell padding-start="5pt">
            <fo:block>

				</fo:block>
        </fo:table-cell>
    </fo:table-row>
</xsl:template>
<xsl:template name="emptyRow">
    <xsl:param name="height" />
    <fo:table-row>
        <xsl:attribute name="height">
            <xsl:value-of select="$height" />
        </xsl:attribute>
        <fo:table-cell>
            <fo:block></fo:block>
        </fo:table-cell>
        <fo:table-cell>
            <fo:block></fo:block>
        </fo:table-cell>
    </fo:table-row>
</xsl:template>
<xsl:template name="row">
    <xsl:param name="label" />
    <xsl:param name="value" />
    <xsl:param name="margin-left" />
    <xsl:param name="hidden_label" />
    <xsl:if test="$value!=''">
        <fo:table-row>
            <fo:table-cell padding-end="5pt">
                <fo:block>
                    <xsl:if test="($hidden_label != 'true')">
                        <xsl:attribute name="margin-left">
                            <xsl:value-of select="$margin-left" />
                        </xsl:attribute>
                        <xsl:value-of select = "$label" />:
                    </xsl:if>
                </fo:block>
            </fo:table-cell>
            <fo:table-cell padding-start="5pt">
                <fo:block linefeed-treatment="preserve" white-space-collapse="false">
                    <xsl:call-template name="intersperse-with-zero-spaces">
                        <xsl:with-param name="str" select="$value" />
                    </xsl:call-template>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:if>
</xsl:template>
<xsl:template name="rowDecimal">
    <xsl:param name="label" />
    <xsl:param name="value" />
    <xsl:param name="margin-left" />
    <xsl:param name="hidden_label" />
    <xsl:if test="$value!=''">
        <fo:table-row>
            <fo:table-cell padding-end="5pt">
                <fo:block>
                    <xsl:if test="($hidden_label != 'true')">
                        <xsl:attribute name="margin-left">
                            <xsl:value-of select="$margin-left" />
                        </xsl:attribute>
                        <xsl:value-of select = "$label" />:
                    </xsl:if>
                </fo:block>
            </fo:table-cell>
            <fo:table-cell padding-start="5pt">
                <fo:block>
                    <xsl:call-template name="dtc_replaceDotWithComma">
                        <xsl:with-param name="input" select="$value" />
                    </xsl:call-template>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:if>
</xsl:template>
<xsl:template name="rowDate">
    <xsl:param name="label" />
    <xsl:param name="value" />
    <xsl:param name="margin-left" />
    <xsl:param name="hidden_label" />
    <xsl:if test="$value!=''">
        <fo:table-row>
            <fo:table-cell padding-end="5pt">
                <fo:block>
                    <xsl:if test="($hidden_label != 'true')">
                        <xsl:attribute name="margin-left">
                            <xsl:value-of select="$margin-left" />
                        </xsl:attribute>
                        <xsl:value-of select = "$label" />:
                    </xsl:if>
                </fo:block>
            </fo:table-cell>
            <fo:table-cell padding-start="5pt">
                <fo:block>
                    <xsl:call-template name="dtc_formatDate">
                        <xsl:with-param name="input" select="$value" />
                    </xsl:call-template>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:if>
</xsl:template>
<xsl:template name="rowDateTime">
    <xsl:param name="label" />
    <xsl:param name="value" />
    <xsl:param name="margin-left" />
    <xsl:param name="hidden_label" />
    <xsl:if test="$value!=''">
        <fo:table-row>
            <fo:table-cell padding-end="5pt">
                <fo:block>
                    <xsl:if test="($hidden_label != 'true')">
                        <xsl:attribute name="margin-left">
                            <xsl:value-of select="$margin-left" />
                        </xsl:attribute>
                        <xsl:value-of select = "$label" />:
                    </xsl:if>
                </fo:block>
            </fo:table-cell>
            <fo:table-cell padding-start="5pt">
                <fo:block>
                    <xsl:call-template name="dtc_formatDateTime">
                        <xsl:with-param name="input" select="$value" />
                    </xsl:call-template>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:if>
</xsl:template>
<xsl:template name="dtc_formatDateTime">
    <xsl:param name="input" />
    <xsl:variable name="date" select="substring-before($input, 'T')" />
    <xsl:variable name="year" select="substring-before($date, '-')" />
    <xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" />
    <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />
    <xsl:value-of select="$day" />
    <xsl:value-of select="'.'" />
    <xsl:value-of select="$month" />
    <xsl:value-of select="'.'" />
    <xsl:value-of select="$year" />
    <xsl:value-of select="' '" />
    <xsl:value-of select="substring-after($input, 'T')" />
</xsl:template>
<xsl:template name="dtc_formatDate">
    <xsl:param name="input" />
    <xsl:variable name="year" select="substring-before($input, '-')" />
    <xsl:variable name="month" select="substring-before(substring-after($input, '-'), '-')" />
    <xsl:variable name="day" select="substring-after(substring-after($input, '-'), '-')" />
    <xsl:value-of select="$day" />
    <xsl:value-of select="'.'" />
    <xsl:value-of select="$month" />
    <xsl:value-of select="'.'" />
    <xsl:value-of select="$year" />
</xsl:template>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>
<xsl:template match="text()">
    <xsl:value-of select="normalize-space()" />
</xsl:template>
<xsl:template name="dtc_replaceDotWithComma">
    <xsl:param name="input" />
    <xsl:value-of select="translate($input, '.', ',')" />
</xsl:template>
</xsl:stylesheet>