/**
 * Launchmind Blog Connector - Admin Scripts
 */

(function($) {
    'use strict';

    $(document).ready(function() {
        
        // Test Connection
        $('#launchmind-test-connection').on('click', function() {
            var $button = $(this);
            var $result = $('#launchmind-test-result');
            
            $button.prop('disabled', true);
            $result
                .removeClass('success error')
                .addClass('loading')
                .text(launchmindAdmin.strings.testing)
                .show();
            
            $.ajax({
                url: launchmindAdmin.ajaxUrl,
                type: 'POST',
                data: {
                    action: 'launchmind_test_connection',
                    nonce: launchmindAdmin.nonce
                },
                success: function(response) {
                    $result.removeClass('loading');
                    
                    if (response.success) {
                        $result
                            .addClass('success')
                            .text(launchmindAdmin.strings.success);
                    } else {
                        $result
                            .addClass('error')
                            .text(launchmindAdmin.strings.error + ' ' + response.data.message);
                    }
                },
                error: function() {
                    $result
                        .removeClass('loading')
                        .addClass('error')
                        .text(launchmindAdmin.strings.error + ' Network error');
                },
                complete: function() {
                    $button.prop('disabled', false);
                }
            });
        });
        
        // Clear Cache
        $('#launchmind-clear-cache').on('click', function() {
            var $button = $(this);
            var $result = $('#launchmind-cache-result');
            
            $button.prop('disabled', true);
            $result
                .removeClass('success error')
                .addClass('loading')
                .text(launchmindAdmin.strings.clearing)
                .show();
            
            $.ajax({
                url: launchmindAdmin.ajaxUrl,
                type: 'POST',
                data: {
                    action: 'launchmind_clear_cache',
                    nonce: launchmindAdmin.nonce
                },
                success: function(response) {
                    $result.removeClass('loading');
                    
                    if (response.success) {
                        $result
                            .addClass('success')
                            .text(response.data.message);
                    } else {
                        $result
                            .addClass('error')
                            .text(response.data.message);
                    }
                },
                error: function() {
                    $result
                        .removeClass('loading')
                        .addClass('error')
                        .text('Network error');
                },
                complete: function() {
                    $button.prop('disabled', false);
                }
            });
        });
        
    });

})(jQuery);

