/**
 * Pawfect Theme - Admin JavaScript
 * 
 * @package Pawfect
 */

(function($) {
    'use strict';

    $(document).ready(function() {
        // Admin notice dismiss functionality
        $('.pawfect-admin-notice .notice-dismiss').on('click', function() {
            var $notice = $(this).closest('.pawfect-admin-notice');
            var noticeId = $notice.data('notice-id');
            
            if (noticeId && typeof pawfect_ajax_object !== 'undefined') {
                $.ajax({
                    url: pawfect_ajax_object.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'pawfect_dismiss_notice',
                        notice_id: noticeId,
                        nonce: pawfect_ajax_object.nonce
                    }
                });
            }
        });

        // Theme options save functionality
        $('.pawfect-save-options').on('click', function(e) {
            e.preventDefault();
            
            var $button = $(this);
            var $form = $button.closest('form');
            
            $button.prop('disabled', true).text('Saving...');
            
            $.ajax({
                url: pawfect_ajax_object.ajax_url,
                type: 'POST',
                data: $form.serialize(),
                success: function(response) {
                    $button.prop('disabled', false).text('Save Changes');
                    
                    if (response.success) {
                        // Show success message
                        $('<div class="notice notice-success is-dismissible"><p>Settings saved successfully!</p></div>')
                            .insertAfter('.pawfect-theme-options h1')
                            .delay(3000)
                            .fadeOut();
                    }
                },
                error: function() {
                    $button.prop('disabled', false).text('Save Changes');
                    alert('Error saving settings. Please try again.');
                }
            });
        });

        // Install required plugins from Starter Sites dashboard
        $('#pawfect-install-plugins-btn').on('click', function(e) {
            e.preventDefault();
            
            var $button = $(this);
            $button.prop('disabled', true).text('Installing & Activating... Please wait');
            
            $.ajax({
                url: pawfect_ajax_object.ajax_url,
                type: 'POST',
                data: {
                    action: 'pawfect_install_plugins',
                    nonce: pawfect_ajax_object.nonce
                },
                success: function(response) {
                    if (response.success) {
                        $button.text('Success! Reloading...');
                        window.location.reload();
                    } else {
                        $button.prop('disabled', false).text('Install & Activate Plugins');
                        alert('Error: ' + (response.data && response.data.message ? response.data.message : 'Unknown error occurred.'));
                    }
                },
                error: function() {
                    $button.prop('disabled', false).text('Install & Activate Plugins');
                    alert('Server error occurred during installation. Please check your connection and try again.');
                }
            });
        });
    });

})(jQuery);

