// source --> https://www.upokojenci-nm.si/wp-content/plugins/ct-ultimate-gdpr/assets/js/cookie-list.js?ver=5.3.9 
/** @var ct_ultimate_gdpr_cookie_list object - localized */

jQuery(function () {

    function render_list(party_filter) {

        if((typeof(ct_ultimate_gdpr_cookie_list) == 'undefined')) return false;
        
        var cookies = ct_ultimate_gdpr_cookie_list.list;
        jQuery("#ct-ultimate-gdpr-cookies-table tbody").empty();

        if (party_filter) {
            cookies = cookies.filter(function (party) {
                return party.first_or_third_party === party_filter;
            });
        }

        cookies.forEach(cookie => {
            jQuery("#ct-ultimate-gdpr-cookies-table tbody").append(render_row(cookie));
        });
    }

    function render_row(cookie) {
        var row = jQuery("<tr></tr>");

        for (const key in cookie) {
            if (key === 'can_be_blocked') {
                // check cookie block if true or false
                if (cookie[key] == 1)
                    row.append('<td><i class="fa fa-check" aria-hidden="true" style="color: green"></i></td>');
                else
                    row.append('<td><i class="fa fa-times" aria-hidden="true" style="color: red"></i></td>');
            } else
                row.append("<td>" + cookie[key] + "</td>");
        }

        return row;
    }

    render_list(jQuery('#ct-ultimate-gdpr-party-filter').val());

    jQuery('#ct-ultimate-gdpr-party-filter').change(function () {
        render_list(jQuery(this).val());
    });

});
// source --> https://www.upokojenci-nm.si/wp-content/plugins/ct-ultimate-gdpr/assets/js/shortcode-block-cookie.js?ver=5.3.9 
jQuery(document).ready(function($){
    var form = $('#ct-ultimate-gdpr-block-cookies');
    var submit = $('.ct-ultimate-gdpr-cookie-block-btn');
    var level = $('#ct-ultimate-gdpr-block-cookies .level').val();
    var checkbox = $('.ct-ultimate-gdpr-block-cookies-checkbox');
    var notificaton = $('.notification');
    var check_count = 0;

    checkbox.on('click', function(){
      check_count++;
      submit.attr('disabled', false);
      if(checkbox.is(':checked')){
        level = 1;
      } else {
        level = 5;
      }
    });



    function Save(e) {

        e.preventDefault();

        $.post(ct_ultimate_gdpr_cookie.ajaxurl, {
            "action": "ct_ultimate_gdpr_cookie_consent_give",
            "level": level
        }, function () {
            $(document).trigger("consentUpdated.ctUltimateGdpr", [level]);
          if (ct_ultimate_gdpr_cookie.reload) {
                window.location.reload(true);
            }
        }).fail(function () {
            $.post(ct_ultimate_gdpr_cookie.ajaxurl, {
                "skip_cookies": true,
                "action": "ct_ultimate_gdpr_cookie_consent_give",
                "level": level
            }, function () {
                setJsCookie(level);
                $(document).trigger("consentUpdated.ctUltimateGdpr", [level]);
                if (ct_ultimate_gdpr_cookie.reload) {
                    window.location.reload(true);
                }
            })

        });

        if(check_count > 0) {
          notificaton.after('<p class="result-notification">Successfully submitted!</p>');
        }

        if(!ct_ultimate_gdpr_cookie.reload){
          setTimeout(function(){
            $('.result-notification').remove().fadeIn('slow');
          },2000);
        }


    }


    submit.bind('click', Save);


});