jQuery(document).ready(function($) {
    // Load keywords from localStorage when page loads
    loadKeywordsFromStorage();
    
    // Add event listener for the refresh button
    $('.aysa-refresh-btn').on('click', function() {
        loadKeywordsFromStorage();
        // Show notification
        $('<div class="notice notice-success is-dismissible"><p>Keywords table refreshed from localStorage.</p></div>')
            .insertAfter('.aysa-table-controls')
            .delay(3000)
            .fadeOut(500, function() {
                $(this).remove();
            });
    });
    
    // Function to load keywords from localStorage
    function loadKeywordsFromStorage() {
        const storedKeywords = localStorage.getItem('aysaKeywordSuggestions');
        if (storedKeywords) {
            try {
                const keywordsData = JSON.parse(storedKeywords);
                // Clear existing table rows except header
                const $tableBody = $('.aysa-keyword-table tbody');
                $tableBody.empty();
                
                // Add keywords to table
                keywordsData.forEach(function(keyword) {
                    const $row = $('<tr></tr>');
                    $row.append('<td><input type="checkbox" class="aysa-keyword-checkbox"></td>');
                    $row.append('<td>' + keyword.keyword + '</td>');
                    $row.append('<td>' + keyword.volume + '</td>');
                    $row.append('<td>' + keyword.difficulty + '</td>');
                    $row.append('<td>' + keyword.cpc + '</td>');
                    $tableBody.append($row);
                });
            } catch (e) {
                console.error('Error parsing keywords from localStorage:', e);
            }
        }
    }
}); 