<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/appuser/templates/global.xsl" />

    <xsl:template name="pickLayout">
        <script src="/production/javascript/jquery/jquery-3.6.0.min.js"></script>
        <xsl:choose>
            <xsl:when test="$cmd = 'auth' and (Params/c = 'auth' or Params/c = 'authc')">
                <xsl:call-template name="authLayout" /> <!-- display MFA input form -->
            </xsl:when>
            <xsl:when test="$cmd = 'auth' and Params/c = 'success'">
                <xsl:call-template name="successLayout" /> <!-- display MFA success page -->
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="authLayout">
        <script type="text/javascript">
        x = document.getElementsByClassName("Layout-header-title");
        x[0].innerText="Verify Your Identity";
        </script>

        <!-- xsl:variable name="email"><xsl:value-of select="/Root/Email" /></xsl:variable -->
        <xsl:variable name="email"><xsl:value-of select="/Root/Email" /></xsl:variable>
        <xsl:variable name="emailPlain"><xsl:value-of select="/Root/EmailPlain" /></xsl:variable>
        <xsl:variable name="mfatype"><xsl:value-of select="/Root/MFAType" /></xsl:variable>
        <xsl:variable name="phone"><xsl:value-of select="/Root/Params/phone" /></xsl:variable>
        <xsl:choose>
            <xsl:when test="/Root/MFAType = 0">
                <h2>ERROR: no MFA type defined</h2>
            </xsl:when>

            <!-- XXX xsl:when test="/Root/MFAType = 1 and not(/Root/Messages/Message/Code)">
                <br/>An authentication code was sent to<br/><h3><xsl:value-of select="$email" /></h3>
            </xsl:when-->

            <xsl:when test="/Root/MFAType = 2">
                <h2>Check your authentication app for your code.</h2>
            </xsl:when>

            <xsl:when test="/Root/MFAType = 3 and /Root/Params/phone != ''">
                <br/>An authentication code was sent to <br/><h3><xsl:value-of select="$phone" /></h3>
            </xsl:when>
        </xsl:choose>

        <form id="login" name="login" action="/rps/login?token={/Params/token}" method="post">
            <input type="hidden" name="submit" value="1" />
            <input type="hidden" name="c" value="{/Root/Params/c}" />
            <input type="hidden" name="qr" value="{/Root/QRPath}" />
            <input type="hidden" name="save" value="{/Root/Save}" />
            <input type="hidden" id="Form_MFAType" name="Form_MFAType" value="{/Root/MFAType}" />

            <input type="hidden" class="Input Form-input" id="Form_Email" name="Form_Email" value="{$emailPlain}" placeholder="" />
            <input type="hidden" name="redirect">
                <xsl:attribute name="value">
                    <xsl:choose>
                        <xsl:when test="normalize-space(Params/redirect)">
                            <xsl:value-of select="Params/redirect" />
                        </xsl:when>
                        <xsl:otherwise></xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </input>
            <script src="/production/javascript/validateLogin.js?v=1" type="text/javascript"></script>

            <xsl:choose>
                <xsl:when test="/Root/Form/*[@e = 'failed_logins']">
                    <xsl:call-template name="uiWarning">
                        <xsl:with-param name="type" select="'error'" />
                        <xsl:with-param name="className" select="'Login-general-error'" />
                        <xsl:with-param name="description">
                            <xsl:text>Too many failed logins.</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="/Root//*[@e]">
                    <xsl:call-template name="uiWarning">
                        <xsl:with-param name="type" select="'error'" />
                        <xsl:with-param name="className" select="'Login-general-error'" />
                        <xsl:with-param name="description">
                            <xsl:text>Invalid Verification Code</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                </xsl:otherwise>
            </xsl:choose>

            <fieldset>
                <div class="Form-item">
                    <label class="Form-label">
                        <div class="Form-input-wrapper">
                            <input type="hidden" id="Form_Token" name="Form_Token" value="{Params/token}" readonly='true' />
                            <div class="Form-error-text-wrapper">
                                <span class="Form-error" id="ERRORForm_Email"></span>
                            </div>
                        </div>
                    </label>
                </div>

                <div class="Form-item">
                    <label class="Form-label">
                        <span class="Form-label-caption">Authentication Code</span>
                        <div class="Form-input-wrapper">
                            <input type="text" class="Input Form-input" id="Form_Password" name="Form_Password" placeholder="Enter Code" />
                            <div class="Form-error-text-wrapper">
                                <span class="Form-error" id="ERRORForm_Password"></span>
                            </div>
                        </div>
                    </label>
                    <br/><input type="checkbox" id="Form_Remember" name="remember" value="1"/>
                    <label> Remember this device for 30 days.</label>
                </div>
            </fieldset>

            <fieldset class="Form-fieldset Form-fieldset-buttons">
                <xsl:call-template name="uiButton">
                    <xsl:with-param name="content" select="'Verify'" />
                    <xsl:with-param name="type" select="'primary'" />
                    <xsl:with-param name="className" select="'Form-button'" />
                    <xsl:with-param name="elemType" select="'submit'" />
                </xsl:call-template>
            </fieldset>

        </form>

<xsl:if test="/Root/MFAType = 3">
        If you did not receive a code, click <a href="/rps/login?c=tcode&amp;email={$email}&amp;token={Params/token}&amp;phone={$phone}">here</a> to resend it.<br/>
</xsl:if>
<xsl:if test="/Root/MFAType = 2 and /Root/MFAVerified = 1">
        Click <a href="/rps/login?c=config&amp;email={$email}&amp;token={Params/token}&amp;sms=1">here</a> to
        revert back to authentication via text message.<br/>
</xsl:if>
Click <a href="/rps/login?c=help">here</a> for additional assistance.


        <script type="text/javascript">
           // initialize when document is fully loaded
           $(function() {
             document.getElementById('Form_Password').addEventListener('keydown', function process(event) {
               const keyCode = event.keyCode;
               const excludedKeys = [8, 13, 32, 39, 45, 46, 189, 127];
               if ( !((keyCode &gt;= 48 &amp;&amp; keyCode &lt;= 57) ||
                    (keyCode &gt;= 96 &amp;&amp; keyCode &lt;= 105) ||
                    (excludedKeys.includes(keyCode)))) {
                 event.preventDefault();
               }
             });
           });
        </script>

        <script type="text/javascript">document.forms["login"].elements["Form_Password"].focus();</script>

    </xsl:template>

    <xsl:template name="successLayout">
        <script type="text/javascript">
        x = document.getElementsByClassName("Layout-header-title");
        x[0].innerText="Success!";
        </script>

        <xsl:variable name="email"><xsl:value-of select="/Root/Email" /></xsl:variable>
        <xsl:variable name="emailPlain"><xsl:value-of select="/Root/EmailPlain" /></xsl:variable>
        <xsl:variable name="mfatype"><xsl:value-of select="/Root/MFAType" /></xsl:variable>
        <xsl:variable name="phone"><xsl:value-of select="/Root/Params/phone" /></xsl:variable>

        <br/>You have been authenticated for the RoyaltyShare application.
        <p>If you prefer to use an authenticator app instead of text, then click
        <a href="/rps/login?c=qrcode&amp;email={$email}&amp;token={/Root/Params/token}">here</a>.
        Otherwise, click Next to proceed to the main dashboard.</p>

        <xsl:choose>
            <xsl:when test="/Root/MFAType = 0">
                <h2>ERROR: no MFA type defined</h2>
            </xsl:when>

            <!-- XXX xsl:when test="/Root/MFAType = 1 and not(/Root/Messages/Message/Code)">
                <br/>An authentication code was sent to<br/><h3><xsl:value-of select="$email" /></h3>
            </xsl:when-->

            <xsl:when test="/Root/MFAType = 2">
                <h2>Check your authentication app for your code.</h2>
            </xsl:when>

            <xsl:when test="/Root/MFAType = 3 and /Root/Params/phone != ''">
                <br/>An authentication code was sent to <br/><h3><xsl:value-of select="$phone" /></h3>
            </xsl:when>
        </xsl:choose>

        <form id="login" name="login" action="/rps/login?token={/Params/token}" method="post">
            <input type="hidden" name="submit" value="1" />
            <input type="hidden" name="c" value="success" />
            <input type="hidden" name="qr" value="{/Root/QRPath}" />
            <input type="hidden" id="Form_MFAType" name="Form_MFAType" value="{/Root/MFAType}" />

            <input type="hidden" class="Input Form-input" id="Form_Email" name="Form_Email" value="{$emailPlain}" placeholder="" />
            <input type="hidden" class="Input Form-input" id="Form_Password" name="Form_Password" placeholder="Enter Code" />
            <input type="hidden" id="Form_Token" name="Form_Token" value="{Params/token}" readonly='true' />
            <input type="hidden" name="redirect">
                <xsl:attribute name="value">
                    <xsl:choose>
                        <xsl:when test="normalize-space(Params/redirect)">
                            <xsl:value-of select="Params/redirect" />
                        </xsl:when>
                        <xsl:otherwise></xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </input>
            <script src="/production/javascript/validateLogin.js?v=1" type="text/javascript"></script>

            <xsl:choose>
                <xsl:when test="/Root/Form/*[@e = 'failed_logins']">
                    <xsl:call-template name="uiWarning">
                        <xsl:with-param name="type" select="'error'" />
                        <xsl:with-param name="className" select="'Login-general-error'" />
                        <xsl:with-param name="description">
                            <xsl:text>Too many failed logins.</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="/Root//*[@e]">
                    <xsl:call-template name="uiWarning">
                        <xsl:with-param name="type" select="'error'" />
                        <xsl:with-param name="className" select="'Login-general-error'" />
                        <xsl:with-param name="description">
                            <xsl:text>Invalid Verification Code</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                </xsl:otherwise>
            </xsl:choose>


            <fieldset class="Form-fieldset Form-fieldset-buttons">
                <xsl:call-template name="uiButton">
                    <xsl:with-param name="content" select="'Next'" />
                    <xsl:with-param name="type" select="'primary'" />
                    <xsl:with-param name="className" select="'Form-button'" />
                    <xsl:with-param name="elemType" select="'submit'" />
                </xsl:call-template>
            </fieldset>

        </form>
<!-- XXX
        <xsl:choose>
        <xsl:when test="/Root/MFAType = 1">
            If you did not receive a code, click <a href="/rps/login?c=scode&amp;email={$email}&amp;token={Params/token}">here</a> to resend it.<br/>
        </xsl:when>
        <xsl:otherwise>
            If you don't have your authenticator app, click <a href="/rps/login?c=scode&amp;email={$email}&amp;token={Params/token}">here</a> to receive a code via email.<br/>
        </xsl:otherwise>
        </xsl:choose>
        If you did not receive a code, click <a href="/rps/login?c=tcode&amp;email={$email}&amp;token={Params/token}&amp;phone={$phone}">here</a> to resend it.<br/>
<xsl:if test="/Root/MFAVerified = 1">
        Click <a href="/rps/login?c=config&amp;email={$email}&amp;token={Params/token}">here</a> to choose another authentication method.<br/>
</xsl:if>
-->

        <script type="text/javascript">document.forms["login"].elements["Form_Password"].focus();</script>

    </xsl:template>
</xsl:stylesheet>
