<?php
/**
 * Enhanced Analytics Dashboard
 * Detailed analytics for SEO performance
 */
?>
<script>
    // Enhanced Analytics Functions
    
    function loadDetailedAnalytics() {
        fetch(window.location.href, {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: 'action=get_analytics_data'
        })
        .then(r => r.json())
        .then(data => {
            if (data.success) {
                displayAnalyticsDashboard(data);
            } else {
                console.error('Error loading analytics:', data.message);
            }
        })
        .catch(err => console.error(err));
    }
    
    function displayAnalyticsDashboard(data) {
        const container = document.getElementById('analyticsContainer');
        if (!container) return;
        
        let html = `
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;">
                <!-- SEO Overview Card -->
                <div style="background: linear-gradient(135deg, #d16baf, #c0478e); color: white; padding: 20px; border-radius: 8px;">
                    <h4 style="margin: 0 0 10px 0;">📊 Overall SEO Score</h4>
                    <div style="font-size: 36px; font-weight: bold;">${data.avg_seo_score || 0}<span style="font-size: 18px;">/100</span></div>
                    <small style="opacity: 0.9;">Average across all pages</small>
                </div>
                
                <!-- Optimized Pages -->
                <div style="background: linear-gradient(135deg, #4caf50, #45a049); color: white; padding: 20px; border-radius: 8px;">
                    <h4 style="margin: 0 0 10px 0;">✅ Optimized Pages</h4>
                    <div style="font-size: 36px; font-weight: bold;">${data.optimized_pages || 0}<span style="font-size: 18px;">/${data.total_pages || 0}</span></div>
                    <div style="font-size: 12px; margin-top: 5px;">Success Rate: ${data.optimization_rate || 0}%</div>
                </div>
                
                <!-- Total Keywords -->
                <div style="background: linear-gradient(135deg, #2196F3, #1976D2); color: white; padding: 20px; border-radius: 8px;">
                    <h4 style="margin: 0 0 10px 0;">🔑 Keywords Tracked</h4>
                    <div style="font-size: 36px; font-weight: bold;">${data.total_keywords || 0}</div>
                    <small style="opacity: 0.9;">Focus keywords configured</small>
                </div>
                
                <!-- Meta Tags Configured -->
                <div style="background: linear-gradient(135deg, #FF9800, #F57C00); color: white; padding: 20px; border-radius: 8px;">
                    <h4 style="margin: 0 0 10px 0;">📝 Meta Tags</h4>
                    <div style="font-size: 36px; font-weight: bold;">${data.meta_configured || 0}</div>
                    <small style="opacity: 0.9;">Pages with SEO data</small>
                </div>
            </div>
            
            <div style="margin-top: 30px; background: #f9f9f9; padding: 20px; border-radius: 8px;">
                <h3>📈 Detailed Breakdown</h3>
                
                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 15px;">
                    <!-- Products Analytics -->
                    <div style="background: white; padding: 15px; border-radius: 6px; border-left: 4px solid #d16baf;">
                        <h4 style="margin-top: 0; color: #d16baf;">📦 Products</h4>
                        <table style="width: 100%; font-size: 14px;">
                            <tr>
                                <td style="padding: 5px 0;">Total Products:</td>
                                <td style="text-align: right; font-weight: bold;">${data.products_count || 0}</td>
                            </tr>
                            <tr style="background: #f5f5f5;">
                                <td style="padding: 5px 0;">With SEO Data:</td>
                                <td style="text-align: right; font-weight: bold; color: #4caf50;">${data.products_optimized || 0}</td>
                            </tr>
                            <tr>
                                <td style="padding: 5px 0;">Avg SEO Score:</td>
                                <td style="text-align: right; font-weight: bold;">${data.avg_product_score || 0}/100</td>
                            </tr>
                            <tr style="background: #f5f5f5;">
                                <td style="padding: 5px 0;">Optimization Rate:</td>
                                <td style="text-align: right; font-weight: bold; color: #ff9800;">${data.product_opt_rate || 0}%</td>
                            </tr>
                        </table>
                    </div>
                    
                    <!-- Blogs Analytics -->
                    <div style="background: white; padding: 15px; border-radius: 6px; border-left: 4px solid #2196F3;">
                        <h4 style="margin-top: 0; color: #2196F3;">📝 Blog Posts</h4>
                        <table style="width: 100%; font-size: 14px;">
                            <tr>
                                <td style="padding: 5px 0;">Total Blog Posts:</td>
                                <td style="text-align: right; font-weight: bold;">${data.blogs_count || 0}</td>
                            </tr>
                            <tr style="background: #f5f5f5;">
                                <td style="padding: 5px 0;">With SEO Data:</td>
                                <td style="text-align: right; font-weight: bold; color: #4caf50;">${data.blogs_optimized || 0}</td>
                            </tr>
                            <tr>
                                <td style="padding: 5px 0;">Avg SEO Score:</td>
                                <td style="text-align: right; font-weight: bold;">${data.avg_blog_score || 0}/100</td>
                            </tr>
                            <tr style="background: #f5f5f5;">
                                <td style="padding: 5px 0;">Optimization Rate:</td>
                                <td style="text-align: right; font-weight: bold; color: #ff9800;">${data.blog_opt_rate || 0}%</td>
                            </tr>
                        </table>
                    </div>
                </div>
                
                <div style="margin-top: 20px;">
                    <h4 style="color: #333;">🎯 Recommendations</h4>
                    <ul style="line-height: 1.8; color: #555;">
                        <li>Focus on optimizing ${Math.ceil((data.total_pages - data.optimized_pages) / 2) || 0} pages that lack SEO data</li>
                        <li>Current optimization rate is ${data.optimization_rate || 0}% - aim for 100%</li>
                        <li>Review pages with scores below 60 for improvement opportunities</li>
                        <li>Ensure all keywords are present in page content</li>
                        <li>Add Open Graph tags to improve social sharing</li>
                        <li>Consider adding schema markup for featured products/posts</li>
                    </ul>
                </div>
            </div>
        `;
        
        container.innerHTML = html;
    }
    
    // Load analytics on page load
    document.addEventListener('DOMContentLoaded', () => {
        if (window.location.href.includes('action=analytics')) {
            loadDetailedAnalytics();
        }
    });
</script>
