$(document).ready(function () {
    $("#createModal").on("show.bs.modal", function (event) {
        const button = $(event.relatedTarget);
        const data = {
            modalTitle: button.data("modal-title"),
            url: button.data("url"),
            method: button.data("method"),

            name: button.data("name"),
            quantity: button.data("quantity"),
            price: button.data("price"),
            description: button.data("description"),
        };

        $(".modal-title").text(data.modalTitle);
        $("#form").attr('action', data.url);
        $("#form-method").val(data.method);

        if (data.method === 'put') {
            $("#name").val(data.name);
            $("#quantity").val(data.quantity);
            $("#price").val(data.price);
            $("#description").val(data.description);
        }
    });

    $('#createModal').on('hidden.bs.modal', function () {
        const method = $("#form-method").val();
        if (method === 'put') {
            $("#name").val('');
            $("#quantity").val('');
            $("#price").val('');
            $("#description").val('');
        }
    });
});
