<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template name="newFilesHeaderRow">
        <tr class="Table-row Table-row-header">
            <th class="Table-col Tracker-col-service">Service</th>
            <th class="Table-col Table-col-icon"></th>
            <th class="Table-col Tracker-col-file-name">File Name</th>

            <th class="Table-col">Errors</th>
            <th class="Table-col Table-col-shrink">Received</th>
            <th class="Table-col Table-col-shrink">Status</th>
            <th class="Table-col Table-col-icon">Delete</th>
        </tr>
    </xsl:template>

    <xsl:template name="newFilesDataRow">

        <xsl:param name="serviceName" />
        <xsl:param name="isErrorLog" />
        <xsl:param name="canDelete" />
        <xsl:param name="showDeleteHelp" />
        <xsl:param name="notes" />
        <xsl:param name="status" />

        <tr class="Table-row Table-row-data">
            <td class="Table-col Tracker-col-service">
                <xsl:value-of select="$serviceName" />
            </td>

            <td class="Table-col Tracker-col-icon">
                <xsl:if test="$isErrorLog">
                    <a class="js-tooltip" href="/app/tracker?c=error_log&amp;file={FileID}" title="Download Error Log">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'download'" />
                            <xsl:with-param name="role" select="'action'" />
                        </xsl:call-template>
                    </a>
                </xsl:if>
            </td>

            <td class="Table-col Tracker-col-file-name">
                <a class="js-tooltip">
                    <xsl:if test="/Root/Access/app/tracker/saveas = 1">
                        <xsl:attribute name="href">/app/tracker?c=saveas&amp;file=<xsl:value-of select="FileID" />&amp;period=<xsl:value-of select="/Root/Report/Period/PeriodID" /></xsl:attribute>
                    </xsl:if>
                    <xsl:if test="/Root/User/AccessLevel = 1 and VersionNum != 0">
                        <xsl:attribute name="title">Importer Version: <xsl:value-of select="VersionNum" /></xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="OrigFileNamePretty" />
                </a>
            </td>

            <td class="Table-col"><xsl:value-of select="$notes" /></td>
            <td class="Table-col"><xsl:value-of select="DateCreatedPretty" /></td>
            <td class="Table-col Table-col-shrink">
                <xsl:call-template name="uiFileStatus">
                    <xsl:with-param name="status" select="$status" />
                </xsl:call-template>
            </td>

            <xsl:choose>
                <xsl:when test="$canDelete">
                    <td class="Table-col Table-col-icon">
                        <xsl:variable name="apos">'</xsl:variable>
                        <xsl:variable name="escaped">"</xsl:variable>

                        <span title="Delete File" class="Tracker-table-delete-trigger Icon-trigger js-tooltip">
                            <xsl:attribute name="data-file-name">
                                <xsl:value-of select="translate(OrigFileNamePretty,$apos,$escaped)" />
                            </xsl:attribute>
                            <xsl:attribute name="data-file-id">
                                <xsl:value-of select="FileID" />
                            </xsl:attribute>
                            <xsl:attribute name="data-page">
                                <xsl:value-of select="/Root/Pagination/CurrentPage" />
                            </xsl:attribute>
                            <xsl:call-template name="icon">
                                <xsl:with-param name="type" select="'delete'" />
                                <xsl:with-param name="role" select="'action'" />
                            </xsl:call-template>
                        </span>
                    </td>
                </xsl:when>

                <xsl:when test="$showDeleteHelp">
                    <xsl:call-template name="helpIcon">
                        <xsl:with-param name="tip">
                            <xsl:text>Errors for this file are currently being resolved.&lt;br&gt;If needed, you can request that this file be deleted by contacting support.</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>

                <xsl:otherwise>
                </xsl:otherwise>
            </xsl:choose>
        </tr>
    </xsl:template>

    <xsl:template name="newUploadedFilesRow">
        <xsl:call-template name="newFilesDataRow">
            <xsl:with-param name="serviceName">
                <xsl:if test="ServiceID != 0 or FileStatus != 1">
                    <xsl:value-of select="ServiceName" />
                </xsl:if>
            </xsl:with-param>
            <xsl:with-param name="isErrorLog" select="NonQualifiedErrorCount > 0 and /Root/Access/app/tracker/error_log = 1 and FileStatus = 3"/>
            <xsl:with-param name="canDelete" select="/Root/Access/app/tracker/delete" />
            <xsl:with-param name="notes">
                <xsl:if test="FileStatus = 3">
                    <xsl:value-of select="Notes" />
                </xsl:if>
            </xsl:with-param>
            <xsl:with-param name="status">
                <xsl:if test="/Root/Access/app/tracker/import">
                    <xsl:choose>
                        <xsl:when test="FileStatus = '2'">processing</xsl:when>
                        <xsl:when test="FileStatus = '6'">in_queue</xsl:when>
                        <xsl:otherwise>invalid</xsl:otherwise>
                    </xsl:choose>
                </xsl:if>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="onHoldFilesRow">
        <xsl:variable name="status">
            <xsl:choose>
                <xsl:when test="MapFaceStatus = 'all'">pending_review</xsl:when>
                <xsl:when test="MapFaceStatus = 'some'">working</xsl:when>
                <xsl:otherwise>support_notified</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:call-template name="newFilesDataRow">
            <xsl:with-param name="serviceName" select="ServiceName" />
            <xsl:with-param name="isErrorLog" select="/Root/Access/app/tracker/error_log = 1"/>
            <xsl:with-param name="canDelete" select="/Root/Access/rps/mapface/main" />
            <xsl:with-param name="showDeleteHelp" select="not(/Root/Access/rps/mapface/main)" />
            <xsl:with-param name="notes" select="'Unknown Service/Format/Country Data'" />
            <xsl:with-param name="status" select="$status" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="trackerNewFiles">
        <div class="Tracker-files-new">
            <div class="ControlsHeader">
                <h3 class="ControlsHeader-text">New Files</h3>
                <xsl:if test="/Root/Access/app/tracker/upload">
                    <div>
                        <xsl:call-template name="trackerUpload" />
                    </div>
                </xsl:if>
            </div>

            <xsl:choose>
                <xsl:when test="Files/NewFiles or (/Root/Access/app/tracker/upload and Files/OnHoldFiles)">
                    <table class="Table Table-with-highlight">
                        <thead>
                            <xsl:call-template name="newFilesHeaderRow" />
                        </thead>
                        <tbody>
                            <xsl:for-each select="Files/NewFiles/NewFile">
                                <xsl:sort select="DateCreated" order="descending" />
                                <xsl:call-template name="newUploadedFilesRow" />
                            </xsl:for-each>
                            <xsl:for-each select="Files/OnHoldFiles/OnHoldFile">
                                <xsl:sort select="DateCreated" order="descending" />
                                <xsl:call-template name="onHoldFilesRow" />
                            </xsl:for-each>
                        </tbody>
                    </table>
                </xsl:when>
                <xsl:otherwise>
                    <div class="Table-empty-results">
                        <p>No new files exist for this period.</p>
                    </div>
                </xsl:otherwise>
            </xsl:choose>
        </div>
    </xsl:template>

</xsl:stylesheet>

