﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
	<xsl:output method="xml" indent="yes"/>
	<xsl:key name="uniqueBaseYearKey" match="TargetData/Target/BaseYear" use="." />
	<xsl:key name="uniqueDataKey" match="TargetData/Target/DataKey" use="." />
	<xsl:template match="/">
		<xsl:variable name="Root" select="AjaxRequest/."></xsl:variable>
		<xsl:variable name="CurrentDate" select="AjaxRequest/Params/KeyDate"></xsl:variable>
		<xsl:variable name="MetricList" select="AjaxRequest/Metrics"></xsl:variable>
		<xsl:variable name="TargetList" select="AjaxRequest/TargetData/Target"></xsl:variable>
		<xsl:variable name="NumberGoalID" select="AjaxRequest/Goals/Goal/ItemID"></xsl:variable>
		<xsl:variable name="NumberGoalName" select="AjaxRequest/Goals/Goal/Item"></xsl:variable>
		<xsl:variable name="TargetListUniqueYearList">
			<xsl:copy-of select="$TargetList/BaseYear[generate-id() = generate-id(key('uniqueBaseYearKey',.))]"/>
		</xsl:variable>
		<xsl:variable name="HostName" select="normalize-space(AjaxRequest/HostName)"></xsl:variable>
		<xsl:variable name="TargetListUniqueDataKeyList">
			<xsl:copy-of select="$TargetList/DataKey[generate-id() = generate-id(key('uniqueDataKey',.))]"/>
		</xsl:variable>
		<xsl:variable name="SalesActivityTrackerHTML" select="AjaxRequest/SalesActivityTrackerHTML"></xsl:variable>

		<xsl:variable name="IsSecureConnection" select="AjaxRequest/IsSecureConnection"></xsl:variable>
		<xsl:variable name="Protocol">
			<xsl:choose>
				<xsl:when test="$IsSecureConnection = 1">
					<xsl:text>https://</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>http://</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- content -->
		<div class="container compwid m-B-40 abc">
			<div class="row m-R-0 abcd">
				<h4 class="text-center business-targ"  style="float:left;">
					My Business Targets
				</h4>
				<div id="no-more-tables" class="newtable">
					<table class="table-bordered table-condensed cf" cellspacing="5" cellpadding="7">
						<colgroup>
							<col  style="background-color:#fff;"></col>
							<col  span="2" style="background-color:#ecf1f7;"></col>
							<col span="3" style="background-color:#9dc2e4;"></col>
							<!--<col style="background-color:#ecf1f7;"></col>
							<col style="background-color:#bed9ef;"></col>-->
						</colgroup>
						<thead class="cf">
							<tr>
								<th class="numeric" style="width:20%">-</th>
								<xsl:for-each select="msxsl:node-set($TargetListUniqueYearList)/child::node()">
									<xsl:sort select="."/>
									<xsl:choose>
										<xsl:when test="position() &lt; 3">
											<th style="width:16%;">
												<xsl:value-of select="."/>
											</th>
										</xsl:when>
										<xsl:otherwise>
											<th class="numeric" style="width:16%;">
												<xsl:value-of select="."/> Target
											</th>
											<th class="numeric" style="width:16%;">
												<xsl:value-of select="."/> Minimum
											</th>
											<th class="numeric" style="width:16%;">
												<xsl:value-of select="."/> Dream
											</th>
										</xsl:otherwise>
									</xsl:choose>
								</xsl:for-each>
							</tr>
						</thead>
						<tbody>
							<!--<xsl:for-each select="msxsl:node-set($TargetListUniqueDataKeyList)/child::node()">-->
							<xsl:for-each select="$MetricList/Metric">
								<xsl:variable name="varControlKey" select="ControlKey"></xsl:variable>
								<xsl:variable name="varBPMetricDisplay" select="MetricKeyDisplay"></xsl:variable>
								<xsl:if test="$TargetList[ControlKey = $varControlKey] != ''">
									<xsl:variable name="varDataKey" select="$TargetList[ControlKey = $varControlKey]/DataKey"></xsl:variable>
									<tr>
										<xsl:for-each select="msxsl:node-set($TargetListUniqueYearList)/child::node()">
											<xsl:sort select="."/>
											<xsl:variable name="varYear" select="."></xsl:variable>
											<xsl:variable name="varInputType">
												<xsl:choose>
													<xsl:when test="$TargetList[BaseYear=$varYear and DataKey=$varDataKey]/InputType='TextCurrency'">
														<xsl:text>$</xsl:text>
													</xsl:when>
													<xsl:otherwise>
														<xsl:text></xsl:text>
													</xsl:otherwise>
												</xsl:choose>
											</xsl:variable>
											<xsl:variable name="varTarget">
												<xsl:choose>
													<xsl:when test="$TargetList[BaseYear=$varYear and DataKey=$varDataKey]/Target">
														<xsl:value-of select="$varInputType"/>
														<xsl:value-of select="format-number(round($TargetList[BaseYear=$varYear and DataKey=$varDataKey]/Target), '###,###')"/>
													</xsl:when>
													<xsl:otherwise>
														<xsl:text></xsl:text>
													</xsl:otherwise>
												</xsl:choose>
											</xsl:variable>
											<xsl:variable name="varTargetMinimum">
												<xsl:choose>
													<xsl:when test="$TargetList[BaseYear=$varYear and DataKey=$varDataKey]/TargetQuota">
														<xsl:value-of select="$varInputType"/>
														<xsl:value-of select="format-number(round($TargetList[BaseYear=$varYear and DataKey=$varDataKey]/TargetQuota), '###,###')"/>
													</xsl:when>
													<xsl:otherwise>
														<xsl:text></xsl:text>
													</xsl:otherwise>
												</xsl:choose>
											</xsl:variable>
											<xsl:variable name="varTargetDream">
												<xsl:choose>
													<xsl:when test="$TargetList[BaseYear=$varYear and DataKey=$varDataKey]/TargetDream">
														<xsl:value-of select="$varInputType"/>
														<xsl:value-of select="format-number(round($TargetList[BaseYear=$varYear and DataKey=$varDataKey]/TargetDream), '###,###')"/>
													</xsl:when>
													<xsl:otherwise>
														<xsl:text></xsl:text>
													</xsl:otherwise>
												</xsl:choose>
											</xsl:variable>
											<!--<xsl:variable name="varBPMetricDisplay" select="$TargetList[BaseYear=$varYear and DataKey=$varDataKey]/DataKey"></xsl:variable>-->
											<xsl:if test="position() =1">
												<td  style="text-align:center!important" data-title="-" class="numeric">
													<xsl:value-of select="$varBPMetricDisplay"/>
												</td>
											</xsl:if>
											<xsl:choose>
												<xsl:when test="position() &lt; 3">
													<td data-title="{$varYear}" >
														<xsl:variable name="CurrentPosition" select="position()"></xsl:variable>
														<xsl:variable name="CurrentPeriod" select="msxsl:node-set($TargetListUniqueYearList)/child::node()[$CurrentPosition]/."></xsl:variable>
														<xsl:if test="$Root/Numbers[Period=$CurrentPeriod]/Metrics/Metric[Metric = $varDataKey]/Value">
															<xsl:if test="$Root/Numbers[Period=$CurrentPeriod]/Metrics/Metric[Metric = $varDataKey]/InputType = 'TextCurrency'">
																<xsl:text>$</xsl:text>
															</xsl:if>
															<xsl:value-of select="format-number(round($Root/Numbers[Period=$CurrentPeriod]/Metrics/Metric[Metric = $varDataKey]/Value), '###,###')"/>
														</xsl:if>
														<!--<xsl:value-of select="$varTarget"/>
														<xsl:variable name="CurrentPosition" select="position()"></xsl:variable>
														(<xsl:value-of select="$CurrentPosition"/>)
														<xsl:variable name="CurrentPeriod" select="msxsl:node-set($TargetListUniqueYearList)/child::node()[$CurrentPosition]/."></xsl:variable>
														(<xsl:value-of select="$CurrentPeriod"/>)
														(<xsl:value-of select="$Root/Numbers[Period=$CurrentPeriod]/Metrics/Metric[Metric = $varDataKey]/Value"/>)-->
													</td>
												</xsl:when>
												<xsl:otherwise>
													<td data-title="Target" class="numeric">
														<xsl:value-of select="$varTarget"/>
													</td>
													<td data-title="Minimum" class="numeric">
														<xsl:value-of select="$varTargetMinimum"/>
													</td>
													<td data-title="Dream" class="numeric">
														<xsl:value-of select="$varTargetDream"/>
													</td>
												</xsl:otherwise>
											</xsl:choose>
										</xsl:for-each>
									</tr>
								</xsl:if>
							</xsl:for-each>
						</tbody>
					</table>
				</div>
				<h5 class="Txtappointment">
					Update Past Data Here <a href="{$Protocol}{$HostName}/Goal.aspx?ID={$NumberGoalID}" title="Open Goal in New Window" target="_blank">
						<xsl:value-of select="$NumberGoalName"/>
					</a>
				</h5>
			</div>
		</div>
		<div class="container abc">
			<xsl:value-of select="$SalesActivityTrackerHTML" disable-output-escaping="yes"/>
		</div>
	</xsl:template>
</xsl:stylesheet>
