function bindActions() {

    $(".giftcerts .showLog").off("click").on("click", function () {
        var $redeemlog = $(this).closest("li").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);

    });

}

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

$(document).ready(function () {

    bindActions();

    $('[data-toggle="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();
        // 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
            },
            dataType: 'json',
            async: true,
            context: modalcontext,
            success: function(result){reloadCertList(result, $stoken, $adminID, "#redeemModal");}
        });
    });

    // 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();
        // 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
            },
            dataType: 'json',
            async: true,
            context: modalcontext,
            success: function(result){reloadCertList(result, $stoken, $adminID, "#addGiftCertModal");}
        });
    });
});



