$(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"),
            amount: button.data("amount"),
            useCash: button.data("use-cash"),
        };

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

        if (data.method === 'put') {
            $("#name").val(data.name);
            $("#amount").val(data.amount);
            $(`input[name="use_cash"][value="${data.useCash}"]`).prop("checked", true);
        }
    });

    $('#createModal').on('hidden.bs.modal', function () {
        const method = $("#form-method").val();
        if (method === 'put') {
            $("#name").val('');
            $("#amount").val('');
            $('input[name="use_cash"]').prop("checked", false);
        }
    });
});
