<?php
if (!defined('ABSPATH')) exit;

$reports = new Dima_Reports();
$period = isset($_GET['period']) ? sanitize_text_field($_GET['period']) : 'month';
$sales_report = $reports->get_sales_report($period);
$top_products = $reports->get_top_products(10, $period);
$chart_data = $reports->get_chart_data(30);
?>

<div class="wrap">
    <h1>📊 گزارشات فروش</h1>
    
    <!-- فیلتر دوره -->
    <div class="dima-period-filter">
        <a href="?page=dima-shop-reports&period=today" class="button <?php echo $period === 'today' ? 'button-primary' : ''; ?>">امروز</a>
        <a href="?page=dima-shop-reports&period=week" class="button <?php echo $period === 'week' ? 'button-primary' : ''; ?>">هفته</a>
        <a href="?page=dima-shop-reports&period=month" class="button <?php echo $period === 'month' ? 'button-primary' : ''; ?>">ماه</a>
        <a href="?page=dima-shop-reports&period=year" class="button <?php echo $period === 'year' ? 'button-primary' : ''; ?>">سال</a>
        <a href="?page=dima-shop-reports&period=all" class="button <?php echo $period === 'all' ? 'button-primary' : ''; ?>">همه</a>
    </div>
    
    <!-- آمار -->
    <div class="dima-stats-grid">
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #10b981 0%, #059669 100%);">
            <div class="stat-icon">💰</div>
            <div class="stat-info">
                <div class="stat-label">کل فروش</div>
                <div class="stat-value"><?php echo number_format($sales_report['total_sales']); ?> تومان</div>
            </div>
        </div>
        
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);">
            <div class="stat-icon"></div>
            <div class="stat-info">
                <div class="stat-label">تعداد سفارشات</div>
                <div class="stat-value"><?php echo intval($sales_report['total_orders']); ?></div>
            </div>
        </div>
        
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);">
            <div class="stat-icon">📈</div>
            <div class="stat-info">
                <div class="stat-label">میانگین سفارش</div>
                <div class="stat-value"><?php echo number_format($sales_report['average_order']); ?> تومان</div>
            </div>
        </div>
        
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);">
            <div class="stat-icon">🎟️</div>
            <div class="stat-info">
                <div class="stat-label">کل تخفیفات</div>
                <div class="stat-value"><?php echo number_format($sales_report['total_discounts']); ?> تومان</div>
            </div>
        </div>
    </div>
    
    <!-- نمودار -->
    <div class="dima-section">
        <h2>📈 نمودار فروش ۳۰ روز اخیر</h2>
        <canvas id="salesChart" height="100"></canvas>
    </div>
    
    <!-- محصولات پرفروش -->
    <div class="dima-section">
        <h2>🏆 محصولات پرفروش</h2>
        <?php if (empty($top_products)): ?>
            <p>داده‌ای موجود نیست.</p>
        <?php else: ?>
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr><th>#</th><th>محصول</th><th>تعداد فروش</th><th>درآمد</th></tr>
            </thead>
            <tbody>
                <?php foreach ($top_products as $i => $p): 
                    $product = get_post($p->product_id);
                ?>
                <tr>
                    <td><strong><?php echo $i + 1; ?></strong></td>
                    <td><?php echo $product ? esc_html($product->post_title) : 'حذف شده'; ?></td>
                    <td><?php echo intval($p->total_quantity); ?></td>
                    <td><strong><?php echo number_format($p->total_revenue); ?> تومان</strong></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
jQuery(document).ready(function($) {
    var ctx = document.getElementById('salesChart').getContext('2d');
    new Chart(ctx, {
        type: 'line',
        data: {
            labels: <?php echo wp_json_encode($chart_data['labels']); ?>,
            datasets: [{
                label: 'فروش (تومان)',
                data: <?php echo wp_json_encode($chart_data['data']); ?>,
                borderColor: '#6366f1',
                backgroundColor: 'rgba(99, 102, 241, 0.1)',
                fill: true,
                tension: 0.4
            }]
        },
        options: {
            responsive: true,
            plugins: { legend: { display: false } },
            scales: {
                y: { beginAtZero: true, ticks: { callback: function(v) { return v.toLocaleString('fa-IR'); } } }
            }
        }
    });
});
</script>

<style>
.dima-period-filter { margin: 20px 0; display: flex; gap: 8px; }
.dima-stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; margin-bottom: 30px; }
.dima-stat-card { border-radius: 16px; padding: 24px; color: white; display: flex; align-items: center; gap: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.15); }
.stat-icon { font-size: 40px; }
.stat-label { font-size: 13px; opacity: 0.9; margin-bottom: 4px; }
.stat-value { font-size: 22px; font-weight: 800; }
.dima-section { background: white; padding: 24px; border-radius: 12px; margin-bottom: 24px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); border: 1px solid #e5e7eb; }
.dima-section h2 { margin-top: 0; padding-bottom: 15px; border-bottom: 2px solid #f3f4f6; }
</style>