@page
@model WorkFlowCheck.Web.Pages.CheckList.CheckListHeader.CheckListHeaderPageModel
@{
    ViewData["Title"] = "Ellenőrzési sablonok";
}
<h1>@ViewData["Title"]</h1>

<div class="card shadow p-4">
    <div class="d-flex justify-content-end">
        <button id="newCheckListHeaderBtn" class="btn btn-primary float-right new-btn" data-bs-toggle="tooltip" data-bs-placement="top" title="Új elem hozzáadása">
            <i class="bi bi-plus-square"></i>
        </button>
    </div>
    <table id="tbCheckListHeadersPage" class="table table-bordered table-hover table-sm" style="width:100%">
        <thead class="table-primary">
            <tr>
                <th>ID</th>
                <th>IsEditable</th>
                <th>DocumentNumber</th>
                <th>DateExecution</th>
                <th>ShortName</th>
                <th>Description</th>
                <th>User</th>
                <th class="text-center">Action</th>
            </tr>
        </thead>
        <tfoot class="table-light">
            <tr>
                <th>ID</th>
                <th>IsEditable</th>
                <th>DocumentNumber</th>
                <th>DateExecution</th>
                <th>ShortName</th>
                <th>Description</th>
                <th>User</th>
                <th class="text-center">Action</th>
            </tr>
        </tfoot>
    </table>
</div>

@section Scripts {
    <script>
        const table = new DataTable('#tbCheckListHeadersPage', {
                ajax: {
                        url: "@Url.Page("./CheckListHeaderPage", "LoadCheckListHeaders")",
                        type: "GET",
                        dataSrc : "data"
                },
                columns: [
                    { data: "id" },
                    {
                            data: "isEditable",
                            searchable: false,
                            sortable: false,
                            // visible: (aDurchgang==1 ? true : false),
                            className: "text-center",
                            render: function ( data, type, row ) {
                                if (data === true) {
                                    return '<input type="checkbox" class="editor-active" disabled checked>';
                                }
                                if (data === false) {
                                    return '<input type="checkbox" class="editor-active" disabled>';
                                }
                                return data;
                            }

                    },
                    { data: "documentNumber"},
                    { data: "dateExecution",
                            render: function (data, type, row) {
                                        if (!data) return '';
                                        if (type === 'display' || type === 'filter') {
                                            return dayjs(data).format('YYYY.MM.DD');
                                        }
                                        return data;
                                    }
                    },
                    { data: "shortName" },
                    { data: "description" },
                    { data: "userDTO.userName" },
                    { data: null, render: function (data, type, row) {
                         return renderActionButtons(row.id);
                    }}
                ],
                columnDefs: [
                    {
                        "targets": 0,
                        "visible": false
                    },
                    {
                        "targets": 3,
                        "className": "text-center",
                        "width": "10%"
                    }
                ],

                processing:true,
                language: { url: '//cdn.datatables.net/plug-ins/2.2.2/i18n/hu.json',}
        });

        $('#newCheckListHeaderBtn').on('click', function ()
        {
            console.log('New button clicked!"');
            window.location.href = `@Url.Page("./CheckListHeaderEditPage")?id=0`;
        });

        $('#tbCheckListHeadersPage').on('click', '.edit-btn', function ()
        {
            const row = table.row($(this).closest('tr')).data();
            window.location.href = `@Url.Page("./CheckListHeaderEditPage")?id=${row.id}`;
        });

        $('#tbCheckListHeadersPage').on('click', '.delete-btn', function ()
        {
            const row = table.row($(this).closest('tr')).data();
            console.log(row);
            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');
                }
            });
        });
    </script>
}
