@page
@model WorkFlowCheck.Web.Pages.UserAndRole.RoleEditPageModel
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    ViewData["Title"] = "Szabály szerkesztése";
    var RoleId = Model.RoleDTO.Id;
    var urlPost = Url.Page("./RoleEditPage", "Save");
}
<h1>@ViewData["Title"]</h1>

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

<div class="card shadow p-4">
    <form method="post" id="RoleForm">
        <meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
        <input type="hidden" asp-for="RoleDTO.Id" />
        <div class="col-md-3">
            <label asp-for="RoleDTO.ParentId"></label>
            <select asp-for="RoleDTO.ParentId" class="form-control" asp-items="Model.Parents"></select>
            <span asp-validation-for="RoleDTO.ParentId" class="text-danger"></span>
        </div>
        <div class="mb-3">
            <label asp-for="RoleDTO.RoleName"></label>
            <input asp-for="RoleDTO.RoleName" class="form-control" />
            <span asp-validation-for="RoleDTO.RoleName" class="text-danger"></span>
        </div>
        <div class="row">
            <div class="col-2">
                <div class="mb-3">
                    <label asp-for="RoleDTO.IsAdmin"></label>
                    <input type="checkbox" asp-for="RoleDTO.IsAdmin" class="form-check-input" />
                    <span asp-validation-for="RoleDTO.IsAdmin" class="text-danger"></span>
                </div>
            </div>
            <div class="col-2">
                <div class="mb-3">
                    <label asp-for="RoleDTO.CanDownloadAPK"></label>
                    <input type="checkbox" asp-for="RoleDTO.CanDownloadAPK" class="form-check-input" />
                    <span asp-validation-for="RoleDTO.CanDownloadAPK" class="text-danger"></span>
                </div>
            </div>
            <div class="col-4">
                <div class="mb-3">
                    <label asp-for="RoleDTO.CanEnableBlocked"></label>
                    <input type="checkbox" asp-for="RoleDTO.CanEnableBlocked" class="form-check-input" />
                    <span asp-validation-for="RoleDTO.CanEnableBlocked" class="text-danger"></span>
                </div>
            </div>
            <div class="col-2">
                <div class="mb-3">
                    <label asp-for="RoleDTO.CanUseMobilApp"></label>
                    <input type="checkbox" asp-for="RoleDTO.CanUseMobilApp" class="form-check-input" />
                    <span asp-validation-for="RoleDTO.CanUseMobilApp" class="text-danger"></span>
                </div>
            </div>
            <div class="col-2">
                <div class="mb-3">
                    <label asp-for="RoleDTO.CanUseWebAdmin"></label>
                    <input type="checkbox" asp-for="RoleDTO.CanUseWebAdmin" class="form-check-input" />
                    <span asp-validation-for="RoleDTO.CanUseWebAdmin" class="text-danger"></span>
                </div>
            </div>
        </div>
        <hr class="mt-4 mb-3 border-secondary">
        <div class="mb-3 d-flex justify-content-between">
            <button type="button" id="saveRole" 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>

        $("#saveRole").click(function () {
            const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
            var formData = getFormAsNestedObject('#RoleForm');
            const $form = $('#RoleForm');
            formData.RoleDTO.IsAdmin = $('#RoleForm input[name="RoleDTO.IsAdmin"]').is(':checked');
            formData.RoleDTO.CanDownloadAPK = $('#RoleForm input[name="RoleDTO.CanDownloadAPK"]').is(':checked');
            formData.RoleDTO.CanEnableBlocked = $('#RoleForm input[name="RoleDTO.CanEnableBlocked"]').is(':checked');
            formData.RoleDTO.CanUseMobilApp = $('#RoleForm input[name="RoleDTO.CanUseMobilApp"]').is(':checked');
            formData.RoleDTO.CanUseWebAdmin = $('#RoleForm input[name="RoleDTO.CanUseWebAdmin"]').is(':checked');

            if ($form.valid())
            {
                saveEntity(csrfToken, formData.RoleDTO, $('#RoleEditPostUrl').val());
            } else
            {
                $form[0].reportValidity();
            }

        });

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

            $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>
}
