@page
@model WorkFlowCheck.Web.Pages.UserAndRole.UserEditPageModel
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    ViewData["Title"] = "Ellenőrzési pont";
    var UserId = Model.User.Id;
    var urlPost = Url.Page("./UserEditPage", "Save");
}
<h1>@ViewData["Title"]</h1>

<input type="hidden" id="UserEditPostUrl" value="@urlPost" />

<div class="card shadow p-4">
    <form method="post" id="UserForm">
        <meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
        <input type="hidden" asp-for="User.Id" />
        <div class="mb-3">
            <label asp-for="User.UserName"></label>
            <input asp-for="User.UserName" class="form-control" />
            <span asp-validation-for="User.UserName" class="text-danger"></span>
        </div>
        <div class="mb-3">
            <label asp-for="User.FirstName"></label>
            <input asp-for="User.FirstName" class="form-control" />
            <span asp-validation-for="User.FirstName" class="text-danger"></span>
        </div>
        <div class="mb-3">
            <label asp-for="User.LastName"></label>
            <input asp-for="User.LastName" class="form-control" />
            <span asp-validation-for="User.LastName" class="text-danger"></span>
        </div>
        <div class="mb-3 d-flex justify-content-between">
            <button type="button" id="saveUser" class="btn btn-primary">Mentés</button>
            <button type="button" class="btn btn-secondary" onclick="history.back()">Mégsem</button>
        </div>
    </form>
</div>

@section Scripts {
    <script>

        $("#saveUser").click(function () {
            const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
            var formData = getFormAsNestedObject('#UserForm');
            const $form = $('#UserForm');

            if ($form.valid())
            {
                $.ajax({
                    url: $('#UserEditPostUrl').val(),
                    type: 'POST',
                    headers: {
                          'X-CSRF-TOKEN': csrfToken,
                          'Content-Type': 'application/json'
                    },
                    data: JSON.stringify(formData.User),
                    success: function (response) {
                        showMessageModal({
                            title: 'Figyelmem!',
                            message: 'Sikeres mentés.',
                            okText: 'Értettem'
                        });
                    },
                    error: function (xhr, status, error) {
                        // Hiba esetén
                        console.error("Hiba: ", error);
                        showMessageModal({
                            title: 'Hiba!',
                            message: 'A mentés NEM sikerült!',
                            okText: 'Értettem'
                        });
                    }
                });
            } else
            {
                $form[0].reportValidity();
            }

        });
                
        $(document).ready(function () {
            const $form = $("#userForm");

            $form.validate({
                errorClass: "text-danger small",
                errorPlacement: function (error, element) {
                    error.appendTo(element.parent());
                }
            });

            // $form.on("submit", function (e) {
            //     if (!$form.valid()) {
            //         e.preventDefault(); // Ne küldje be, ha van hiba
            //     }
            // });
        });
    </script>
}