@page
@model WorkFlowCheck.Web.Pages.BaseStock.Equipments.EquipmentEditPageModel
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
    ViewData["Title"] = "Berendezés";
    var EquipmentId = Model.EquipmentDTO.Id;
    var urlPost = Url.Page("./EquipmentEditPage", "Save");
}
<h1>@ViewData["Title"]</h1>

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

<div class="card shadow p-4">
    <form method="post" id="EquipmentForm">
        <meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
        <input type="hidden" asp-for="EquipmentDTO.Id" />
        <div class="mb-3">
            <label asp-for="EquipmentDTO.ShortName"></label>
            <input asp-for="EquipmentDTO.ShortName" class="form-control" />
            <span asp-validation-for="EquipmentDTO.ShortName" class="text-danger"></span>
        </div>
        <div class="mb-3">
            <label asp-for="EquipmentDTO.EquipmentNumber"></label>
            <input asp-for="EquipmentDTO.EquipmentNumber" class="form-control" />
            <span asp-validation-for="EquipmentDTO.EquipmentNumber" class="text-danger"></span>
        </div>
        <hr class="mt-4 mb-3 border-secondary">
        <div class="mb-3 d-flex justify-content-between">
            <button type="button" id="saveEquipment" 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>

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

            console.log(formData.Equipment);

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

        });

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

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