<!--                                                                  -->
<!--  Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic"
                extension-element-prefixes="dyn">
    <xsl:output method="html" indent="no" doctype-system="" encoding="utf-8" />

    <xsl:include href="/app/tools/rps/templates/global.xsl"/>

    <xsl:template name="pickLayout">
        <div class="MainBaseSection">
            <script type="text/javascript">
                validationSet = {
                    Group: {
                        regexp: /^.*$/,
                        errorBlank: 'Please select data to report.<br/>'
                    },
                    Type: {
                        regexp: /^.*$/,
                        errorBlank: 'Please select the data subset or type.<br/>'
                    }
                };
            </script>
            <xsl:call-template name="createTypeList"/>

            <div class="Report-page">
                <h2>Request Report</h2>
                <xsl:call-template name="errorMessageGeneral"/>
                <xsl:call-template name="reportRequestSelect"/>
                <xsl:call-template name="dynamicReportList"/>
            </div>
        </div>
    </xsl:template>

    <xsl:template name="dynamicReportList">
        <xsl:if test="Form/ReportList/Report">
            <xsl:call-template name="reportTable"/>
        </xsl:if>
        <div class="reportMessaging">
            <xsl:if test="not(Form/ReportList/Report)">
                <section class="Table-empty-results">
                    <p>No reports are available at this time.</p>
                </section>
            </xsl:if>
            <div class="HelpText Table-annotation">Reports are available for 30 days after their request date.</div>
        </div>
    </xsl:template>

    <xsl:template name="reportTable">
        <table id="generatedReports" class="Table">
            <thead>
                <tr class="Table-row Table-row-header">
                    <th class="Table-col">Request Date</th>
                    <th class="Table-col">Requested By</th>
                    <th class="Table-col">Report</th>
                    <th class="Table-col">Status</th>
                    <th class="Table-col Table-col-all-actions">Action</th>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="Form/ReportList/Report">
                    <tr class="Table-row Table-row-data">
                        <xsl:if test="ReportID = /Root/Params/Highlight">
                            <xsl:attribute name="class">Table-row Table-row-data Table-row-checked</xsl:attribute>
                        </xsl:if>
                        <td class="Table-col">
                            <xsl:value-of select="substring(DateCreated,0,11)"/>
                        </td>
                        <td class="Table-col">
                            <xsl:value-of select="CreatedBy"/>
                        </td>
                        <td class="Table-col">
                            <xsl:call-template name="reportFileDownload"/>
                        </td>
                        <td class="Table-col">
                            <xsl:choose>
                                <xsl:when test="Status = 'new'">New</xsl:when>
                                <xsl:when test="Status = 'waiting'">
                                    <em>Waiting to Run</em>
                                </xsl:when>
                                <xsl:when test="Status = 'running'">
                                    <em>Running</em>
                                </xsl:when>
                                <xsl:when test="Status = 'available'">Available</xsl:when>
                                <xsl:when test="Status = 'cancelled'">Canceled</xsl:when>
                                <xsl:when test="Status = 'deleted'">Deleted</xsl:when>
                                <xsl:when test="Status = 'error'">Error</xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="Status"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </td>
                        <td class="Table-col Table-col-all-actions">
                            <xsl:call-template name="reportCancelReport"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template name="reportCancelReport">
        <xsl:choose>
            <xsl:when test="Status = 'new' or Status = 'waiting' or Status = 'running'">
                <a class="Table-action-item" href="/rps/report/cancel?ReportID={ReportID}">Cancel</a>
            </xsl:when>
            <xsl:otherwise>
                <div class="Table-action-item js-tooltip" title="Delete">
                    <a href="javascript:void(-1)" onClick="ReportPage.confirmDelete({ReportID}, event);">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'delete'" />
                            <xsl:with-param name="role" select="'action'" />
                        </xsl:call-template>
                    </a>
                </div>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="reportFileDownload">
        <xsl:value-of select="FileName"/>
        <xsl:if test="Status = 'available'">
            <xsl:for-each select="Downloads/Type">
                <xsl:sort select="."/>

                <xsl:call-template name="uiDownloadLink">
                    <xsl:with-param name="text">
                        <xsl:text>.</xsl:text>
                        <xsl:value-of select="translate(.,$lcletters,$ucletters)"/>
                    </xsl:with-param>
                    <xsl:with-param name="url">
                        <xsl:text>/rps/report/download?ReportID=</xsl:text>
                        <xsl:value-of select="../../ReportID"/>
                        <xsl:text>&amp;Type=</xsl:text>
                        <xsl:value-of select="."/>
                    </xsl:with-param>
                    <xsl:with-param name="className" select="'Report-page-download-link'"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:if>
    </xsl:template>

    <xsl:template name="reportRequestSelect">
        <form action="/rps/report/show" method="get" id="reportRequest" >
            <div class="Table-controls Table-controls-top-up">
                <div class="Table-controls-item">
                    <div class="Form-group-horizontal Form-group-horizontal-up">
                        <div class="Form-item Form-item-standart">
                            <label class="Form-label">
                                <span class="Form-label-caption">Select data to report</span>
                                <div class="Form-input-wrapper">
                                    <xsl:variable name="groupItems">
                                        <map>
                                            <mapEntry>
                                                <name>Choose...</name>
                                                <value></value>
                                            </mapEntry>
                                            <xsl:for-each select="Form/GroupList/Group">
                                                <xsl:sort select="Name"/>
                                                <mapEntry>
                                                    <name><xsl:value-of select="Name" /></name>
                                                    <value><xsl:value-of select="Name" /></value>
                                                </mapEntry>
                                            </xsl:for-each>
                                        </map>
                                    </xsl:variable>
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$groupItems"/>
                                        <xsl:with-param name="id" select="'Group'"/>
                                        <xsl:with-param name="name" select="'Group'"/>
                                        <xsl:with-param name="onChange">updateSubSelect(this, Type, typeList)</xsl:with-param>
                                    </xsl:call-template>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Group</xsl:with-param>
                                <xsl:with-param name="displayName">Data</xsl:with-param>
                            </xsl:call-template>
                        </div>
                        <div class="Form-item Form-item-standart">
                            <label class="Form-label">
                                <span class="Form-label-caption">Select data subset or type</span>
                                <div class="Form-input-wrapper">
                                    <xsl:variable name="typeItems">
                                        <map>
                                            <mapEntry>
                                                <name>Choose...</name>
                                                <value></value>
                                            </mapEntry>
                                        </map>
                                    </xsl:variable>
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$typeItems"/>
                                        <xsl:with-param name="id" select="'Type'"/>
                                        <xsl:with-param name="name" select="'Type'"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Type</xsl:with-param>
                                <xsl:with-param name="displayName">Data Subset or Type</xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </div>
                </div>
                <div class="Table-controls-item Table-controls-item-additive">
                    <div class="Form-item">
                        <label class="Form-label">
                            <span class="Form-label-caption"> </span>
                            <div class="Form-input-wrapper">
                                <xsl:call-template name="uiButton">
                                    <xsl:with-param name="elemType" select="'submit'" />
                                    <xsl:with-param name="content" select="'Configure Report'" />
                                </xsl:call-template>
                            </div>
                        </label>
                    </div>
                </div>
            </div>
        </form>
    </xsl:template>

    <xsl:template name="createTypeList">
        <script type="text/javascript">
            // setting up an array of types for each group
            var typeList = {};

            <xsl:for-each select="/Root/Form/GroupList/Group">
                var type<xsl:value-of select="@i"/> = {};

                <xsl:call-template name="reportJSArrayElements">
                    <xsl:with-param name="arrayName">type<xsl:value-of select="@i"/></xsl:with-param>
                </xsl:call-template>

                typeList['<xsl:value-of select="Name"/>'] = type<xsl:value-of select="@i"/>;
            </xsl:for-each>

        </script>
    </xsl:template>

    <xsl:template name="reportJSArrayElements">
        <xsl:param name="arrayName"/>
        <xsl:for-each select="TypeList/Type">
            <xsl:sort select="Type"/>
            <xsl:value-of select="$arrayName"/>['<xsl:value-of select="."/>'] = '<xsl:value-of select="."/>';
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
