@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@{
    var userLanguage = config.GetServerLanguage();
    var openRegistrationEnabled = config.GetServerOpenRegistration();
}
@model string
@{
    ViewData["Title"] = "Register";
}
@section Scripts {
    <script src="~/js/login.js?v=@StaticHelper.VersionNumber"></script>
}
<div class="container d-flex align-items-center justify-content-center" style="height:100vh">
    <div>
        <div style="max-width:204px;">
            <img src="@config.GetLogoUrl()" class="lubelogger-logo" />
            <div class="form-group">
                <label for="inputToken">@translator.Translate(userLanguage, "Token")</label>
                @if (openRegistrationEnabled)
                {
                    <div class="input-group">
                        <input type="text" id="inputToken" class="form-control">
                        <div class="input-group-text">
                            <button type="button" class="btn btn-sm text-secondary password-visible-button" onclick="sendOpenIdRegistrationToken()"><i class="bi bi-send"></i></button>
                        </div>
                    </div>
                }
                else
                {
                    <input type="text" id="inputToken" class="form-control">
                }
            </div>
            <div class="form-group">
                <label for="inputUserName">@translator.Translate(userLanguage, "Username")</label>
                <input type="text" id="inputUserName" class="form-control" value="@Model" onkeyup="callBackOnEnter(event, performOpenIdRegistration)">
            </div>
            <div class="d-grid">
                <button type="button" class="btn btn-warning mt-2" onclick="performOpenIdRegistration()"><i class="bi bi-box-arrow-in-right me-2"></i>@translator.Translate(userLanguage, "Register")</button>
            </div>
            <div class="d-grid">
                <a href="/Login/Index" class="btn btn-link mt-2">@translator.Translate(userLanguage, "Back to Login")</a>
            </div>
        </div>
    </div>
</div>
<script>
    function performOpenIdRegistration() {
        var token = $("#inputToken").val();
        var userName = $("#inputUserName").val();
        var userEmail = decodeHTMLEntities('@Model');
        $.post('/Login/RegisterOpenIdUser', { userName: userName, token: token, emailAddress: userEmail }, function (data) {
            if (data.success) {
                successToast(data.message);
                setTimeout(function () { window.location.href = '/Home' }, 500);
            } else {
                errorToast(data.message);
            }
        });
    }
    function sendOpenIdRegistrationToken(){
        var userEmail = decodeHTMLEntities('@Model');
        $.post('/Login/SendRegistrationToken', { emailAddress: userEmail }, function (data) {
            if (data.success) {
                successToast(data.message);
            } else {
                errorToast(data.message);
            }
        });
    }
</script>