@page
@model WorkFlowCheck.Web.Pages.CheckList.CheckListRow.CheckListRowEditPageModel
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    ViewData["Title"] = "Ellenőrzési pont";
    var checkListrowId = Model.CheckListRow.Id;
    var url = Url.Page("./CheckListRowEditPage", "LoadCheckListRows", new { id = checkListrowId });
    var urlPost = Url.Page("./CheckListRowEditPage", "Save");
}
<h1>@ViewData["Title"]</h1>

<input type="hidden" id="checkListRowEditUrl" value="@url" />
<input type="hidden" id="checkListRowEditPostUrl" value="@urlPost" />

<div class="card shadow p-4">
    <form method="post" id="checkListRowForm">
        <meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
        <input type="hidden" asp-for="CheckListRow.Id" />
        <input type="hidden" asp-for="CheckListRow.CheckListHeaderId" />
        <input type="hidden" asp-for="CheckListRow.GuidNumber" />
        <input type="hidden" asp-for="CheckListRow.CheckListTemplateRowId" />
        <div class="row mb-3">
            <label class="form-label">Leírás</label>
            <div class="form-control" style="white-space: pre-wrap;">
                @Model.CheckListRow.CheckListTemplateRowDTO?.OperationDescription
            </div>
        </div>

        <div class="row mb-3">
            <label asp-for="CheckListRow.Answer"></label>
            <input asp-for="CheckListRow.Answer" class="form-control" />
            <span asp-validation-for="CheckListRow.Answer" class="text-danger"></span>
        </div>
        <div class="row mb-3">
            <label asp-for="CheckListRow.Photo"></label>

            @if (Model.CheckListRow.Photo != null && Model.CheckListRow.Photo.Length > 0)
            {
                var base64Image = $"data:image/jpeg;base64,{Convert.ToBase64String(Model.CheckListRow.Photo)}";
                <img src="@base64Image" width="150" height="100" style="object-fit: cover; display: block; margin-bottom: 10px;" />
            }

            <input asp-for="CheckListRow.Photo" type="file" class="form-control" />
            <span asp-validation-for="CheckListRow.Photo" class="text-danger"></span>
        </div>

        <div class="mb-3">
            <button type="button" id="saveCheckListRow" 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>

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

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

        });

    </script>
}
