<!--                                                                  -->
<!--  Copyright (C) 2009 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="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="canDelete">
        <xsl:choose>
            <xsl:when test="/Root/WatchList/Dynamic = 1">0</xsl:when>
            <xsl:when test="/Root/WatchList/IsOwner = 1">1</xsl:when>
            <xsl:when test="/Root/User/AccessLevel = 1">1</xsl:when>
            <xsl:when test="/Root/User/AccessLevel = 2">1</xsl:when>
            <xsl:when test="/Root/User/AccessLevel = 3">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="canEdit">
        <xsl:choose>
            <xsl:when test="normalize-space(/Root/WatchList/GlobalID)">0</xsl:when>
            <xsl:when test="/Root/WatchList/IsOwner = 1">1</xsl:when>
            <xsl:when test="/Root/User/AccessLevel = 1">1</xsl:when>
            <xsl:when test="/Root/User/AccessLevel = 2">1</xsl:when>
            <xsl:when test="/Root/User/AccessLevel = 3">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>


    <xsl:template name="pickLayout">

        <link href="/production/css/bookpub/market.css?v=3" rel="stylesheet" type="text/css" />
        <script src="/production/javascript/jquery/jquery-3.6.0.min.js"></script>

        <div id="marketMain">
            <xsl:if test="/Root/Access/bookpub/market/report_full = 1">
                <xsl:call-template name="serviceAlerts" />
            </xsl:if>
            <xsl:call-template name="watchList" />
        </div>
    </xsl:template>


    <xsl:template name="serviceAlerts">
        <div class="section">
            <div class="serviceAlerts">
                <xsl:choose>
                    <xsl:when test="ActiveAlertCount &gt; 0">
                        <img src="/production/images/icons/office/Warning-(24x24).gif" />
                        Service alerts have been triggered for your catalog!
                    </xsl:when>
                    <xsl:otherwise>
                        <img src="/production/images/icons/office/Checkmark-(24x24).gif" />
                        You have no active service alerts for your catalog.
                    </xsl:otherwise>
                </xsl:choose>
                <a href="?c=report_full">View Report</a>
                <p>
                    Unauthorized advertised prices, physical products priced higher than digital products, unavailable products, and lack of cover art can trigger a service alert. View the detailed report for more information.
                </p>
            </div>
        </div>
    </xsl:template>


    <xsl:template name="watchList">
        <div class="section">

            <xsl:if test="Messages/Message/Code = 'delete_success'">
                <xsl:call-template name="successMessageGeneral">
                    <xsl:with-param name="successMessage">Watch List successfully deleted!</xsl:with-param>
                </xsl:call-template>
            </xsl:if>


            <xsl:if test="WatchList">

                <xsl:choose>
                    <xsl:when test="WatchList[Public = 1] or $canEdit = 1">
                        <div class="watchListTitle">
                            <h1><xsl:value-of select="WatchList/Name" /></h1>

                            <xsl:if test="normalize-space(WatchList/Description)">
                                <xsl:value-of select="WatchList/Description" />
                                <br />
                            </xsl:if>

                            <xsl:if test="WatchList/Dynamic = 1">
                                <xsl:if test="normalize-space(WatchList/Description)"><br /></xsl:if>
                                <i>This list is Dynamic and is automatically updated every day.</i>
                            </xsl:if>

                        </div>

                        <div class="watchListOptions">
                            <xsl:call-template name="watchListOptions" />
                        </div>

                        <br class="superBreak" />


                        <xsl:call-template name="watchListEditLinks" />

                        <xsl:if test="/Root/WatchList/WatchListItemList/Pagination/NumPages &gt; 1">
                            <xsl:call-template name="numericListNav">
                                <xsl:with-param name="pagination">/Root/WatchList/WatchListItemList/Pagination</xsl:with-param>
                            </xsl:call-template>
                        </xsl:if>

                        <xsl:call-template name="watchListTable" />
                    </xsl:when>
                    <xsl:otherwise>
                        <p class="errorGeneral messageBox">
                            You do not have access to this watch list.
                        </p>
                        <a href="/bookpub/market?WatchListID=0">Click here</a> to return to the market tab.
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>

            <xsl:call-template name="watchListEditLinks" />
        </div>
    </xsl:template>


    <xsl:template name="watchListEditLinks">
        <div class="watchListEditLinks">
            <xsl:if test="$canEdit = 1">
                <a href="?c=edit&amp;WatchListID={WatchList/WatchListID}" class="editLink">Edit This List</a>
            </xsl:if>
            <p class="addLink">
	    	    <a href="?c=edit" title="Create New Watch List" id="addImageLink">
	    		    <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" /> Create New Watch List
	    		</a>
   		    </p>
        </div>
    </xsl:template>


    <xsl:template name="watchListOptions">
        <form method="get" action="/bookpub/market">
            <input type="hidden" name="c" value="main" />
            <strong>Now Viewing: </strong>
            <select name="WatchListID">
                <xsl:for-each select="WatchListList/WatchList[IsOwner = 1 or Public = 1]">
                    <xsl:variable name="watchListID" select="WatchListID" />
                    <option value="{WatchListID}">
                        <xsl:if test="/Root/WatchList/WatchListID = $watchListID">
                            <xsl:attribute name="selected">true</xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="Name" />
                    </option>
                </xsl:for-each>
            </select>
            with
            <select name="pageSize">
                <option value="25">
                    25
                </option>
                <option value="50">
                    <xsl:if test="PageSize = '50'">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    50
                </option>
                <option value="100">
                    <xsl:if test="PageSize = '100'">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                    </xsl:if>
                    100
                </option>
            </select>
            per page
            <input type="submit" value="Update" />
        </form>
    </xsl:template>


    <xsl:template name="watchListTable">

        <table class="textChart watchList">
            <xsl:if test="not(WatchList/WatchListItemList/WatchListItem)">
                <xsl:attribute name="id">emptyList</xsl:attribute>
            </xsl:if>
            <tr>
                <th class="rankCol">
                    <xsl:variable name="marketID" select="WatchList/Market" />
                    <xsl:variable name="marketName">
                        <xsl:choose>
                            <xsl:when test="$marketID and $marketID != 0 and WatchList/Dynamic = 1">
                                <xsl:if test="$marketID = 8">Amazon</xsl:if>
                                <xsl:if test="$marketID = 16">Barnes &amp; Noble</xsl:if>
                                <xsl:if test="$marketID = 60">NYT Best Sellers</xsl:if>
                            </xsl:when>
                            <xsl:otherwise>Average</xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>
                    <xsl:value-of select="$marketName" /> Rank
                </th>
                <th>Title</th>
                <th>Author</th>
                <th>Imprint</th>
                <th>Format</th>
                <th class="center">Pub Date</th>
                <th class="center">Price</th>
                <th class="alertCol"></th>
                <xsl:if test="$canDelete = 1">
                    <th class="deleteCol"></th>
                </xsl:if>
            </tr>

            <xsl:choose>
                <xsl:when test="WatchList/WatchListItemList/WatchListItem">
                    <xsl:for-each select="WatchList/WatchListItemList/WatchListItem">
                        <xsl:call-template name="watchListItemRow" />
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="emptyList" />
                </xsl:otherwise>
            </xsl:choose>

        </table>
    </xsl:template>


    <xsl:template name="watchListItemRow">
        <tr class="row{position() mod 2}" id="item{WatchListItemID}">
            <td class="rankCol">
                <xsl:choose>
                    <xsl:when test="normalize-space(/Root/WatchList/Market) and /Root/WatchList/Market != 0 and /Root/WatchList/Dynamic = 1">
                        <xsl:value-of select="ServiceRank" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="AverageRank" />
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td>
                <a href="/bookpub/catalog?c=show_book_product&amp;ProductID={Product/ProductID}">
                    <xsl:value-of select="Book/Title" />
                </a>
            </td>
            <td>
                <xsl:value-of select="Book/AuthorName" />
            </td>
            <td>
                <xsl:variable name="imprintID" select="Product/ImprintID" />
                <xsl:value-of select="/Root/ImprintList/Imprint[ImprintID = $imprintID]/Name" />
            </td>
            <td>
                <xsl:value-of select="Product/ProductFormat/BookFormatType/Description" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="Product/ProductFormat/BookFormatSubtype/Description" />
            </td>
            <td class="center">
                <xsl:value-of select="Product/ReleaseDate" />
            </td>
            <td class="center">
                <xsl:variable name="primaryCurrency" select="Product/DefaultPrimary/Currency/CurrencyCode" />
                <xsl:value-of select="Product/Pricing/Market/Currency[CurrencyCode = $primaryCurrency]/Prices/ProductMarketPrice[Status = 'current']/Price" />
            </td>
            <td class="icon">
                <xsl:choose>
                    <xsl:when test="AlertCount &gt; 0">
                        <a href="?c=report&amp;productID={Product/ProductID}" title="View Alert Report">
                            <img src="/production/images/icons/office/Warning-(16x16).gif" />
                        </a>
                    </xsl:when>
                    <xsl:otherwise>
                        <img src="/production/images/icons/office/Checkmark-(16x16).png" />
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <xsl:if test="$canDelete = 1">
                <td class="icon">
                    <a title="Delete from Watch List" href="javascript:void(0)">
                        <img src="/production/images/icons/office/Cancel-Grey-(16x16).png" style="margin-top: 1px"  id="delete{WatchListItemID}" />
                    </a>
                </td>

                <script type="text/javascript">
                    $(document).ready(function() {
                        $("#delete<xsl:value-of select="WatchListItemID" />").on('click', function()
                        {
                            $.ajax({
                              url: '/bookpub/market?c=delete_item&amp;WatchListItemID=<xsl:value-of select="WatchListItemID" />',
                              dataType: 'json',
                              error: function() { alert("error"); },
                              success: function()
                              {
                                $("#item<xsl:value-of select="WatchListItemID" />").hide();
                                $("#deletedItem<xsl:value-of select="WatchListItemID" />").show();
                              }
                            });

                        });
                    });
                </script>

            </xsl:if>
        </tr>
        <xsl:if test="$canDelete = 1">
            <tr class="deletedItem hidden" id="deletedItem{WatchListItemID}">
                <td></td>
                <td>
                    <b>
                        <xsl:value-of select="Book/Title" />
                    </b>
                </td>
                <td></td>
                <td colspan="3">
                    This product has been removed.
                </td>
                <td colspan="3" class="undoCell">
                    <a href="javascript:void(0)" id="undoDelete{WatchListItemID}">Undo</a>


                    <script type="text/javascript">
                        $(document).ready(function() {
                            $("#undoDelete<xsl:value-of select="WatchListItemID" />").on('click', function()
                            {
                                $.ajax({
                                  url: '/bookpub/market?c=delete_item&amp;Undo=1&amp;WatchListItemID=<xsl:value-of select="WatchListItemID" />',
                                  dataType: 'json',
                                  error: function() { alert("error"); },
                                  success: function()
                                  {
                                    $("#item<xsl:value-of select="WatchListItemID" />").show();
                                    $("#deletedItem<xsl:value-of select="WatchListItemID" />").hide();
                                  }
                                });
                            });
                        });
                    </script>

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


    <xsl:template name="emptyList">
        <xsl:variable name="colspan">
            <xsl:choose>
                <xsl:when test="$canDelete = 1">9</xsl:when>
                <xsl:otherwise>8</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <tr class="row1">
            <td colspan="{$colspan}"></td>
        </tr>

        <xsl:if test="WatchList/Dynamic = 0">

            <tr class="row0">
                <td colspan="{$colspan}">
                     You have not added any books to this list.
                </td>
            </tr>

            <tr class="row1">
                <td colspan="{$colspan}">
                     Search your catalog to add books and book products.
                </td>
            </tr>

        </xsl:if>

        <xsl:if test="WatchList/Dynamic = 1">

            <tr class="row0">
                <td colspan="{$colspan}">
                     No book products currently meet your list criteria
                </td>
            </tr>

            <tr class="row1">
                <td colspan="{$colspan}">
                </td>
            </tr>

        </xsl:if>

        <tr class="row0">
            <td colspan="{$colspan}"></td>
        </tr>

        <tr class="row1">
            <td colspan="{$colspan}"></td>
        </tr>

        <tr class="row0">
            <td colspan="{$colspan}"></td>
        </tr>

        <tr class="row1">
            <td colspan="{$colspan}"></td>
        </tr>

        <tr class="row0">
            <td colspan="{$colspan}"></td>
        </tr>

        <tr class="row1">
            <td colspan="{$colspan}"></td>
        </tr>

        <tr class="row0">
            <td colspan="{$colspan}"></td>
        </tr>
    </xsl:template>


</xsl:stylesheet>
