﻿<?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">

  <xsl:include href="GenericTemplates/Item.xslt" />
  <xsl:include href="TaskFilter.xslt" />

  <xsl:template match="/">
    <xsl:variable name="TemplatePathURL" select ="Goal/TemplateURL"></xsl:variable>
    <xsl:variable name="GoalID" select="normalize-space(Goal/ID)"></xsl:variable>
    <xsl:variable name="DisplayCompleted" select="normalize-space(Goal/UserSettings/Completed)"></xsl:variable>
    <xsl:variable name="DisplayFuture" select="normalize-space(Goal/UserSettings/Future)"></xsl:variable>
    <xsl:variable name="DisplayCurrent" select="normalize-space(Goal/UserSettings/Current)"></xsl:variable>
    <xsl:variable name="DisplayMyItems" select="normalize-space(Goal/UserSettings/MyItems)"></xsl:variable>
    <xsl:variable name="DisplayOthersItems" select="normalize-space(Goal/UserSettings/Others)"></xsl:variable>
    <div id="divGoalDetail" style="width:100%">
      <div id="div_GoalOutlineTree_Filter" style="margin-bottom:15px">
        <xsl:call-template name="TaskFilter">
          <xsl:with-param name="Current" select="$DisplayCurrent"></xsl:with-param>
          <xsl:with-param name="Completed" select="$DisplayCompleted"></xsl:with-param>
          <xsl:with-param name="Future" select="$DisplayFuture"></xsl:with-param>
          <xsl:with-param name="MyItems" select="$DisplayMyItems"></xsl:with-param>
          <xsl:with-param name="Others" select="$DisplayOthersItems"></xsl:with-param>
          <xsl:with-param name="GoalItemID" select="$GoalID"></xsl:with-param>
        </xsl:call-template>
      </div>

      <div id="div_GoalOutlineTree" style="width:100%">

        <input type="hidden" id="hiddenDivGoalTreeID" value="div_item_{normalize-space(Goal/ID)}"></input>
        <div id="div_item_{normalize-space(Goal/ID)}" class="body" style="margin-left: -15px">

          <xsl:for-each select="Goal/Items/Item[ParentID = ../../ID]">
            <xsl:variable name="ItemDivPrefix">
              <xsl:text>div_item_</xsl:text>
              <xsl:value-of select ="normalize-space(ItemID)"></xsl:value-of>
            </xsl:variable>
            <xsl:variable name ="DisplayStyle">
              <xsl:choose>
                <xsl:when test ="($DisplayCompleted = 1 and normalize-space(Status) = 'Completed' and $DisplayMyItems = 1 and normalize-space(MineOrOther) = 'Me') 
                                              or ($DisplayCompleted = 1 and normalize-space(Status) = 'Completed' and $DisplayOthersItems = 1 and normalize-space(MineOrOther) != 'Me')
                                              or ($DisplayFuture = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&gt; 7 and $DisplayMyItems = 1 and normalize-space(MineOrOther) = 'Me')
                                              or ($DisplayFuture = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&gt; 7 and $DisplayOthersItems = 1 and normalize-space(MineOrOther) != 'Me')
                                              or ($DisplayCurrent = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&lt;= 7 and $DisplayMyItems = 1 and normalize-space(MineOrOther) = 'Me')
                                              or ($DisplayCurrent = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&lt;= 7 and $DisplayOthersItems = 1 and normalize-space(MineOrOther) != 'Me')
                                              or normalize-space(TypeID) = 17">
                  display:block
                </xsl:when>
                <xsl:otherwise>
                  display:none
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>

            <div id="{normalize-space($ItemDivPrefix)}" style="{normalize-space($DisplayStyle)};margin-left:15px; margin-right: 5px; margin-bottom: 10px;">
              <xsl:call-template name="MakeItem">
                <xsl:with-param name="TemplatePathURL" select="$TemplatePathURL"></xsl:with-param>
                <xsl:with-param name ="ItemDivPrefix" select="$ItemDivPrefix"></xsl:with-param>
                <xsl:with-param name="ParentParentID" select="-1"></xsl:with-param>
              </xsl:call-template>

              <xsl:call-template name="DisplayItemTree">
                <xsl:with-param name="ParentItemID" select="ItemID"></xsl:with-param>
                <xsl:with-param name ="ParentParentID" select ="ParentID"></xsl:with-param>
                <xsl:with-param name="TemplatePathURL" select="$TemplatePathURL"></xsl:with-param>
              </xsl:call-template>
            </div>
          </xsl:for-each>
        </div>
      </div>

      <xsl:if test="count(Goal/AddNewItem) &gt; 0">
        <div id="divParentAddNewItem" style="margin-bottom:10px;">
          <div id="divAddNewItem">
            <div id="divTxtAddAction" style="display: block;">
              <input id="txtAddAction" class="textAreaGoal" value="Add new item here..."
                     onclick="ItemNewMode('0','{$GoalID}', 'divGoalTask');"/>
            </div>
            <div id="divGoalTask" style="display: none">
              <!--<xsl:value-of select="Goal/AddNewItem/Html" disable-output-escaping ="yes"/>-->
            </div>
          </div>
        </div>
      </xsl:if>
    </div>
  </xsl:template>

  <xsl:template name ="DisplayItemTree">
    <xsl:param name="ParentItemID" select="'N/A'"></xsl:param>
    <xsl:param name ="ParentParentID" select="'N/A'"></xsl:param>
    <xsl:param name="TemplatePathURL" select="'N/A'"></xsl:param>
    <xsl:variable name="DisplayCompleted" select="normalize-space(../../UserSettings/Completed)"></xsl:variable>
    <xsl:variable name="DisplayFuture" select="normalize-space(../../UserSettings/Future)"></xsl:variable>
    <xsl:variable name="DisplayCurrent" select="normalize-space(../../UserSettings/Current)"></xsl:variable>
    <xsl:variable name="DisplayMyItems" select="normalize-space(../../UserSettings/MyItems)"></xsl:variable>
    <xsl:variable name="DisplayOthersItems" select="normalize-space(../../UserSettings/Others)"></xsl:variable>
    <xsl:for-each select="../Item[ParentID = $ParentItemID]">

      <xsl:variable name="ItemDivPrefix">
        <xsl:text>div_item_</xsl:text>
        <xsl:value-of select ="normalize-space(ItemID)"></xsl:value-of>
      </xsl:variable>
      <xsl:variable name ="DisplayStyle">
        <xsl:choose>
          <xsl:when test ="($DisplayCompleted = 1 and normalize-space(Status) = 'Completed' and $DisplayMyItems = 1 and normalize-space(MineOrOther) = 'Me') 
                          or ($DisplayCompleted = 1 and normalize-space(Status) = 'Completed' and $DisplayOthersItems = 1 and normalize-space(MineOrOther) != 'Me')
                          or ($DisplayFuture = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&gt; 7 and $DisplayMyItems = 1 and normalize-space(MineOrOther) = 'Me')
                          or ($DisplayFuture = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&gt; 7 and $DisplayOthersItems = 1 and normalize-space(MineOrOther) != 'Me')
                          or ($DisplayCurrent = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&lt;= 7 and $DisplayMyItems = 1 and normalize-space(MineOrOther) = 'Me')
                          or ($DisplayCurrent = 1 and normalize-space(Status) != 'Completed' and normalize-space(LeftDays)&lt;= 7 and $DisplayOthersItems = 1 and normalize-space(MineOrOther) != 'Me')
                          or normalize-space(TypeID) = 17
                          or normalize-space(TypeID) = 24">
            display:block
          </xsl:when>
          <xsl:otherwise>
            display:none
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <div id="{normalize-space($ItemDivPrefix)}" style="{normalize-space($DisplayStyle)};margin-left:15px; margin-right: 5px; margin-bottom: 10px;">
        <xsl:call-template name="MakeItem">
          <xsl:with-param name="TemplatePathURL" select="$TemplatePathURL"></xsl:with-param>
          <xsl:with-param name ="ItemDivPrefix" select="$ItemDivPrefix"></xsl:with-param>
          <xsl:with-param name="ParentParentID" select="$ParentParentID"></xsl:with-param>
        </xsl:call-template>

        <xsl:call-template name="DisplayItemTree">
          <xsl:with-param name ="ParentItemID" select="ItemID"></xsl:with-param>
          <xsl:with-param name ="ParentParentID" select="ParentID"></xsl:with-param>
          <xsl:with-param name="TemplatePathURL" select="$TemplatePathURL"></xsl:with-param>
        </xsl:call-template>
      </div>
    </xsl:for-each>
  </xsl:template>


  <xsl:template name ="MakeItem">
    <xsl:param name="TemplatePathURL" select="'N/A'"></xsl:param>
    <xsl:param name ="ItemDivPrefix" select ="'N/A'"></xsl:param>
    <xsl:param name ="ParentParentID" select ="'N/A'"></xsl:param>

    <xsl:variable name="UpperSiblingID">
      <xsl:if test ="preceding-sibling::Item[1]/ParentID = ParentID">
        <xsl:value-of select="preceding-sibling::Item[1]/ItemID"/>
      </xsl:if>
      <xsl:if test ="preceding-sibling::Item[1]/ParentID != ParentID or count(preceding-sibling::Item) = 0">
        -1
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="LowerSiblingID">
      <xsl:if test ="following-sibling::Item[1]/ParentID = ParentID">
        <xsl:value-of select="following-sibling::Item[1]/ItemID"/>
      </xsl:if>
      <xsl:if test ="following-sibling::Item[1]/ParentID != ParentID or count(following-sibling::Item) = 0">
        -1
      </xsl:if>
    </xsl:variable>

    <xsl:variable name="TempItemChilds">
      <xsl:call-template name="GetTotalsChildCounts">
        <xsl:with-param name="ParentItemID" select="normalize-space(ItemID)"></xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name ="TotalChildCount">
      <xsl:value-of select="sum(msxsl:node-set($TempItemChilds)/TotalChildCount)"/>
    </xsl:variable>
    <xsl:variable name ="TotalCount">
      <xsl:value-of select="sum(msxsl:node-set($TempItemChilds)/TotalCount)"/>
    </xsl:variable>
    <xsl:variable name ="Completed">
      <xsl:value-of select="sum(msxsl:node-set($TempItemChilds)/Completed)"/>
    </xsl:variable>
    <xsl:variable name ="PastDueCount">
      <xsl:value-of select="sum(msxsl:node-set($TempItemChilds)/PastDue)"/>
    </xsl:variable>

    <xsl:call-template name="Item">
      <xsl:with-param name="TemplatePathURL" select="$TemplatePathURL"></xsl:with-param>
      <xsl:with-param name="ItemID" select="normalize-space(ItemID)"></xsl:with-param>
      <xsl:with-param name="GoalID" select="normalize-space(../../ID)"></xsl:with-param>
      <xsl:with-param name="Status" select="normalize-space(Status)"></xsl:with-param>
      <xsl:with-param name="Level" select="normalize-space(Level)"></xsl:with-param>
      <xsl:with-param name="ParentParentID" select="normalize-space($ParentParentID)"></xsl:with-param>
      <xsl:with-param name="ParentID" select="normalize-space(ParentID)"></xsl:with-param>
      <xsl:with-param name="UpperSiblingID" select="normalize-space($UpperSiblingID)"></xsl:with-param>
      <xsl:with-param name="LowerSiblingID" select="normalize-space($LowerSiblingID)"></xsl:with-param>
      <xsl:with-param name="Mode" select="'GoalDetail'"></xsl:with-param>

      <xsl:with-param name="PermissionID" select="normalize-space(PermissionID)"></xsl:with-param>
      <xsl:with-param name="LeftDays" select="normalize-space(LeftDays)"></xsl:with-param>
      <xsl:with-param name="DueDate" select="normalize-space(DueDate)"></xsl:with-param>
      <xsl:with-param name="Responsible" select="normalize-space(MineOrOther)"></xsl:with-param>

      <xsl:with-param name="TotalCount" select="normalize-space($TotalCount)"></xsl:with-param>
      <xsl:with-param name="TotalChildCount" select="normalize-space($TotalChildCount)"></xsl:with-param>
      <xsl:with-param name="Completed" select="normalize-space($Completed)"></xsl:with-param>
      <xsl:with-param name="PastDueCount" select="normalize-space($PastDueCount)"></xsl:with-param>
      <xsl:with-param name="Name" select="normalize-space(Item)"></xsl:with-param>
      <xsl:with-param name="TypeID" select="normalize-space(TypeID)"></xsl:with-param>

      <xsl:with-param name="ItemDivPrefix" select ="normalize-space($ItemDivPrefix)"></xsl:with-param>

      <xsl:with-param name="RefItemTypeID" select="normalize-space(RefItemTypeID)"/>
      <xsl:with-param name="RefItemID" select="normalize-space(RefItemID)"/>
    </xsl:call-template>

  </xsl:template>

  <xsl:template name="GetTotalsChildCounts">
    <xsl:param name ="ParentItemID" select ="'N/A'"></xsl:param>
    <TotalChildCount>
      <xsl:value-of select="count(../Item[ParentID = $ParentItemID])"/>
    </TotalChildCount>
    <TotalCount>
      <xsl:value-of select="count(../Item[ParentID = $ParentItemID  and (normalize-space(TypeID) = 8 or normalize-space(TypeID) = 9)])"/>
    </TotalCount>
    <Completed>
      <xsl:value-of
          select="count(../Item[ParentID = $ParentItemID  
                        and (normalize-space(TypeID) = 8 or normalize-space(TypeID) = 9) 
                        and normalize-space(Status) = 'Completed'])"/>
    </Completed>
    <PastDue>
      <xsl:value-of
          select="count(../Item[ParentID = $ParentItemID  
                        and (normalize-space(TypeID) = 8 or normalize-space(TypeID) = 9) 
                        and normalize-space(Status) != 'Completed'
                        and normalize-space(LeftDays) &lt; 0])"/>

    </PastDue>
    <xsl:for-each select="../Item[ParentID = $ParentItemID and normalize-space(TypeID) = 17]">
      <xsl:call-template name="GetTotalsChildCounts">
        <xsl:with-param name="ParentItemID" select="normalize-space(ItemID)"></xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

  </xsl:template>

  <xsl:template name="GetTotalCompletedAction">
    <xsl:param name ="ParentItemID" select ="'N/A'"></xsl:param>

    <xsl:variable name="TempTotal">
      <xsl:call-template name="GetTotalsChildCounts">
        <xsl:with-param name="ParentItemID" select="$ParentItemID"></xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="sum(msxsl:node-set($TempTotal)/TotalChildCount)"/>
  </xsl:template>

</xsl:stylesheet>