﻿<?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">

    <xsl:key name="distinctSeqNum" match="AjaxRequest/Workout/WorkoutDetail" use="./SeqNum"></xsl:key>


    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="WorkoutName" select="AjaxRequest/ItemWorkouts/Workout/WorkoutName"></xsl:variable>
        <xsl:variable name="ResponsibleName" select="AjaxRequest/ItemWorkouts/ResponsibleName"></xsl:variable>
        <xsl:variable name="WorkoutCompletedDate" select="AjaxRequest/ItemWorkouts/Workout/FormattedCompleteDate"></xsl:variable>
        <xsl:variable name="WorkoutStartDate" select="AjaxRequest/ItemWorkouts/Workout/FormattedStartDate"></xsl:variable>
        <xsl:variable name="WorkoutExercisesXML" select="AjaxRequest/."></xsl:variable>
        <xsl:variable name="PTName" select="AjaxRequest/ItemWorkouts/PTName"></xsl:variable>
        <xsl:variable name="TodayDate" select="AjaxRequest/ItemWorkouts/Workout/TodaysDate"></xsl:variable>
        <xsl:variable name="FormattedStartDateTime" select="AjaxRequest/ItemWorkouts/Workout/FormattedStartDateTime"></xsl:variable>
        <xsl:variable name="FormattedCompleteDateTime" select="AjaxRequest/ItemWorkouts/Workout/FormattedCompleteDateTime"></xsl:variable>
        <xsl:variable name="Duration" select="AjaxRequest/ItemWorkouts/Workout/Duration"></xsl:variable>
        <xsl:variable name="PortalImage" select="AjaxRequest/ItemWorkouts/PortalImage"></xsl:variable>
        <xsl:variable name="ApprovedUserDisplayName" select="AjaxRequest/ItemWorkouts/Workout/ApprovedUserDisplayName"></xsl:variable>
        <xsl:variable name="InternalComments" select="AjaxRequest/ItemWorkouts/Workout/InternalComments"></xsl:variable>
        <xsl:variable name="FormattedApprovedDate" select="AjaxRequest/ItemWorkouts/Workout/FormattedApprovedDate"></xsl:variable>

        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="A4-portrait-workout"
                              page-height="29.7cm" page-width="21.0cm" margin="1.8cm">
                    <fo:region-body/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="A4-portrait-workout">
                <fo:flow flow-name="xsl-region-body">
                    <fo:table>
                        <fo:table-column column-width="12.7cm"/>
                        <fo:table-column column-width="8.0cm"/>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block margin-bottom="5pt">
                                        <fo:inline>
                                            <fo:external-graphic src="{$PortalImage}" content-height="50%" content-width="50%"/>
                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block>
                                        <fo:inline margin-right="5pt" font-family="PT Sans , sans-serif" font-size="10.0pt" color="#4B4B4B" font-weight="bold">Patient: </fo:inline>
                                        <fo:inline font-family="PT Sans ,  sans-serif" font-size="9.50pt" color="#4B4B4B" font-weight="bold">
                                            <xsl:value-of select="$ResponsibleName"/>
                                        </fo:inline>
                                    </fo:block>
                                    <fo:block>
                                        <fo:inline margin-right="5pt" font-family="PT Sans , sans-serif" font-size="10.0pt" color="#4B4B4B" font-weight="bold">Supervised By: </fo:inline>
                                        <fo:inline font-family="PT Sans , sans-serif" font-size="9.50pt" color="#4B4B4B" font-weight="bold">
                                            <xsl:value-of select="$PTName"/>
                                        </fo:inline>
                                    </fo:block>
                                    <fo:block>
                                        <fo:inline font-family="PT Sans , sans-serif" font-size="9.50pt" color="#4B4B4B" font-weight="bold">
                                            <xsl:value-of select="$TodayDate"/>
                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>

                    <fo:block text-align="left" space-before="2mm" space-after="4mm" background-color="#D2D3D4">
                        <fo:inline font-family="PT Sans , sans-serif" font-size="9.25pt" color="#4B4B4B" font-weight="bold">
                            <xsl:value-of select="$WorkoutName"/>
                        </fo:inline>
                    </fo:block>


                    <fo:table>
                        <fo:table-column column-width="1.0cm"/>
                        <fo:table-column column-width="2.0cm"/>
                        <fo:table-column column-width="12.0cm"/>
                        <fo:table-column column-width="0.6cm"/>
                        <fo:table-column column-width="0.9cm"/>
                        <fo:table-body>
                            <xsl:for-each select="$WorkoutExercisesXML/ItemWorkouts/WorkoutExercises/WorkoutExercise">
                                <xsl:variable name="WorkoutExerciseID" select="WorkoutExerciseID"></xsl:variable>

                                <xsl:variable name="MeasureValue">
                                    <xsl:choose>
                                        <xsl:when test="RepsYN = 1 and RepsNum != 0">
                                            <xsl:value-of select="RepsNum"/>
                                        </xsl:when>
                                        <xsl:when test="LevelYN = 1 and LevelNum != 0">
                                            <xsl:value-of select="LevelNum"/>
                                        </xsl:when>
                                        <xsl:when test="TimeYN = 1 and TimeActValue != 0">
                                            <xsl:value-of select="TimeActValue"/>
                                        </xsl:when>
                                        <xsl:when test="DistanceYN = 1 and DistanceActValue != 0">
                                            <xsl:value-of select="DistanceActValue"/>
                                        </xsl:when>
                                        <xsl:when test="WeightYN = 1 and WeightActValue != 0">
                                            <xsl:value-of select="WeightActValue"/>
                                        </xsl:when>
                                        <xsl:when test="CaloriesYN = 1 and CaloriesActValue != 0">
                                            <xsl:value-of select="CaloriesActValue"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:text></xsl:text>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:variable>

                                <fo:table-row>
                                    <fo:table-cell>
                                        <fo:block margin-bottom="5pt">
                                            <fo:inline font-family="PT Sans , sans-serif" font-size="10.0pt" color="#4B4B4B" font-weight="bold">
                                                <xsl:value-of select="position()"/>.
                                            </fo:inline>
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell>
                                        <fo:block>
                                            <fo:inline>
                                                <xsl:choose>
                                                    <xsl:when test="ThumbnailURL != ''">
                                                        <fo:external-graphic src="{ThumbnailURL}" content-height="38%" content-width="38%"/>
                                                    </xsl:when>
                                                    <xsl:otherwise>
                                                        <fo:external-graphic src="{VideoThumbnailURL}" content-height="38%" content-width="38%"/>
                                                    </xsl:otherwise>
                                                </xsl:choose>
                                            </fo:inline>
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell>
                                        <fo:block margin-bottom="2pt">
                                            <fo:inline font-family="PT Sans , sans-serif" font-size="10.0pt" color="#4B4B4B" font-weight="bold">
                                                <xsl:value-of select="ExerciseName"/>
                                            </fo:inline>
                                        </fo:block>
                                        <fo:block margin-bottom="3pt" font-family="PT Sans , sans-serif" font-size="7.5pt"
                                                  color="#6D6E71">
                                            <xsl:call-template name="break">
                                                <xsl:with-param name="text" select="Description"></xsl:with-param>
                                            </xsl:call-template>
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell>
                                        <fo:block text-align="center">
                                            <xsl:if test="$MeasureValue != ''">
                                                <fo:inline font-family="PT Sans , sans-serif" font-size="10.0pt" color="#4B4B4B" font-weight="bold">
                                                    <xsl:value-of select="$MeasureValue"/>
                                                </fo:inline>
                                            </xsl:if>
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell>
                                        <fo:block>
                                            <xsl:variable name="MeasureTextLabel">
                                                <xsl:choose>
                                                    <xsl:when test="RepsYN = 1">
                                                        <xsl:text>Reps</xsl:text>
                                                    </xsl:when>
                                                    <xsl:when test="LevelYN = 1">
                                                        <xsl:text>Levels</xsl:text>
                                                    </xsl:when>
                                                    <xsl:when test="TimeYN = 1">
                                                        <xsl:text>Seconds</xsl:text>
                                                    </xsl:when>
                                                    <xsl:when test="DistanceYN = 1">
                                                        <xsl:value-of select="DistanceMeasureValue"/>
                                                    </xsl:when>
                                                    <xsl:when test="WeightYN = 1">
                                                        <xsl:value-of select="WeightMeasureValue"/>
                                                    </xsl:when>
                                                    <xsl:when test="CaloriesYN = 1">
                                                        <xsl:text>Cals</xsl:text>
                                                    </xsl:when>
                                                    <xsl:otherwise>
                                                        <xsl:text></xsl:text>
                                                    </xsl:otherwise>
                                                </xsl:choose>
                                            </xsl:variable>
                                            <xsl:if test="$MeasureValue != ''">
                                                <fo:inline font-family="PT Sans , sans-serif" font-size="10.0pt" color="#4B4B4B" font-weight="bold">
                                                    <xsl:value-of select="$MeasureTextLabel"/>
                                                </fo:inline>
                                            </xsl:if>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>


                                <xsl:if test="count($WorkoutExercisesXML/ItemWorkouts/WorkoutExercises/WorkoutExercise/NotesXML/Note[CRMSourceID = $WorkoutExerciseID]) &gt; 0">
                                    <fo:table-row>
                                        <fo:table-cell>
                                            <fo:table >
                                                <fo:table-column column-width="1.0cm"/>
                                                <fo:table-column column-width="2.0cm"/>
                                                <fo:table-column column-width="15.0cm"/>

                                                <fo:table-body>
                                                    <xsl:for-each select="$WorkoutExercisesXML/ItemWorkouts/WorkoutExercises/WorkoutExercise/NotesXML/Note[CRMSourceID = $WorkoutExerciseID]">
                                                        <fo:table-row>
                                                            <fo:table-cell>
                                                                <fo:block font-family="Calibri, Verdana, Arial" font-size="7.5pt" margin-bottom="3pt">
                                                                    <fo:inline font-family="Calibri, Verdana, Arial" font-size="7.5pt" font-weight="bold" >

                                                                    </fo:inline>
                                                                </fo:block>
                                                            </fo:table-cell>
                                                            <fo:table-cell>
                                                                <fo:block font-family="Calibri, Verdana, Arial" font-size="7.5pt" margin-bottom="3pt">
                                                                    <fo:inline font-family="Calibri, Verdana, Arial" font-size="7.5pt" font-weight="bold" >

                                                                    </fo:inline>
                                                                </fo:block>
                                                            </fo:table-cell>
                                                            <fo:table-cell>
                                                                <fo:block>
                                                                    <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="8.0pt">
                                                                        <xsl:value-of select="Note"/> -
                                                                    </fo:inline>
                                                                    <fo:inline font-family="PT Sans , sans-serif" font-size="7.0pt" font-weight="bold" color="#6D6E71">
                                                                        <xsl:value-of select="DisplayName"/> - <xsl:value-of select="FormattedNoteDateTime"/>
                                                                    </fo:inline>
                                                                </fo:block>
                                                            </fo:table-cell>
                                                        </fo:table-row>
                                                    </xsl:for-each>
                                                </fo:table-body>
                                            </fo:table>
                                        </fo:table-cell>
                                    </fo:table-row>


                                    <xsl:if test="position() != count($WorkoutExercisesXML/ItemWorkouts/WorkoutExercises/WorkoutExercise)">
                                        <fo:table-row>
                                            <fo:table-cell>
                                                <fo:block margin-bottom="1.2pt">
                                                    <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="9.95pt" font-weight="bold">
                                                    </fo:inline>
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell>
                                                <fo:block margin-bottom="1.2pt">
                                                    <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="9.95pt" font-weight="bold">
                                                    </fo:inline>
                                                </fo:block>
                                            </fo:table-cell>
                                            <fo:table-cell>
                                                <fo:block margin-top="5pt" >
                                                    <fo:leader leader-length="3.8in" leader-pattern="rule"
                                                               baseline-shift="center" alignment-baseline="middle"
                                                               rule-thickness="0.5pt" color="#D2D3D4"/>
                                                </fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                    </xsl:if>
                                </xsl:if>
                            </xsl:for-each>
                        </fo:table-body>
                    </fo:table>
                    <fo:block margin-top="5pt">
                        <fo:leader leader-length="6.8in" leader-pattern="rule"
                                   baseline-shift="left" alignment-baseline="middle"
                                   rule-thickness="0.5pt" color="#696969"/>
                    </fo:block>


                    <fo:table>
                        <fo:table-column column-width="4.3cm"/>
                        <fo:table-column column-width="10.0cm"/>

                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block margin-bottom="1.2pt">
                                        <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="9.95pt" font-weight="bold">
                                            Start:
                                        </fo:inline>
                                        <fo:inline font-family="PT Sans , sans-serif" font-size="8.5pt" color="#6D6E71" font-weight="bold">
                                            <xsl:value-of select="$FormattedStartDateTime"/>
                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block margin-bottom="1.2pt">
                                        <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="9.95pt" font-weight="bold">
                                            End:
                                        </fo:inline>
                                        <fo:inline font-family="PT Sans , sans-serif" font-size="8.5pt" color="#6D6E71" font-weight="bold">
                                            <xsl:value-of select="$FormattedCompleteDateTime"/>
                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block margin-bottom="1.2pt">
                                        <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="9.95pt" font-weight="bold">
                                            Duration:
                                        </fo:inline>
                                        <fo:inline font-family="PT Sans , sans-serif" font-size="8pt" color="#6D6E71" font-weight="bold">
                                            <xsl:value-of select="$Duration"/>
                                        </fo:inline>
                                        <fo:inline font-family="PT Sans , sans-serif" font-size="8pt" color="#6D6E71" font-weight="bold">
                                            mins
                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block margin-bottom="1.2pt">
                                        <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="8pt" font-weight="bold">

                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell number-columns-spanned="2">
                                    <fo:block margin-bottom="1.2pt">
                                        <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="9.95pt" font-weight="bold">
                                            Comments:
                                        </fo:inline>
                                        <fo:inline font-family="PT Sans , sans-serif" font-size="8pt" color="#6D6E71" font-weight="bold">
                                            <xsl:value-of select="$InternalComments"/>
                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell>

                                    <fo:table>
                                        <fo:table-column column-width="5.0cm"/>
                                        <fo:table-column column-width="9.0cm"/>

                                        <fo:table-body>
                                            <fo:table-row>
                                                <fo:table-cell >
                                                    <fo:block>
                                                        <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="9.95pt" font-weight="bold">
                                                            Approved By:
                                                        </fo:inline>
                                                        <fo:inline font-family="PT Sans , sans-serif" font-size="8pt" color="#6D6E71" font-weight="bold" text-transform="capitalize">
                                                            <xsl:value-of select="$ApprovedUserDisplayName"/>
                                                        </fo:inline>
                                                    </fo:block>
                                                </fo:table-cell>
                                                <fo:table-cell>
                                                    <fo:block>
                                                        <fo:inline font-family="PT Sans , sans-serif" font-size="8pt" color="#6D6E71" font-weight="bold" text-transform="capitalize">
                                                            <xsl:value-of select="$FormattedApprovedDate"/>
                                                        </fo:inline>
                                                    </fo:block>
                                                </fo:table-cell>
                                            </fo:table-row>
                                        </fo:table-body>
                                    </fo:table>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block margin-bottom="1.2pt">
                                        <fo:inline font-family="PT Sans , sans-serif" color="#4B4B4B" font-size="8pt" font-weight="bold">

                                        </fo:inline>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template name="break">
        <xsl:param name="text" select="."/>

        <xsl:choose>
            <xsl:when test="contains($text, '&#xa;') or contains($text, '&#xa;')">
                <xsl:value-of select="substring-before($text, '&#xa;')"/>
                <br/>
                <xsl:call-template name="break">
                    <xsl:with-param name="text" select="substring-after($text,'&#xa;')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" disable-output-escaping="yes"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>