/**
 * @package         MailChimp Auto-Subscribe
 * @version         5.0.3 Pro
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            http://www.tassos.gr
 * @copyright       Copyright © 2021 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

jQuery(function($) {
    var $app    = $(".test_connection")
        $btn    = $app.find(".btn")
        $response = $app.find(".test_connection_response");

    $btn.on("click", function(event) {
        event.preventDefault();
        
        $.ajax({ 
            type: "POST",
            url: $app.attr("data-base") + 'index.php?option=com_ajax&format=raw&plugin=TM_MailChimpUserAutoAdd&group=user',
            data: $app.closest("form").serialize(),
            success: function(response) {
                if (response.indexOf('added to MailChimp') > -1) {
                    $response.html('<span class="icon-checkmark-2"></span>' + response);
                } else {
                    error(response);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                error(errorThrown);
            },
            beforeSend: function() {
                $response.empty();
                $btn.addClass("working");
            },
            complete: function() {
                $btn.removeClass("working");
            }
        });
    });

    function error(text) {
        $response.html('<span class="error">' + text + '</span>');
    }
})