<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--  $Id$  -->
<!--                                                                  -->
<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:include href="shared.xsl" />


    <xsl:template name="pickLayout">
        <div class="MainBaseSection">
            <xsl:call-template name="publisherShow" />
            <xsl:call-template name="buttons" />
        </div>
    </xsl:template>


    <xsl:template name="publisherShow">
        <div class="PayeeView" id="publisherShow">
            <xsl:call-template name="helpLink" />
            <xsl:call-template name="successMessageGeneral">
                <xsl:with-param name="successMessage">Publisher successfully updated!</xsl:with-param>
            </xsl:call-template>

            <script src="/production/javascript/jquery/jquery-3.6.0.min.js"></script>
            <link href="/production/css/portal.css" media="screen" rel="stylesheet" type="text/css" />
            <div id="confirmResend" title="Resend Invite">
                <form id="resend_form" action="/rps/portal/resend" method="post">
                    <input type="hidden" name="resend" value="1"/>
                    <input type="hidden" name="payeeID" value="{Form/Publisher/PublisherID}"/>
                    <input type="hidden" name="payeeType" value="2"/>
                    <input type="hidden" id="payeeRecipientFlex" name="payeeRecipientID" value=""/>
                </form>
            </div>
            <script>
                function failResend() {
                    var popHTML = '<p class="Modal-paragraph">Unable to resend invite.</p>';
                    popHTML += '<p class="Modal-paragraph">You can only resend an invite once per day. Please try again later.</p>';
                    jsInlinePop(
                        popHTML,
                        "Okay",
                        'hideInlinePop();',
                        '',
                        '$(\'input[name="mainButton"]\').remove();',
                        'Resend Invite'
                    );
                }
                function confirmResend() {
                    var recipient = document.getElementById('payeeRecipientFlex');
                    recipient.setAttribute('value', 0);
                    var popHTML = '<p class="Modal-paragraph">Are you sure you want to resend an invite to this contact?</p>';
                    jsInlinePop(popHTML,"Resend",
                        <xsl:choose>
                            <xsl:when test="Form/Publisher/CanInvite = '1'">
                                "$('#resend_form').submit();"
                            </xsl:when>
                            <xsl:otherwise>
                                "failResend();"
                            </xsl:otherwise>
                        </xsl:choose>,
                        null,
                        null,
                        'Resend Invite'
                    );
                }
                $(function() {
                    $("#viewConfirm").on('click', function() {
                        confirmResend();
                        return false;
                    });
                });
                function confirmRecipientResend(recipientID, canResend) {
                    if ( !canResend ) {
                        failResend();
                        return;
                    }
                    var flex = document.getElementById('payeeRecipientFlex');
                    flex.setAttribute('value', recipientID);
                    var popHTML = '<p class="Modal-paragraph">Are you sure you want to resend an invite to this contact?</p>';
                    jsInlinePop(popHTML,"Resend", "$('#resend_form').submit();",null,null,'Resend Invite');
                    return 1;
                }
            </script>

            <section class="PageShow-section PayeeShow-section">
                <h2><xsl:value-of select="$publisherType" /> Publisher</h2>
                <div class="PageShow-linked-publishers">
                    <xsl:call-template name="linkedPublishers" />
                </div>
                <div class="InformationCols">
                    <div class="InformationCols-item">
                        <xsl:call-template name="column1Show" />
                    </div>
                    <div class="InformationCols-item" style="width:66.6666%">
                        <xsl:call-template name="column2Show" />
                    </div>
                </div>
                <div class="PageShow-last-modified">
                    <p class="HelpText">
                        Last modified <xsl:value-of select="Form/Publisher/ModifiedDate" /><xsl:if test="normalize-space(Form/Publisher/ModifiedBy)"> by <xsl:value-of select="Form/Publisher/ModifiedBy" /></xsl:if>
                    </p>
                </div>
            </section>
            <xsl:if test="Form/PayorList/Payor">
                <xsl:call-template name="payorList" />
            </xsl:if>
        </div>
    </xsl:template>


    <xsl:template name="column1Show">
        <div class="ValuePair">

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Publisher'" />
                <xsl:with-param name="value">
                    <xsl:value-of select="Form/Publisher/PublisherName" />
                    <xsl:if test="/Root/Form/Publisher/IsAgency != 0">
                        <xsl:text> [Agent]</xsl:text>
                    </xsl:if>
                </xsl:with-param>
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Client Account #'" />
                <xsl:with-param name="value" select="Form/Publisher/ClientAccountID" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Status'" />
                <xsl:with-param name="value">
                    <xsl:choose>
                        <xsl:when test="Form/Publisher/Status = 0">Inactive</xsl:when>
                        <xsl:when test="Form/Publisher/Status = 2">On-hold</xsl:when>
                        <xsl:otherwise>Active</xsl:otherwise>
                    </xsl:choose>
                </xsl:with-param>
            </xsl:call-template>
        </div>
        <div class="ValuePair">
            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Address 1'" />
                <xsl:with-param name="value" select="Form/Publisher/StreetAddress" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Address 2'" />
                <xsl:with-param name="value" select="Form/Publisher/StreetAddress2" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Address 3'" />
                <xsl:with-param name="value" select="Form/Publisher/StreetAddress3" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'City'" />
                <xsl:with-param name="value" select="Form/Publisher/City" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'State/Province'" />
                <xsl:with-param name="value" select="Form/Publisher/StateProvince" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'ZIP/Postal'" />
                <xsl:with-param name="value" select="Form/Publisher/PostalCode" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Country'" />
                <xsl:with-param name="value" select="Form/Publisher/CountryCode" />
            </xsl:call-template>
        </div>
        <div class="ValuePair">
            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'E-mail'" />
                <xsl:with-param name="value" select="Form/Publisher/Email" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Phone'" />
                <xsl:with-param name="value" select="Form/Publisher/PhoneNumber" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Fax'" />
                <xsl:with-param name="value" select="Form/Publisher/FaxNumber" />
            </xsl:call-template>
        </div>
    </xsl:template>

    <xsl:template name="column2Show">
        <div class="ValuePair">
            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Publisher Type'" />
                <xsl:with-param name="value">
                    <xsl:choose>
                        <xsl:when test="Form/Publisher/IsAgency = 1">Agency</xsl:when>
                        <xsl:when test="Form/Publisher/IsAdmin = 1">Administrator</xsl:when>
                        <xsl:otherwise>Standard</xsl:otherwise>
                    </xsl:choose>
                </xsl:with-param>
            </xsl:call-template>

            <xsl:if test="/Root/Form/Publisher/IsAdmin = 0">
                <xsl:call-template name="uiValuePairItem">
                    <xsl:with-param name="label" select="'Admin'" />
                    <xsl:with-param name="value" select="Form/AdminList/Admin[PublisherID = /Root/Form/Publisher/AdminID]/PublisherName" />
                </xsl:call-template>
            </xsl:if>

            <xsl:if test="/Root/Form/Publisher/IsAgency = 0">

                <xsl:call-template name="uiValuePairItem">
                    <xsl:with-param name="label" select="'Agent'" />
                    <xsl:with-param name="value" select="Form/AgentList/Agent[AgentID = /Root/Form/Publisher/AgentID]/Name" />
                </xsl:call-template>

            </xsl:if>
        </div>
        <div class="ValuePair">
            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Tax ID'" />
                <xsl:with-param name="value" select="Form/Publisher/TaxID" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Affiliation'" />
                <xsl:with-param name="value" select="/Root/Form/AffiliateList/Affiliate[AffiliateID = /Root/Form/Publisher/AffiliateID]/Name" />
            </xsl:call-template>

            <xsl:call-template name="uiValuePairItem">
                <xsl:with-param name="label" select="'Comments'" />
                <xsl:with-param name="value" select="Form/Publisher/Comments" />
                <xsl:with-param name="replaceLineBreak" select="'true'" />
            </xsl:call-template>
        </div>

        <!-- XXX FEATURE_FLAG XXX -->
        <xsl:if test="ClientTypes/ClientType[ClientTypeID = 11]/IsClientTypeFlag = 1">
            <xsl:if test="Area != 'ca_publisher'">
                <div class="ValuePair">
                    <xsl:call-template name="uiValuePairItem">
                        <xsl:with-param name="label" select="'Statement Distribution'" />
                        <xsl:with-param name="value">
                            <xsl:choose>
                                <xsl:when test="Form/Publisher/Distribution = 0">Manual</xsl:when>
                                <xsl:when test="Form/Publisher/Distribution = 1">Online Through Payee Portal</xsl:when>
                                <xsl:otherwise></xsl:otherwise>
                            </xsl:choose>
                        </xsl:with-param>
                    </xsl:call-template>

                    <xsl:if test="Form/Publisher/Distribution = 1">
                        <div class="ValuePair-item">
                            <div class="ValuePair-label">Statement Distribution Status</div>
                            <div class="ValuePair-value">
                                <xsl:choose>
                                    <xsl:when test="Form/Publisher/DateConfirmed = ''">
                                        <i>Waiting on confirmation.</i>&#xA0;&#xA0;<a href="#" id="viewConfirm">Resend</a><br/>
                                    </xsl:when>
                                    <xsl:otherwise>Confirmed <xsl:value-of select="Form/Publisher/DateConfirmed" /></xsl:otherwise>
                                </xsl:choose>
                            </div>
                        </div>
                    </xsl:if>
                </div>
            </xsl:if>
        </xsl:if>

        <xsl:if test="Form/Publisher/Distribution = 1">
            <xsl:if test="Area != 'ca_publisher'">
                <xsl:call-template name="renderAdditionalRecipientsBlock" />
            </xsl:if>
        </xsl:if>

    </xsl:template>

    <xsl:template name="payorList">
        <section class="PageShow-section" id="payorViewTable">
            <h2>Payors</h2>
            <xsl:call-template name="payorListTable" />
        </section>
    </xsl:template>

    <xsl:template name="payorListTable">
        <xsl:variable name="accountCurrencyCode">
            <xsl:choose>
                <xsl:when test="$area = 'publisher'">USD</xsl:when>
                <xsl:when test="$area = 'ca_publisher'">CAD</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="Client/Locale/CurrencyFormat/CurrencyCode" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <table class="Table">
            <thead>
                <tr class="Table-row Table-row-header">
                    <th class="Table-col">Payor</th>
                    <th class="Table-col">License Admin</th>

                    <!-- show CMRRA manufacturer data if this is an agency -->
                    <xsl:if test="$area = 'ca_publisher'">
                        <xsl:choose>
                            <xsl:when test="Form/Publisher/IsAgency = 1">
                                <th class="Table-col">CMRRA Manufacturer ID</th>
                                <th class="Table-col">Manufacturer Name</th>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:if>

                    <th class="Table-col Table-col-amount">License Count</th>
                    <th class="Table-col Table-col-amount">Minimum Payment (<xsl:value-of select="$accountCurrencyCode" />)</th>
                    <th class="Table-col Table-col-amount">
                        <span class="Table-col-wrapper">
                            Pending Transactions (<xsl:value-of select="$accountCurrencyCode" />)
                        </span>
                        <xsl:call-template name="helpIcon">
                            <xsl:with-param name="tip">Sum of all non-committed transactions</xsl:with-param>
                        </xsl:call-template>
                    </th>
                    <th class="Table-col Table-col-icon">Edit</th>
                </tr>
            </thead>
            <tbody>
                <xsl:choose>
                    <xsl:when test="Form/Publisher/IsAgency = 1">

                        <xsl:for-each select="Form/PayorList/Payor[Direct = 1]">
                            <xsl:call-template name="directRow" />
                        </xsl:for-each>

                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:for-each select="Form/PayorList/Payor[Direct = 1 or HasAccount &gt; 0 or LicenseCount &gt; 0 or normalize-space(/Root/Form/PublisherAgentID) or normalize-space(/Root/Form/Publisher/AdminID)]">
                            <!-- direct -->
                            <xsl:if test="Direct = 1">
                                <xsl:call-template name="directRow" />
                            </xsl:if>

                            <!-- indirect -->
                            <xsl:if test="Indirect = 1">
                                <xsl:call-template name="indirectRow" />
                            </xsl:if>

                        </xsl:for-each>
                    </xsl:otherwise>
                </xsl:choose>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template name="directRow">
        <xsl:variable name="payorID" select="PayorID" />
        <tr class="Table-row Table-row-data">
            <td class="Table-col">
                <xsl:value-of select="Name" />
            </td>
            <td class="Table-col">
                <xsl:choose>
                    <xsl:when test="/Root/Form/Publisher/IsAgency = 1">Agency</xsl:when>
                    <xsl:when test="/Root/Form/Publisher/IsAdmin = 1">Administrator</xsl:when>
                    <xsl:otherwise>Direct</xsl:otherwise>
                </xsl:choose>
            </td>

            <!-- show CMRRA manufacturer data if this is an agency -->
            <xsl:if test="$area = 'ca_publisher'">
                <xsl:choose>
                    <xsl:when test="/Root/Form/Publisher/IsAgency = 1">
                        <td class="Table-col"><xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/ManufacturerID" /></td>
                        <td class="Table-col"><xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/ManufacturerName" /></td>
                    </xsl:when>
                </xsl:choose>
            </xsl:if>

            <td class="Table-col Table-col-amount">
                <xsl:choose>
                    <xsl:when test="LicenseCount &gt; 0">
                        <a>
                            <xsl:attribute name="href">
                                <xsl:choose>
                                    <xsl:when test="$publisherDirect = 1">?c=list_licenses&amp;publisherID=<xsl:value-of select="/Root/Params/PublisherID" />&amp;payorID=<xsl:value-of select="$payorID" /></xsl:when>
                                    <xsl:otherwise>?c=list_direct_licenses&amp;publisherID=<xsl:value-of select="/Root/Params/PublisherID" />&amp;payorID=<xsl:value-of select="$payorID" /></xsl:otherwise>
                                </xsl:choose>
                            </xsl:attribute>
                            <xsl:value-of select="LicenseCount" />
                        </a>
                    </xsl:when>
                    <xsl:otherwise>0</xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col Table-col-amount">
                <xsl:if test="/Root/Form/Publisher/Account[PayorID = $payorID]">
                    <xsl:if test="normalize-space(/Root/Form/Publisher/Account[PayorID = $payorID]/MinPayment)"><xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/FinanceAccount/Balance/@currency_symbol" /></xsl:if>
                    <xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/MinPayment" />
                </xsl:if>
            </td>
            <td class="Table-col Table-col-amount">
                <xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/FinanceAccount/PendingBalance" />
            </td>
            <td class="Table-col Table-col-icon">
                <xsl:call-template name="uiEditTrigger">
                    <xsl:with-param name="url">
                        <xsl:text>?PayorID=</xsl:text>
                        <xsl:value-of select="PayorID" />
                        <xsl:text>&amp;c=edit_direct&amp;PublisherID=</xsl:text>
                        <xsl:value-of select="/Root/Form/Publisher/PublisherID" />
                    </xsl:with-param>
                </xsl:call-template>
            </td>
        </tr>
    </xsl:template>


    <xsl:template name="indirectRow">
        <xsl:variable name="payorID" select="PayorID" />
        <tr class="Table-row Table-row-data">
            <td class="Table-col">
                <xsl:value-of select="Name" />
            </td>
            <td class="Table-col">
                <xsl:choose>
                    <xsl:when test="normalize-space($indirectPublisherType)">
                        <xsl:value-of select="$indirectPublisherType" />
                    </xsl:when>
                    <xsl:otherwise>N/A</xsl:otherwise>
                </xsl:choose>
            </td>

            <!-- show CMRRA manufacturer data if this is an agency -->
            <xsl:if test="$area = 'ca_publisher'">
                <xsl:choose>
                    <xsl:when test="/Root/Form/Publisher/IsAgency = 1">
                        <td class="Table-col"><xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/ManufacturerID" /></td>
                        <td class="Table-col"><xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/ManufacturerName" /></td>
                    </xsl:when>
                </xsl:choose>
            </xsl:if>

            <td class="Table-col Table-col-amount">
                <xsl:choose>
                    <xsl:when test="LicenseCount &gt; 0">
                        <a href="?c=list_indirect_licenses&amp;publisherID={/Root/Params/PublisherID}&amp;payorID={$payorID}"><xsl:value-of select="LicenseCount" /></a>
                    </xsl:when>
                    <xsl:otherwise>0</xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col Table-col-amount">
                <xsl:choose>
                    <xsl:when test="normalize-space($indirectPublisherType)">
                        <i>See <xsl:value-of select="$indirectPublisherType" /></i>
                    </xsl:when>
                    <xsl:otherwise>N/A</xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col Table-col-amount">
                <xsl:value-of select="PendingBalance" />
            </td>
            <td class="Table-col Table-col-icon">

                <xsl:call-template name="uiEditTrigger">
                    <xsl:with-param name="url">
                        <xsl:text>?PayorID=</xsl:text>
                        <xsl:value-of select="PayorID" />
                        <xsl:text>&amp;c=edit_indirect&amp;PublisherID=</xsl:text>
                        <xsl:value-of select="/Root/Form/Publisher/PublisherID" />
                    </xsl:with-param>
                </xsl:call-template>

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

    <xsl:template name="renderAdditionalRecipientsBlock">

        <div class="ValuePair-item" style="border-top: 1px solid #d9d2d0">
            <label class="Form-label">
                <br/>
                <span class="Form-label-caption">Additional Statement Recipients</span>
            </label>
            <xsl:choose>
            <xsl:when test="count(Form/PublisherRecipients) &gt; 0">
                <table id="recipientViewTable">
                    <tr>
                         <th style="padding:0 10px 0 0"><span class="Form-label-caption">Recipient</span></th>
                         <th style="padding:0 10px 0 0"><span class="Form-label-caption">E-mail</span></th>
                         <th style="padding:0 10px 0 0"><span class="Form-label-caption">Statement Distribution Status</span></th>
                    </tr>
                    <xsl:for-each select="Form/PublisherRecipients">
                        <xsl:call-template name="renderRecipientViewTableRow">
                            <xsl:with-param name="i" select="position()" />
                        </xsl:call-template>
                    </xsl:for-each>
                </table>
            </xsl:when>
            <xsl:otherwise>
                <span class="ValuePair-value">-</span>
            </xsl:otherwise>
            </xsl:choose>
        </div>

    </xsl:template>


    <xsl:template name="renderRecipientViewTableRow">

        <xsl:param name="i" select="0"/>
        <tr>
            <td style="padding:0 10px 15px 0">
                <div class="ValuePair-value">
                    <xsl:value-of select="Name" />
                </div>
            </td>
            <td style="padding:0 10px 15px 0">
                <div class="ValuePair-value">
                    <xsl:value-of select="Email" />
                </div>
            </td>
            <td style="padding:0 10px 15px 0">
                <div class="ValuePair-value">
                    <xsl:choose>
                        <xsl:when test="DateConfirmed = ''">
                            <i>Waiting on confirmation.</i>&#160;&#160;
                            <a href="#" id="{concat('viewConfirm_Alt', $i)}" onClick="confirmRecipientResend( {StatementRecipientID}, {translate(number(CanInvite), 'aN', '0')} );return false;">Resend</a>
                        </xsl:when>
                        <xsl:otherwise>Confirmed <xsl:value-of select="DateConfirmed" /></xsl:otherwise>
                    </xsl:choose>
                </div>
            </td>
         </tr>

    </xsl:template>

    <xsl:template name="submitButton">
        <xsl:call-template name="uiButton">
            <xsl:with-param name="type" select="'primary'" />
            <xsl:with-param name="className" select="'Form-button'" />
            <xsl:with-param name="elemType" select="'submit'" />
            <xsl:with-param name="content" select="'Edit Publisher'" />
            <xsl:with-param name="disabled">
                <xsl:if test="$openRuns = 1">
                    <xsl:text>true</xsl:text>
                </xsl:if>
            </xsl:with-param>
            <xsl:with-param name="url">
                <xsl:text>?PublisherID=</xsl:text>
                <xsl:value-of select="Form/Publisher/PublisherID" />
                <xsl:text>&amp;c=edit</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="cancelButton">
        <xsl:call-template name="uiButton">
            <xsl:with-param name="content" select="'Back to Publishers'" />
            <xsl:with-param name="name" select="'Back'" />
            <xsl:with-param name="className" select="'Form-button'" />
            <xsl:with-param name="url" select="$payeeHome" />
        </xsl:call-template>
    </xsl:template>

</xsl:stylesheet>
