@page
@model WorkFlowCheck.Web.Pages.CheckListTemplate.CheckListTemplateRows.CheckListTemplateRowEditPageModel
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    ViewData["Title"] = "Ellenőrzési pont";
    var checklisttemplaterowId = Model.CheckListTemplateRow.Id;
    var url = Url.Page("./CheckListTemplateRowEditPage", "LoadCheckListTemplateRows", new { id = checklisttemplaterowId });
    var urlPost = Url.Page("./CheckListTemplateRowEditPage", "Save");
}
<h1>@ViewData["Title"]</h1>

<input type="hidden" id="checkListTemplateRowEditUrl" value="@url" />
<input type="hidden" id="checkListTemplateRowEditPostUrl" value="@urlPost" />

<div class="card shadow p-4">
    <form method="post" id="checkListTemplateRowForm">
        <meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
        <input type="hidden" asp-for="CheckListTemplateRow.Id" />
        <input type="hidden" asp-for="CheckListTemplateRow.CheckListTemplateHeaderId" />

        <div class="row mb-3">
            <div class="col-md-3">
                <label asp-for="CheckListTemplateRow.RowIndex"></label>
                <input asp-for="CheckListTemplateRow.RowIndex" class="form-control" />
                <span asp-validation-for="CheckListTemplateRow.RowIndex" class="text-danger"></span>
            </div>
            <div class="col-md-3">
                <label asp-for="CheckListTemplateRow.CheckPointId"></label>
                <select asp-for="CheckListTemplateRow.CheckPointId" class="form-control" asp-items="Model.CheckPoints"></select>
                <span asp-validation-for="CheckListTemplateRow.CheckPointId" class="text-danger"></span>
            </div>
            <div class="col-md-3">
                <label asp-for="CheckListTemplateRow.EquipmentId"></label>
                <select asp-for="CheckListTemplateRow.EquipmentId" class="form-control" asp-items="Model.Equipments"></select>
                <span asp-validation-for="CheckListTemplateRow.EquipmentId" class="text-danger"></span>
            </div>
            <div class="col-md-3">
                <label asp-for="CheckListTemplateRow.AnswerType"></label>
                <select asp-for="CheckListTemplateRow.AnswerType" class="form-control" asp-items="Model.AnswerTypes"></select>
                <span asp-validation-for="CheckListTemplateRow.AnswerType" class="text-danger"></span>
            </div>
        </div>

        <div class="mb-3">
            <label asp-for="CheckListTemplateRow.OperationDescription"></label>
            <textarea asp-for="CheckListTemplateRow.OperationDescription" class="form-control" rows="4"></textarea>
            <span asp-validation-for="CheckListTemplateRow.OperationDescription" class="text-danger"></span>
        </div>
        <hr class="mt-4 mb-3 border-secondary">
        <div class="mb-3">
            <button type="button" id="saveCheckListTemplateRow" 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>

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

            $.ajax({
                url: $('#checkListTemplateRowEditPostUrl').val(),
                type: 'POST',
                headers: {
                      'X-CSRF-TOKEN': csrfToken,
                      'Content-Type': 'application/json'
                },
                data: JSON.stringify(formData.CheckListTemplateRow),
                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>
}
