{% extends 'template.html' %}

{% block title %}Login{% endblock %}

{% block header %}
<script>
    function login() {
        let ajaxRes = $.ajax({
            url: '/v1/terrareg/auth/admin/login',
            contentType: 'application/json',
            dataType: 'json',
            method: 'post',
            headers: {
                'Accept': 'application/json',
                'X-Terrareg-ApiKey': $('#admin_token_input').val()
            },
            statusCode: {
                200: () => {
                    // Obtain redirect URL from query string
                    const queryString = window.location.search;
                    const urlParams = new URLSearchParams(queryString);
                    const redirectUrl = urlParams.get('redirect');
                    // If redirect has been provided and is relative (starts with /),
                    // use this
                    if (redirectUrl && redirectUrl.indexOf('/') === 0) {
                        location.href = redirectUrl;
                    } else {
                        // Redirect to homepage
                        location.href = '/';
                    }
                },
                401: () => {
                    // Show login error
                    $('#login_error').css('display', 'block');
                    $('#login_error').html('Incorrect admin token');                    
                }
            }
        });
    }

    getConfig().then((config) => {
        let authenticationEnabled = false;

        if (config.ADMIN_LOGIN_ENABLED) {
            authenticationEnabled = true;
            $('#admin-login').removeClass('default-hidden');
        }
        if (config.OPENID_CONNECT_ENABLED || config.SAML_ENABLED || config.PROVIDER_SOURCES.length) {
            authenticationEnabled = true;

            $('#sso-login').removeClass('default-hidden');

            if (config.OPENID_CONNECT_ENABLED) {
                $('#openid-connect-login').text(config.OPENID_CONNECT_LOGIN_TEXT);
                $('#openid-connect-login').removeClass('default-hidden');
            }
            if (config.SAML_ENABLED) {
                $('#saml-login').text(config.SAML_LOGIN_TEXT);
                $('#saml-login').removeClass('default-hidden');
            }
            config.PROVIDER_SOURCES.forEach((providerSource) => {
                $('#sso-login').append($(`<a id="${providerSource.api_name}-login" class="button" href="/${providerSource.api_name}/login">${providerSource.login_button_text}</a>`))
            });
        }

        if (authenticationEnabled === false) {
            $('#no-authentication-methods-warning').removeClass('default-hidden');
        }
        // Show title to indicate that processing has completed for tests
        $('#login-title').removeClass('default-hidden');
    });
</script>
{% endblock %}

{% block content %}

<nav class="breadcrumb" aria-label="breadcrumbs">
    <ul>
        <li class="is-active"><a href="/login">Login</a></li>
    </ul>
</nav>

<nav class="level">
    <div class="level-item">
        <div class="field is-vertical">
            <h1 id="login-title" class="title is-3 default-hidden">Login</h1>

            <div id="login_error" style="display: none" class="notification is-danger">
            </div>

            <h1 id="no-authentication-methods-warning" class="is-3 default-hidden">
                Login is not available as there are no authentication methods configured
            </h1>

            <section id="admin-login" class="section default-hidden">
                <form onsubmit="login(); event.preventDefault();">
                    <div class="field">
                        <label class="label">Admin Token</label>
                        <div class="control">
                            <input id="admin_token_input" class="input" type="password" placeholder="Admin Token">
                        </div>
                    </div>

                    <div class="field">
                        <div class="control">
                            <button id="login-button" class="button is-link">Login</button>
                        </div>
                    </div>
                </form>
            </section>

            <section id="sso-login" class="section default-hidden">
                <label class="label">Single Sign-On</label>
                <a id="openid-connect-login" class="button default-hidden" href="/openid/login">Login with OpenID Connect</a>
                <a id="saml-login" class="button default-hidden" href="/saml/login?sso">Login with SAML</a>
            </section>
        </div>

    </div>
</nav>

{% endblock %}
