/**
 * Zoho CRM Admin JavaScript
 */
jQuery(document).ready(function($) {
    // Test connection
    $('#test-connection').on('click', function() {
        var $button = $(this);
        var $status = $('#connection-status');
        
        $button.prop('disabled', true).text('Testing...');
        $status.html('');
        
        $.ajax({
            url: hvacZoho.ajaxUrl,
            method: 'POST',
            data: {
                action: 'hvac_zoho_test_connection',
                nonce: hvacZoho.nonce
            },
            success: function(response) {
                if (response.success) {
                    $status.html('<div class="notice notice-success"><p>' + response.data.message + ' (' + response.data.modules + ')</p></div>');
                } else {
                    $status.html('<div class="notice notice-error"><p>' + response.data.message + ': ' + response.data.error + '</p></div>');
                }
            },
            error: function() {
                $status.html('<div class="notice notice-error"><p>Connection test failed</p></div>');
            },
            complete: function() {
                $button.prop('disabled', false).text('Test Connection');
            }
        });
    });
    
    // Sync data
    $('.sync-button').on('click', function() {
        var $button = $(this);
        var type = $button.data('type');
        var $status = $('#' + type + '-status');
        
        $button.prop('disabled', true).text('Syncing...');
        $status.html('<p>Syncing ' + type + '...</p>');
        
        $.ajax({
            url: hvacZoho.ajaxUrl,
            method: 'POST',
            data: {
                action: 'hvac_zoho_sync_data',
                type: type,
                nonce: hvacZoho.nonce
            },
            success: function(response) {
                if (response.success) {
                    var result = response.data;
                    var html = '<div class="notice notice-success">';
                    
                    if (result.staging_mode) {
                        html += '<h4>🔧 STAGING MODE - Simulation Results</h4>';
                        html += '<p>' + result.message + '</p>';
                    } else {
                        html += '<p>Sync completed successfully!</p>';
                    }
                    
                    html += '<ul>' +
                        '<li>Total records: ' + result.total + '</li>' +
                        '<li>Synced: ' + result.synced + '</li>' +
                        '<li>Failed: ' + result.failed + '</li>' +
                        '</ul>';
                    
                    if (result.test_data && result.test_data.length > 0) {
                        html += '<details>' +
                            '<summary>View test data (first 5 records)</summary>' +
                            '<pre style="background: #f0f0f0; padding: 10px; overflow: auto;">' +
                            JSON.stringify(result.test_data.slice(0, 5), null, 2) +
                            '</pre>' +
                            '</details>';
                    }
                    
                    html += '</div>';
                    $status.html(html);
                } else {
                    $status.html('<div class="notice notice-error"><p>' + response.data.message + ': ' + response.data.error + '</p></div>');
                }
            },
            error: function() {
                $status.html('<div class="notice notice-error"><p>Sync failed</p></div>');
            },
            complete: function() {
                $button.prop('disabled', false).text('Sync ' + type.charAt(0).toUpperCase() + type.slice(1));
            }
        });
    });
    
    // Save settings
    $('#zoho-settings-form').on('submit', function(e) {
        e.preventDefault();
        
        var $form = $(this);
        var $button = $form.find('button[type="submit"]');
        
        $button.prop('disabled', true).text('Saving...');
        
        $.ajax({
            url: hvacZoho.ajaxUrl,
            method: 'POST',
            data: {
                action: 'hvac_zoho_save_settings',
                nonce: hvacZoho.nonce,
                auto_sync: $form.find('input[name="auto_sync"]').is(':checked') ? '1' : '0',
                sync_frequency: $form.find('select[name="sync_frequency"]').val()
            },
            success: function(response) {
                if (response.success) {
                    alert('Settings saved successfully!');
                } else {
                    alert('Error saving settings: ' + response.data.message);
                }
            },
            error: function() {
                alert('Error saving settings');
            },
            complete: function() {
                $button.prop('disabled', false).text('Save Settings');
            }
        });
    });
});