function bindActions() {

    $(".giftcerts .showLog").off("click").on("click", function () {
        var $redeemlog = $(this).closest("td").find(".giftcertlogs").clone();
        $('#additionalInfoModal .modal-body').html($redeemlog);
    });

    $(".giftcerts .showRedeemForm").off("click").on("click", function () {
        var $giftcertcode = $(this).data("giftcertcode");
        var $giftcertvalue = $(this).data("giftcertvalue");

        $('#redeemModal .giftcertcode').val($giftcertcode);
        $('#redeemModal #modalRedeemValue').val($giftcertvalue);

    });

    $(".giftcerts .showDeleteForm").off("click").on("click", function () {
        var $giftcertcode = $(this).data("giftcertcodedelete");
        $('#deleteModal .giftcertcodedelete').val($giftcertcode);

    });

    $("#onlyWithValue").off("change").on("change", function () {
        var formToSend = $(this).closest("form");

        updateFilters(formToSend);
        //
        // var $stoken = formToSend.find('input[name="stoken"]').val();
        // var $adminID = formToSend.find('input[name="admin_sid"]').val();
        // var $shopID = formToSend.find('input[name="shp"]').val();
        //
        // // AJAX CALL TO CHECK VALUES AGAINST SERVICE
        // $.ajax({
        //     url: "/admin/index.php?cl=ys_giftcert__admin_ajax&fnc=changeFilter",
        //     method: "POST",
        //     data: {
        //         onlyWithValue: $onlyWithValue,
        //         stoken: $stoken,
        //         force_admin_sid: $adminID,
        //         shp: $shopID
        //     },
        //     dataType: 'json',
        //     async: true,
        //     context: formToSend,
        //     success: function(result){reloadCertList(result, $stoken, $adminID, false, $shopID);}
        // });
    })

    $("#filterByCode").off("input").on("input", function () {
        var formToSend = $(this).closest("form");
        updateFilters(formToSend);
    })

    document.getElementById('filterByCode').addEventListener('keydown', function (event) {
        if (event.key === 'Enter') {
            event.preventDefault(); // Verhindert z.B. das Absenden eines Formulars
        }
    });

}

function updateFilters(formToSend) {
    var $stoken = formToSend.find('input[name="stoken"]').val();
    var $adminID = formToSend.find('input[name="admin_sid"]').val();
    var $shopID = formToSend.find('input[name="shp"]').val();

    var $filterByCode = $("#filterByCode").val();
    var $resetFilterByCode=0;
    if ($filterByCode == "") {
        $resetFilterByCode=1;
    }

    var $onlyWithValue = 0;
    if ($("#onlyWithValue").prop("checked")) {
        $onlyWithValue = 1;
    }

    $.ajax({
        url: "/admin/index.php?cl=ys_giftcert__admin_ajax&fnc=changeFilter",
        method: "POST",
        data: {
            onlyWithValue: $onlyWithValue,
            filterByCode: $filterByCode,
            resetFilterByCode: $resetFilterByCode,
            stoken: $stoken,
            force_admin_sid: $adminID,
            shp: $shopID
        },
        dataType: 'json',
        async: true,
        context: formToSend,
        success: function(result){reloadCertList(result, $stoken, $adminID, false, $shopID);}
    });
}

function reloadCertList(result, $stoken, $adminID, $modalID, $shopid) {
    if (result.status === "200") {
        $.ajax({
            url: "/admin/index.php?cl=ys_giftcert__admin_logs&stoken=" + $stoken + "&force_admin_sid=" + $adminID+"&shp="+$shopid,
            method: "GET",
            async: true,
            success: function (newdata) {
                // var $certoverview = $(newdata).find('table.giftcerts')
                $('table.giftcerts').replaceWith($(newdata).find('table.giftcerts'));
                $('ul.pagination').replaceWith($(newdata).find('ul.pagination'))
                bindActions();
                if($modalID !== false) {
                    $($modalID).modal('toggle');
                }
                if(result.message) {
                    $('#ys_giftcerts_successbox .message').html(result.message);
                    $('#ys_giftcerts_successbox').show(); //.delay(5000).fadeOut();
                }
                if(result.generatedCode) {
                    $('#giftcert-new-certs-export').show();
                } else {
                    $('#giftcert-new-certs-export').hide();
                }
            }
        });
    } else {
        if(result.message) {
            $('#ys_giftcerts_failbox .message').html(result.message)
        }
        $('#ys_giftcerts_failbox').show().delay(5000).fadeOut();
        $($modalID).modal('toggle');
    }
}

$(document).ready(function () {

    bindActions();

    $('[data-tooltip="tooltip"]').tooltip()

    $('#redeemModal #customSwitch1').on('change', function () {
        if ($('.modalRedeemValueGroup').length) {
            if ($('#redeemModal #customSwitch1').prop("checked")) {
                $('.modalRedeemValueGroup').hide();
            } else {
                $('.modalRedeemValueGroup').show();
            }
        }
    });

    $('#redeemModal .btn-primary').on('click', function () {

        var modalcontext = $(this).closest(".modal-content");

        var $fullredeem = 0;
        if (modalcontext.find("#customSwitch1").prop("checked")) {
            $fullredeem = 1;
        }

        var $stoken = modalcontext.find('input[name="stoken"]').val();
        var $adminID = modalcontext.find('input[name="admin_sid"]').val();
        var $shopID = modalcontext.find('input[name="shp"]').val();

        // AJAX CALL TO CHECK VALUES AGAINST SERVICE
        $.ajax({
            url: "/admin/index.php?cl=ys_giftcert__admin_ajax&fnc=redeemGiftcert",
            method: "POST",
            data: {
                voucherNr: modalcontext.find(".giftcertcode").val(),
                voucherValue: modalcontext.find("#modalRedeemValue").val(),
                fullredeem: $fullredeem,
                stoken: $stoken,
                force_admin_sid: $adminID,
                shp: $shopID
            },
            dataType: 'json',
            async: true,
            context: modalcontext,
            success: function(result){reloadCertList(result, $stoken, $adminID, "#redeemModal", $shopID);}
        });
    });

    // delete gift cert
    $('#deleteModal .btn-danger').on('click', function () {

        var modalcontext = $(this).closest(".modal-content");
        var $stoken = modalcontext.find('input[name="stoken"]').val();
        var $adminID = modalcontext.find('input[name="admin_sid"]').val();
        var $shopID = modalcontext.find('input[name="shp"]').val();

        // AJAX CALL TO CHECK VALUES AGAINST SERVICE
        $.ajax({
            url: "/admin/index.php?cl=ys_giftcert__admin_ajax&fnc=deleteGiftcert",
            method: "POST",
            data: {
                voucherNr: modalcontext.find(".giftcertcodedelete").val(),
                stoken: $stoken,
                force_admin_sid: $adminID,
                shp: $shopID
            },
            dataType: 'json',
            async: true,
            context: modalcontext,
            success: function(result){reloadCertList(result, $stoken, $adminID, "#deleteModal", $shopID);}
        });
    });

    // Create a new Cert
    $('#addGiftCertModal .btn-primary').on('click', function () {

        var modalcontext = $(this).closest(".modal-content");

        var $stoken = modalcontext.find('input[name="stoken"]').val();
        var $adminID = modalcontext.find('input[name="admin_sid"]').val();
        var $shopID = modalcontext.find('input[name="shp"]').val();

        // AJAX CALL TO CHECK VALUES AGAINST SERVICE
        $.ajax({
            url: "/admin/index.php?cl=ys_giftcert__admin_ajax&fnc=addGiftcert",
            method: "POST",
            data: {
                voucherValue: modalcontext.find("#modalAddValue").val(),
                voucherQuantity: modalcontext.find("#modalAddQuantity").val(),
                voucherDate: modalcontext.find("#modalAddDate").val(),
                stoken: $stoken,
                force_admin_sid: $adminID,
                shp: $shopID
            },
            dataType: 'json',
            async: true,
            context: modalcontext,
            success: function(result){
                reloadCertList(result, $stoken, $adminID, "#addGiftCertModal", $shopID);
            }
        });
    });
});



