@page
@model WorkFlowCheck.Web.Pages.BaseStock.CheckPoints.CheckPointEditPageModel
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    ViewData["Title"] = "Ellenőrzési pont";
    var checkpointId = Model.CheckPoint.Id;
    var url = Url.Page("./CheckPointEditPage", "LoadCheckListTemplateRows", new { id = checkpointId });
    var urlPost = Url.Page("./CheckPointEditPage", "Save");
}
<h1>@ViewData["Title"]</h1>

<input type="hidden" id="checkPointEditUrl" value="@url" />
<input type="hidden" id="checkPointEditPostUrl" value="@urlPost" />

<div class="card shadow p-4">
    <form method="post" id="checkPointForm">
        <meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
        <input type="hidden" asp-for="CheckPoint.Id" />
        <div class="mb-3">
            <label asp-for="CheckPoint.ShortName"></label>
            <input asp-for="CheckPoint.ShortName" class="form-control" />
            <span asp-validation-for="CheckPoint.ShortName" class="text-danger"></span>
        </div>
        <div class="mb-3">
            <label asp-for="CheckPoint.Code"></label>
            <input asp-for="CheckPoint.Code" class="form-control" />
            <span asp-validation-for="CheckPoint.Code" class="text-danger"></span>
        </div>
        <div class="mb-3">
            <button type="button" id="saveCheckPoint" class="btn btn-primary">Mentés</button>
        </div>
    </form>
</div>

<div class="card shadow p-4">
    <div class="d-flex justify-content-end">
        <button class="btn btn-primary float-right">Új elem hozzáadása</button>
    </div>
    <table id="tbCheckPointsEditPage" class="table table-bordered table-hover table-sm" style="width:100%">
        <thead class="table-primary">
            <tr>
                <th>ID</th>
                <th>RowIndex</th>
                <th>OperationDescription</th>
                <th>AnswerType</th>
                <th class="text-center">Action</th>
            </tr>
        </thead>
        <tfoot class="table-light">
            <tr>
                <th>ID</th>
                <th>RowIndex</th>
                <th>OperationDescription</th>
                <th>AnswerType</th>
                <th class="text-center">Action</th>
            </tr>
        </tfoot>
    </table>
</div>

@section Scripts {
    <script>

        const table = new DataTable('#tbCheckPointsEditPage', {
                ajax: {
                       url: $('#checkPointEditUrl').val(),
                       type: "GET",
                       dataSrc : "data"
                },
                columns: [
                    { data: "id" },
                    { data: "rowIndex" },
                    { data: "operationDescription" },
                    { data: "answerType" },
                    { data: null, render: function (data, type, row) {
                        return `<button class="btn btn-primary edit-btn" data-id="${row.id}">Módosítás</button>
                                <button class="btn btn-danger delete-btn" data-id="${row.id}">Törlés</button>
                               `;
                    }}
                ],
                columnDefs: [
                    {
                        "targets": 0,
                        "visible": false
                    },
                    {
                        "targets": 4,
                        "className": "text-center",
                        "width": "15%"
                    }
                ],

                processing:true
        });
        
        $('#tbCheckPointsEditPage').on('click', '.edit-btn', function ()
        {
            const row = table.row($(this).closest('tr')).data();
            window.location.href = `@Url.Page("./CheckListTemplateRowEditPage")?id=${row.id}`;
        });
        
        $('#tbCheckPointsEditPage').on('click', '.delete-btn', function ()
        {
            const row = table.row($(this).closest('tr')).data();
            showConfirmModal({
                title: 'Törlés megerősítése',
                message: 'Biztosan törölni szeretnéd ezt az elemet?',
                okText: 'Törlés',
                cancelText: 'Mégsem'
            }).then(function(result) {
                if(result === 'ok') {
                    console.log('Törlés végrehajtva');
                }
            });
        });
        
        $("#saveCheckPoint").click(function () {
            const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
            var formData = getFormAsNestedObject('#checkPointForm');

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

        });

    </script>
}
