<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:import href="Common.xslt"/>

  <xsl:template name="Result">
    <xsl:param name="res"/>
    <xsl:param name="code"/>
    <table class="input" align="center">
      <td class="input">
        <xsl:choose>
          <xsl:when test="$res = 'true' and ($code='000')">
            <span class="black">
              <xsl:text>ЗАГРУЗКА</xsl:text>
            </span>
          </xsl:when>
          <xsl:when test="$res = 'true' and ($code='001')">
            <span class="black">
              <xsl:text>ПРОВЕРКА</xsl:text>
            </span>
          </xsl:when>
          <xsl:when test="$res = 'true' and ($code='002')">
            <span class="black">
              <xsl:text>ОЖИДАНИЕ ОПЛАТЫ</xsl:text>
            </span>
          </xsl:when>
          <xsl:when test="$res = 'true' and ($code='004' or $code='0040' or $code='0041' or $code='0042')">
            <span class="black">
              <xsl:text>ПОЛУЧЕН ОТВЕТ</xsl:text>
            </span>
          </xsl:when>
          <xsl:when test="$res = 'true'">
            <span class="green">
              <xsl:text>УСПЕХ</xsl:text>
            </span>
          </xsl:when>
          <xsl:otherwise>
            <span class="red">
              <xsl:text>НЕУДАЧА</xsl:text>
            </span>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </table>
  </xsl:template>

  <xsl:template name="Stage">
    <p />
    <table align="center">
      <xsl:attribute name="width">
        <xsl:value-of select="$TableWidth"/>
      </xsl:attribute>
      <xsl:call-template name="Table4"/>
      <tr>
        <td colspan="4" align="center">
          <span style="font-weight:bold">
            <xsl:text>ЭТАП </xsl:text>
            <xsl:value-of select="position()"/>
          </span>
        </td>
      </tr>
      <tr>
        <td colspan="4">
          <xsl:choose>
            <xsl:when test="@CodeStage != ''">
              <xsl:call-template name="ControlStageBox">
                <xsl:with-param name="siz" select="100"/>
                <xsl:with-param name="code" select="@CodeStage"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="TextBox">
                <xsl:with-param name="siz" select="100"/>
                <xsl:with-param name="val" select="@NameStage"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </tr>
      <tr>
        <td>
          <span>
            <xsl:text>Результат</xsl:text>
          </span>
        </td>
        <td>
          <xsl:call-template name="Result">
            <xsl:with-param name="res" select="@CodeResult"/>
            <xsl:with-param name="code" select="@CodeStage"/>
          </xsl:call-template>
        </td>
        <td>
          <span>
            <xsl:text>Дата и время</xsl:text>
          </span>
        </td>
        <td>
          <xsl:call-template name="DateBox">
            <xsl:with-param name="siz" select="25"/>
            <xsl:with-param name="val" select="@Date"/>
          </xsl:call-template>
        </td>
      </tr>
      <xsl:if test="Info">
        <xsl:choose>
          <xsl:when test="Info/Protocol">
            <xsl:if test="Info/Protocol/Requests/Request/Reason!=''">
              <tr>
                <td colspan="4">
                  <div>
                    <xsl:text>Перечень ошибок и предупреждений</xsl:text>
                  </div>
                  <xsl:call-template name="Textarea">
                    <xsl:with-param name="text" select="Info/Protocol/Requests/Request/Reason"/>
                  </xsl:call-template>
                </td>
              </tr>
            </xsl:if>
          </xsl:when>
          <xsl:otherwise>
            <tr>
              <td colspan="4">
                <div>
                  <xsl:text>Комментарий</xsl:text>
                </div>
                <xsl:call-template name="Textarea">
                  <xsl:with-param name="text" select="Info"/>
                </xsl:call-template>
              </td>
            </tr>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
      <xsl:if test="Result">
        <tr>
          <td colspan="4">
            <div>
              <xsl:text>Перечень ошибок</xsl:text>
            </div>
            <xsl:call-template name="Textarea">
              <xsl:with-param name="text" select="Result"/>
            </xsl:call-template>
          </td>
        </tr>
      </xsl:if>
    </table>
  </xsl:template>
  
  <xsl:template name="Main_Proto">
    <p/>
    <xsl:call-template name="Main_First">
      <xsl:with-param name="X1" select="Proto/@CodeReg"/>
      <xsl:with-param name="X2" select="Proto/@CodePack"/>
      <xsl:with-param name="T2" select="'Пакет документов'"/>
      <xsl:with-param name="X3" select="Proto/@SessionID"/>
      <xsl:with-param name="T4">
        <xsl:choose>
          <xsl:when test="Proto/@CodeOut !=''">
            <xsl:text>Дата</xsl:text>
          </xsl:when>
          <xsl:otherwise />
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="X4">
        <xsl:call-template name="DateText">
          <xsl:with-param  name="val">
            <xsl:value-of select="Proto/Stage/@Date"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="T5" select="'Номер заявления в ОКУ'"/>
      <xsl:with-param name="X5">
        <xsl:value-of select="Proto/@CodeOut"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:if test="Proto/Stage[@CodeStage='000'] and not(Proto/Stage[@CodeStage!='000'])">
      <xsl:for-each select="Proto/Stage[@CodeStage='000']">
        <xsl:call-template name="Stage"/> 
      </xsl:for-each>
    </xsl:if>
    <xsl:if test="Proto/Stage[@CodeStage='001']">
      <xsl:for-each select="Proto/Stage[@CodeStage='001']">
        <xsl:call-template name="Stage"/>
      </xsl:for-each>
    </xsl:if>
    <xsl:if test="Proto/Stage[substring(@CodeStage,1,3)='002']">
      <xsl:for-each select="Proto/Stage[substring(@CodeStage,1,3)='002']">
        <xsl:call-template name="Stage"/>
      </xsl:for-each>
    </xsl:if>
    <xsl:if test="Proto/Stage[substring(@CodeStage,1,3)='003']">
      <xsl:for-each select="Proto/Stage[substring(@CodeStage,1,3)='003']">
        <xsl:call-template name="Stage"/>
      </xsl:for-each>
    </xsl:if>
    <xsl:if test="Proto/Stage[substring(@CodeStage,1,3)='004']">
      <xsl:for-each select="Proto/Stage[substring(@CodeStage,1,3)='004']">
        <xsl:call-template name="Stage"/>
      </xsl:for-each>
    </xsl:if>
        
    <xsl:for-each select="Proto/Stage[not((@CodeStage = 'ArcUnPack' or @CodeStage = 'Regulation' or @CodeStage = 'DocumNumber') and @CodeResult = 'true')]">
      <xsl:choose>
        <xsl:when test="@CodeStage='000' or @CodeStage='001' or substring(@CodeStage,1,3)='002' or substring(@CodeStage,1,3)='003' or substring(@CodeStage,1,3)='004'"/>
        <xsl:when test="@CodeStage='000'">
          <xsl:call-template name="Stage"/>
        </xsl:when>
        <xsl:when test="@CodeStage='001'">
          <xsl:call-template name="Stage"/>
        </xsl:when>
        <xsl:when test="substring(@CodeStage,1,3)='002'">
          <xsl:call-template name="Stage"/>
        </xsl:when>
        <xsl:when test="substring(@CodeStage,1,3)='003'">
          <xsl:call-template name="Stage"/>
        </xsl:when>
        <xsl:when test="substring(@CodeStage,1,3)='004'">
          <xsl:call-template name="Stage"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="Stage"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="Main">
    <P align="center">
      <span style="font-weight:bold">
        <xsl:text>Протокол контроля</xsl:text>
      </span>
    </P>
    <xsl:call-template name="Main_Proto"/>
  </xsl:template>

</xsl:stylesheet>
