<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="http://www.w3.org/TR/html4/strict.dtd" doctype-public="-//W3C//DTD HTML 4.01//EN" encoding="utf-8" />

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

    <xsl:variable name="fieldPrefix">Form_SaleFeedServiceList_SaleFeedService_</xsl:variable>

    <xsl:template name="pickLayout">
        <link href="/production/css/bookpub/admin.css?v=2" rel="stylesheet" type="text/css" />
        <script src="/production/javascript/jquery/jquery-3.6.0.min.js"></script>
        <script type="text/javascript" src="/production/javascript/bookpub/admin.js?v=13"></script>
        <input type="hidden" name="dateFormat" id="dateFormat" value="{$calendarDateFormat}" />
        <xsl:call-template name="salesFeedLayout" />
        <xsl:call-template name="buttons" />
        <xsl:call-template name="serviceCountryCodeArrays" />
    </xsl:template>


    <xsl:template name="salesFeedLayout">
        <div class="section" id="salesFeed">

            <p class="errorGeneral messageBox hidden" id="mainErrorMessage">
                Please fix the errors below.
            </p>

            <p class="error hidden" id="userNameError">
                User Name cannot be blank.
            </p>

            <p class="error hidden" id="customNameError">
                Name cannot be blank.
            </p>

            <p class="error hidden" id="passwordError">
                Password cannot be blank.
            </p>

            <p class="error hidden" id="startDateError">
                Start Date cannot be blank.
            </p>

            <p class="error hidden" id="countryCodeError">
                Country Code must be selected.
            </p>

            <table class="Table" id="serviceList">
                <thead>
                    <tr class="Table-row Table-row-header">
                        <th class="Table-col">Service</th>
                        <th class="Table-col">User Name</th>
                        <th class="Table-col">Password</th>
                        <th class="Table-col">Status</th>
                        <th class="Table-col">Start Date</th>
                        <th class="Table-col">Last Updated</th>
                        <th class="Table-col">Edit</th>
                    </tr>
                </thead>
                <tbody>
                    <tr class="Table-row Table-row-data" id="emptyListMessage" class="row1">
                        <td class="Table-col" colspan="7">
                            No services have been added.  Add a service from the drop-down list to enable configuration.
                        </td>
                    </tr>

                    <xsl:call-template name="serviceRow">
                        <xsl:with-param name="rowID">hiddenService</xsl:with-param>
                        <xsl:with-param name="fieldID">0</xsl:with-param>
                    </xsl:call-template>

                    <xsl:for-each select="SaleFeedServiceList/SaleFeedService">
                        <xsl:sort select="StartDate" />
                        <xsl:call-template name="serviceRow">
                            <xsl:with-param name="rowID">
                                <xsl:text>row</xsl:text><xsl:value-of select="SaleFeedServiceID" />
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:for-each>
                </tbody>
            </table>
            <br />

            <xsl:if test="SaleFeedServiceList/SaleFeedService">
                <script type="text/javascript">
                    // Hide the empty list message
                    $("#emptyListMessage").hide();
                </script>
            </xsl:if>

            <div style="float: left;">
                <p class="addLink">
                    <select name="serviceID" id="serviceID">
                        <option value="8">Amazon</option>
                        <option value="9">Apple</option>
                        <option value="16">Barnes &amp; Noble</option>
                    </select>&#160;
                    <a href="javascript:void(0)" onClick="addServiceRowCheck('{$fieldPrefix}')" title="Add Service" id="addImageLink">
        	            <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />Add Service
          	    	</a>
      	    	</p>
  	    	</div>

            <br class="superBreak" />
            <br />

            <script type="text/javascript">
                var serviceAccounts = new Object();
                serviceAccounts[16] = <xsl:value-of select="count(SaleFeedServiceList/SaleFeedService[ServiceID = 16])" />;
            </script>

        </div>
    </xsl:template>


    <xsl:template name="serviceRow">
        <xsl:param name="rowID" />
        <xsl:param name="fieldID" select="SaleFeedServiceID" />

        <xsl:variable name="fieldIdentifier"><xsl:value-of select="$fieldPrefix" /><xsl:value-of select="$fieldID" /></xsl:variable>
        <xsl:variable name="serviceID" select="ServiceID" />
        <xsl:variable name="serviceName" select="/Root/ServiceList/Service[ServiceID = $serviceID]/ServiceName" />
        <xsl:variable name="status">
            <xsl:choose>
                <xsl:when test="Verified = 1">Verified</xsl:when>
                <xsl:otherwise>Unable to Verify</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <tr class="hidden Table-row Table-row-data" id="{$rowID}">
            <td class="Table-col mainRow">
                <xsl:value-of select="$serviceName" />
                <xsl:if test="ServiceID = 8">
                    <xsl:text> - </xsl:text>
                    <xsl:value-of select="CountryCode" />
                    <input type="hidden" name="{$fieldIdentifier}_CountryCode" id="{$fieldIdentifier}_CountryCode" value="{CountryCode}" />
                </xsl:if>
                <input type="hidden" name="{$rowID}Verified" id="{$rowID}Verified" value="{Verified}" />
                <input type="hidden" name="{$rowID}ServiceName" id="{$rowID}ServiceName" value="{$serviceName}" />
                <input type="hidden" name="{$rowID}ServiceID" id="{$rowID}ServiceID" value="{ServiceID}" />
                <input type="hidden" name="{$fieldIdentifier}_ServiceID" id="{$fieldIdentifier}_ServiceID" value="{ServiceID}" />
            </td>
            <td class="Table-col mainRow">
                <input type="hidden" name="{$fieldIdentifier}_SaleFeedServiceID" id="{$fieldIdentifier}_SaleFeedServiceID" value="{SaleFeedServiceID}" />
                <input type="text" name="{$fieldIdentifier}_Username" id="{$fieldIdentifier}_Username" value="{Username}" />
            </td>
            <td class="Table-col mainRow">
                <input type="password" name="{$fieldIdentifier}_Password" id="{$fieldIdentifier}_Password" value="{Password}" style="width: 100px;" />
            </td>
            <td class="Table-col mainRow" id="{$rowID}Status">
                <xsl:if test="SaleFeedServiceID">
                    <xsl:value-of select="$status" />
                </xsl:if>
            </td>
            <td class="Table-col mainRow">
                <input type="text" class="dateField" name="{$fieldIdentifier}_StartDate" id="{$fieldIdentifier}_StartDate" value="{StartDate}" />
                <a title="Select Date Sent" id="calendarLink{$fieldID}">
                    <img src="/production/images/icons/office/Calendar-(16x16).gif" height="16" width="16" class="calendarIcon" id="calendarIcon{$fieldID}"/>
                </a>
                <xsl:call-template name="calendar">
            	    <xsl:with-param name="fieldID"><xsl:value-of select="$fieldIdentifier" />_StartDate</xsl:with-param>
            	    <xsl:with-param name="buttonID">calendarIcon<xsl:value-of select="$fieldID" /></xsl:with-param>
                </xsl:call-template>
            </td>
            <td class="Table-col mainRow" id="{$rowID}DateModified">
                <xsl:value-of select="DateModified" />
            </td>
            <td class="Table-col mainRow">
                <table class="buttons">
                    <tr>
                        <td class="defaultButton">
                            <input type="button" value="Save" onClick="saveServiceChanges(this, '{$fieldPrefix}');" />
                        </td>

                        <td>

                            <input type="button" value="Cancel" id="{$rowID}Cancel">
                                <xsl:if test="not(SaleFeedServiceID)">
                                    <xsl:attribute name="onClick">removeServiceRow(this)</xsl:attribute>
                                </xsl:if>
                            </input>

                            <xsl:if test="SaleFeedServiceID">
                                <script type="text/javascript">
                                    $(document).ready(function() {
                                        $("#<xsl:value-of select="$rowID" />Cancel").on('click', function()
                                        {
                                            $("#<xsl:value-of select="$rowID" />ReadOnly").show();
                                            $("#<xsl:value-of select="$rowID" />").hide();
                                        });
                                    });
                                </script>

                                <input type="button" value="Delete" onClick="confirmDeleteSaleFeedService({SaleFeedServiceID},{ServiceID});" />

                            </xsl:if>

                        </td>

                    </tr>
                </table>
            </td>
        </tr>

        <xsl:if test="$fieldID != 0">
            <tr id="{$rowID}ReadOnly" class="Table-row">
                <td class="Table-col mainRow">
                    <xsl:value-of select="$serviceName" />
                    <xsl:if test="ServiceID = 8"> - <xsl:value-of select="CountryCode" /></xsl:if>
                </td>
                <td class="Table-col mainRow">
                    <xsl:value-of select="Username" />
                </td>
                <td class="Table-col mainRow">
                    <xsl:call-template name="passwordPlaceholder">
                        <xsl:with-param name="currentCount" select="'1'" />
                        <xsl:with-param name="characterCount" select="string-length(Password)" />
                    </xsl:call-template>
                </td>
                <td class="Table-col mainRow">
                    <xsl:value-of select="$status" />
                </td>
                <td class="Table-col mainRow">
                    <xsl:value-of select="StartDate" />
                </td>
                <td class="Table-col mainRow">
                    <xsl:value-of select="DateModified" />
                </td>
                <td class="Table-col mainRow">
                    <a title="Edit Service">
                        <img src="/production/images/icons/office/Edit-(16x16).gif" height="16" width="16" id="{$rowID}EditIcon" />
                    </a>
                </td>
            </tr>

            <tr id="{$rowID}Help" class="mainRow Table-row Table-row-data">
                <td class="Table-col" colspan="3">
                    <xsl:choose>
                        <xsl:when test="Verified = 0">
                            <em>Sales feed information will be available once your credentials are verified.</em>
                        </xsl:when>
                        <xsl:when test="Verified = 1 and count(SaleFeedList/SaleFeed) &gt; 0">
                            <a href="javascript:void(0)" id="{$rowID}SalesFeedShow">View Sales Feed Information</a>
                            <a href="javascript:void(0)" id="{$rowID}SalesFeedHide">Hide Sales Feed Information</a>
                        </xsl:when>
                        <xsl:otherwise>
                            No sales feeds found for this account.
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
                <td class="Table-col" colspan="4">
                    <xsl:if test="Verified = 1 and count(SaleFeedList/SaleFeed[Status = 'pending']) &gt; 0">
                        <span id="{$rowID}PendingFeedCount">
                            You have <b><u><xsl:value-of select="count(SaleFeedList/SaleFeed[Status = 'pending'])" /></u></b> pending sales feed<xsl:if test="count(SaleFeedList/SaleFeed[Status = 'pending']) &gt; 1">s</xsl:if>.
                        </span>
                    </xsl:if>
                </td>
            </tr>

            <xsl:if test="Verified = 1 and count(SaleFeedList/SaleFeed) &gt; 0">
                <tr id="{$rowID}SalesFeed" class="Table-row Table-row-data">
                    <td class="Table-col" colspan="7" class="saleFeedWrapper">
                        <table class="Table">
                            <thead>
                                <xsl:call-template name="salesFeedHeaderRow" />
                            </thead>
                            <tbody>
                                <xsl:for-each select="SaleFeedList/SaleFeed">
                                    <xsl:sort select="StartDate" />
                                    <xsl:sort select="Name" />
                                    <xsl:call-template name="salesFeedRow" />
                                    <xsl:call-template name="salesFeedEditRow" />

                                    <script type="text/javascript">
                                        $(document).ready(function() {
                                            $("#feed<xsl:value-of select="SaleFeedID" />EditIcon").on('click', function()
                                            {
                                                $("#feed<xsl:value-of select="SaleFeedID" />ReadOnly").hide();
                                                $("#feed<xsl:value-of select="SaleFeedID" />").show();
                                            });
                                        });
                                    </script>

                                </xsl:for-each>
                            </tbody>
                        </table>
                    </td>
                </tr>
            </xsl:if>

        </xsl:if>

        <script type="text/javascript">
            $(document).ready(function() {
                $("#<xsl:value-of select="$rowID" />EditIcon").on('click', function()
                {
                    $("#<xsl:value-of select="$rowID" />ReadOnly").hide();
                    $("#<xsl:value-of select="$rowID" />").show();
                });

                $("#<xsl:value-of select="$rowID" />SalesFeed").hide();
                $("#<xsl:value-of select="$rowID" />SalesFeedHide").hide();
                $("#<xsl:value-of select="$rowID" />SalesFeedHide").on('click', function()
                {
                    $("#<xsl:value-of select="$rowID" />SalesFeed").hide();
                    $("#<xsl:value-of select="$rowID" />SalesFeedHide").hide();
                    $("#<xsl:value-of select="$rowID" />SalesFeedShow").show();
                });
                $("#<xsl:value-of select="$rowID" />SalesFeedShow").on('click', function()
                {
                    $("#<xsl:value-of select="$rowID" />SalesFeed").show();
                    $("#<xsl:value-of select="$rowID" />SalesFeedShow").hide();
                    $("#<xsl:value-of select="$rowID" />SalesFeedHide").show();
                });
                $("#<xsl:value-of select="$rowID" />PendingFeedCount").on('click', function()
                {
                    $("#<xsl:value-of select="$rowID" />SalesFeed").toggle();
                    $("#<xsl:value-of select="$rowID" />SalesFeedHide").toggle();
                    $("#<xsl:value-of select="$rowID" />SalesFeedShow").toggle();
                });

            });
        </script>

    </xsl:template>


    <xsl:template name="salesFeedHeaderRow">
        <tr class="Table-row Table-row-header">
            <th class="Table-col feedCol">Sales Feed</th>
            <th class="Table-col nameCol">Name</th>
            <th class="Table-col currencyCol">Reporting Currency</th>
            <th class="Table-col statusCol">Status</th>
            <th class="Table-col startDateCol">Start Date</th>
            <th class="Table-col updatedCol">Last Updated</th>
            <th class="Table-col editCol">Edit</th>
        </tr>
    </xsl:template>


    <xsl:template name="salesFeedRow">
        <tr class="Table-row Table-row-data" id="feed{SaleFeedID}ReadOnly">
            <td class="Table-col mainRow">
                <xsl:value-of select="Name" />
            </td>
            <td class="Table-col mainRow" id="feed{SaleFeedID}CustomNameReadOnly">
                <xsl:value-of select="CustomName" />
            </td>
            <td class="Table-col mainRow">
                <xsl:value-of select="CurrencyCode" />
            </td>
            <td class="Table-col mainRow" id="feed{SaleFeedID}StatusReadOnly">
                <xsl:if test="Status = 'pending'">Pending</xsl:if>
                <xsl:if test="Status = 'active'">Active</xsl:if>
                <xsl:if test="Status = 'inactive'">Inactive</xsl:if>
            </td>
            <td class="Table-col mainRow" id="feed{SaleFeedID}StartDateReadOnly">
                <xsl:value-of select="StartDate" />
            </td>
            <td class="Table-col mainRow" id="feed{SaleFeedID}DateModifiedReadOnly">
                <xsl:value-of select="DateModified" />
            </td>
            <td class="Table-col mainRow">
                <a title="Edit Service">
                    <img src="/production/images/icons/office/Edit-(16x16).gif" height="16" width="16" id="feed{SaleFeedID}EditIcon" />
                </a>
            </td>
        </tr>
    </xsl:template>


    <xsl:template name="salesFeedEditRow">
        <tr class="hidden Table-row Table-row-data" id="feed{SaleFeedID}">
            <td class="Table-col mainRow">
                <input type="hidden" name="feed{SaleFeedID}SaleFeedServiceID" id="feed{SaleFeedID}SaleFeedServiceID" value="{SaleFeedServiceID}" />
                <input type="hidden" name="feed{SaleFeedID}FeedName" id="feed{SaleFeedID}FeedName" value="{Name}" />
                <xsl:value-of select="Name" />
            </td>
            <td class="Table-col mainRow">
                <input type="text" name="feed{SaleFeedID}CustomName" id="feed{SaleFeedID}CustomName" value="{CustomName}" />
            </td>
            <td class="Table-col mainRow">
                <input type="hidden" name="feed{SaleFeedID}CurrencyCode" id="feed{SaleFeedID}CurrencyCode" value="{CurrencyCode}" />
                <xsl:value-of select="CurrencyCode" />
            </td>
            <td class="Table-col mainRow">
                <select name="feed{SaleFeedID}Status" id="feed{SaleFeedID}Status" onChange="checkFeedStatus(this.value, 'feed{SaleFeedID}')" >
                    <option value="pending">Pending</option>
                    <option value="active">
                        <xsl:if test="Status='active'">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        Active
                    </option>
                    <option value="inactive">
                        <xsl:if test="Status='inactive'">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        Inactive
                    </option>
                </select>
            </td>
            <td class="Table-col mainRow">
                <input type="text" class="dateField" name="feed{SaleFeedID}StartDate" id="feed{SaleFeedID}StartDate" value="{StartDate}" />

                <a title="Select Date Sent" id="feed{SaleFeedID}CalendarLink">
                    <img src="/production/images/icons/office/Calendar-(16x16).gif" height="16" width="16" class="calendarIcon" id="feed{SaleFeedID}CalendarIcon"/>
                </a>

                <img src="/production/images/icons/office/Calendar-Disabled-(16x16).gif" height="16" width="16" class="calendarIcon" id="feed{SaleFeedID}CalendarDisabledIcon"/>

                <xsl:call-template name="calendar">
            	    <xsl:with-param name="fieldID">feed<xsl:value-of select="SaleFeedID" />StartDate</xsl:with-param>
            	    <xsl:with-param name="buttonID">feed<xsl:value-of select="SaleFeedID" />CalendarIcon</xsl:with-param>
                </xsl:call-template>

                <script type="text/javascript">
                    checkFeedStatus('<xsl:value-of select="Status" />', 'feed<xsl:value-of select="SaleFeedID" />');
                </script>

            </td>
            <td class="Table-col mainRow" id="feed{SaleFeedID}DateModified">
                <xsl:value-of select="DateModified" />
            </td>
            <td class="Table-col mainRow">
                <table class="buttons">
                    <tr>
                        <td class="defaultButton">
                            <input type="button" value="Save" onClick="saveFeedChanges(this, 'feed{SaleFeedID}');" />
                        </td>

                        <td>
                            <input type="button" value="Cancel" id="feed{SaleFeedID}Cancel" />

                            <script type="text/javascript">
                                $(document).ready(function() {
                                    $("#feed<xsl:value-of select="SaleFeedID" />Cancel").on('click', function()
                                    {
                                        $("#feed<xsl:value-of select="SaleFeedID" />ReadOnly").show();
                                        $("#feed<xsl:value-of select="SaleFeedID" />").hide();
                                    });
                                });
                            </script>

                        </td>

                    </tr>
                </table>
            </td>
        </tr>
    </xsl:template>


	<xsl:template name="submitButton">
		<xsl:attribute name="class" />
	</xsl:template>


	<xsl:template name="cancelButton">
		<input type="button" name="Back" value="Back to Admin" onClick="location.href='/bookpub/admin'" />
	</xsl:template>


	<xsl:template name="passwordPlaceholder">
	    <xsl:param name="currentCount" />
	    <xsl:param name="characterCount" />
	    <xsl:text>*</xsl:text>
	    <xsl:if test="$currentCount &lt; $characterCount">
            <xsl:call-template name="passwordPlaceholder">
                <xsl:with-param name="currentCount" select="$currentCount + 1" />
                <xsl:with-param name="characterCount" select="$characterCount" />
            </xsl:call-template>
	    </xsl:if>
	</xsl:template>


  <xsl:template name="serviceCountryCodeArrays">

    <script type="text/javascript">

      var countryCodesForService = new Object();

      <xsl:for-each select="ServiceList/Service">

        <xsl:variable name="serviceID" select="ServiceID" />

        <xsl:if test="/Root/SaleFeedServiceURLList/SaleFeedServiceURL/ServiceID = $serviceID">
          var countryCodeList = new Object();
          countryCodeList[0] = 'Choose...';

          <xsl:for-each select="/Root/SaleFeedServiceURLList/SaleFeedServiceURL[ServiceID = $serviceID]">
            countryCodeList[<xsl:value-of select="position()" />] = '<xsl:value-of select="CountryCode" />';
          </xsl:for-each>

          countryCodesForService[<xsl:value-of select="$serviceID" />] = countryCodeList;

        </xsl:if>

      </xsl:for-each>

    </script>

  </xsl:template>


</xsl:stylesheet>
