@page
@model WorkFlowCheck.Web.Pages.Account.LoginModel
@using WorkFlowCheck.Common.Helper
@using WorkFlowCheck.Common.DTO
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    Layout = null;
    ViewData["Title"] = "Bejelentkezés";
}



<!DOCTYPE html>
<html lang="hu">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>@ViewData["Title"]</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
@* <body class="bg-light d-flex justify-content-center align-items-center vh-100"> *@
<body class="bg-light d-flex flex-column min-vh-100">
    <main class="flex-grow-1 d-flex justify-content-center align-items-center">
    <div class="card shadow p-4" style="min-width: 350px; max-width: 400px; width: 100%;">
        <h2 class="mb-4 text-center">Bejelentkezés</h2>

        <div id="errorMessageContainer" class="alert alert-danger" style="display:none;"></div>

        <form method="post">
            <meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
            <input type="hidden" asp-for="User2FADTO.Token2FA" />
            <div class="mb-3">
                <label asp-for="User2FADTO.UserName" class="form-label">Felhasználónév</label>
                <input asp-for="User2FADTO.UserName" class="form-control" />
                <span asp-validation-for="User2FADTO.UserName" class="text-danger small"></span>
            </div>

            <div class="mb-3">
                <label asp-for="User2FADTO.Password" class="form-label">Jelszó</label>
                <input asp-for="User2FADTO.Password" type="password" class="form-control" />
                <span asp-validation-for="User2FADTO.Password" class="text-danger small"></span>
            </div>
            <div id="VerificationCode" class="mb-3">
                <label asp-for="User2FADTO.Code" class="form-label">Megerősítő kód</label>
                <input asp-for="User2FADTO.Code" class="form-control" />
                <span asp-validation-for="User2FADTO.Code" class="text-danger small"></span>
            </div>
            <hr class="mt-4 mb-3 border-secondary">
            <div id="login2F1" class="d-grid">
                <button id="Login2F1Btn" type="button" class="btn btn-primary">Bejelentkezés</button>
            </div>
            <div id="login2F2" class="d-grid">
                <button id="Login2F2Btn" type="button" class="btn btn-primary">Megerősítés</button>
            </div>
        </form>
    </div>
    </main>
    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; @(DateTime.Now.Year) - Workflow Check App - Version @(SystemHelper.ProgramVersion)
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery-validation-unobtrusive@4.0.0/dist/jquery.validate.unobtrusive.min.js"></script>

    <script>
        $(document).ready(function () {
            $('#VerificationCode').hide();
            $('#login2F2').hide();
            $('#Login2F2Btn').hide();

            $('#Login2F1Btn').on('click', function () {
                // $('#Login2F1Btn, #Login2F2Btn').prop('disabled', true);
                $('#errorMessageContainer').hide().text('');
                const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
                const userName = $('#User2FADTO_UserName').val();
                const password = $('#User2FADTO_Password').val();

               $.ajax({
                   url: '/Account/Login?handler=Login2F1',
                   type: 'POST',
                   headers: {
                           'X-CSRF-TOKEN': csrfToken,
                           'Content-Type': 'application/json'
                   },
                   contentType: 'application/json',
                   data: JSON.stringify({
                       UserName: userName,
                       Password: password
                   }),
                   success: function (result) {
                       if (result.success) {
                           $('#login2F1').hide();
                           $('#Login2F1Btn').hide();
                           $('#VerificationCode').show();
                           $('#login2F2').show();
                           $('#Login2F2Btn').show();
                           $('#User2FADTO_Token2FA').val(result.data)
                       } else {
                           $('#errorMessageContainer').text('Hibás felhasználónév vagy jelszó!').show();
                               // $('#Login2F1Btn, #Login2F2Btn')
                               //  .prop('disabled', false)
                               //  .removeAttr('disabled')
                               //  .removeClass('disabled');
                       }
                   },
                   error: function () {
                       $('#errorMessageContainer').text('Hiba történt a kérés során.').show();
                           // $('#Login2F1Btn, #Login2F2Btn')
                           //  .prop('disabled', false)
                           //  .removeAttr('disabled')
                           //  .removeClass('disabled');
                   }
               });
               
            });

            $('#Login2F2Btn').on('click', function () {
                // $('#Login2F1Btn, #Login2F2Btn').prop('disabled', true);
                $('#errorMessageContainer').hide().text('');
                const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
                
                const userName = $('#User2FADTO_UserName').val();
                const password = $('#User2FADTO_Password').val();
                const code = $('#User2FADTO_Code').val();
                const token2FA = $('#User2FADTO_Token2FA').val();

               $.ajax({
                   url: '/Account/Login?handler=Login2F2',
                   type: 'POST',
                   headers: {
                           'X-CSRF-TOKEN': csrfToken,
                           'Content-Type': 'application/json'
                   },
                   contentType: 'application/json',
                   data: JSON.stringify({
                       UserName: userName,
                       Password: password,
                       Code: code,
                       Token2FA: token2FA
                   }),
                   success: function (result) {
                       if (result.success) {
                           window.location.href = `/Index`;
                       } else {
                           $('#errorMessageContainer').text('Hibás felhasználónév vagy jelszó!').show();
                               // $('#Login2F1Btn, #Login2F2Btn')
                               //  .prop('disabled', false)
                               //  .removeAttr('disabled')
                               //  .removeClass('disabled');
                       }
                   },
                   error: function (xhr) {
                        console.log(xhr.status);
                        console.log(xhr.responseText);
                        $('#errorMessageContainer').text('Hiba történt a kérés során.').show();
                            // $('#Login2F1Btn, #Login2F2Btn')
                            // .prop('disabled', false)
                            // .removeAttr('disabled')
                            // .removeClass('disabled');
                   }
               });
            });

            $('input').on('input', function () {
                $('#errorMessageContainer').hide().text('');
            });
        });
    </script>


    <partial name="_ValidationScriptsPartial" />
</body>
</html>


