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

$reports = new Dima_Reports();
$stats = $reports->get_dashboard_stats();
$checkout = Dima_Checkout::get_instance();
$recent_orders = $checkout->get_customer_orders(null);
?>

<div class="wrap dima-dashboard">
    <h1 style="font-size:28px; font-weight:800; color:#1f2937; margin-bottom:25px;">
        📊 داشبورد Dima Shop
    </h1>
    
    <!-- آمار کلی -->
    <div class="dima-stats-grid">
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
            <div class="stat-icon">💰</div>
            <div class="stat-info">
                <div class="stat-label">فروش امروز</div>
                <div class="stat-value"><?php echo number_format($stats['today_sales']); ?> تومان</div>
            </div>
        </div>
        
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
            <div class="stat-icon">🛒</div>
            <div class="stat-info">
                <div class="stat-label">سفارشات امروز</div>
                <div class="stat-value"><?php echo intval($stats['today_orders']); ?></div>
            </div>
        </div>
        
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
            <div class="stat-icon">📈</div>
            <div class="stat-info">
                <div class="stat-label">فروش این ماه</div>
                <div class="stat-value"><?php echo number_format($stats['month_sales']); ?> تومان</div>
            </div>
        </div>
        
        <div class="dima-stat-card" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);">
            <div class="stat-icon">📦</div>
            <div class="stat-info">
                <div class="stat-label">کل محصولات</div>
                <div class="stat-value"><?php echo intval($stats['total_products']); ?></div>
            </div>
        </div>
    </div>
    
    <!-- سفارشات اخیر -->
    <div class="dima-section">
        <div class="section-header">
            <h2>🕐 سفارشات اخیر</h2>
            <a href="<?php echo admin_url('admin.php?page=dima-shop-orders'); ?>" class="button button-primary">
                مشاهده همه سفارشات
            </a>
        </div>
        
        <?php
        global $wpdb;
        $orders = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}dima_orders ORDER BY created_at DESC LIMIT 10");
        
        if (empty($orders)):
        ?>
        <div class="empty-state">
            <div class="empty-icon"></div>
            <h3>هنوز سفارشی ثبت نشده است</h3>
            <p>اولین سفارش شما پس از ثبت توسط مشتری در اینجا نمایش داده می‌شود.</p>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th>شناسه</th>
                        <th>مشتری</th>
                        <th>مبلغ</th>
                        <th>وضعیت</th>
                        <th>تاریخ</th>
                        <th>عملیات</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $order): 
                        $customer = json_decode($order->customer_data, true);
                        $status_labels = array(
                            'pending' => 'در انتظار',
                            'processing' => 'در حال پردازش',
                            'completed' => 'تکمیل شده',
                            'failed' => 'ناموفق',
                            'cancelled' => 'لغو شده',
                            'refunded' => 'مرجوع شده'
                        );
                        $status_colors = array(
                            'pending' => '#f59e0b',
                            'processing' => '#3b82f6',
                            'completed' => '#10b981',
                            'failed' => '#ef4444',
                            'cancelled' => '#6b7280',
                            'refunded' => '#8b5cf6'
                        );
                    ?>
                    <tr>
                        <td><strong>#<?php echo intval($order->id); ?></strong></td>
                        <td><?php echo esc_html($customer['name'] ?? 'مهمان'); ?></td>
                        <td><strong><?php echo number_format($order->final_amount); ?> تومان</strong></td>
                        <td>
                            <span style="background:<?php echo $status_colors[$order->status] ?? '#6b7280'; ?>; color:white; padding:4px 12px; border-radius:20px; font-size:12px; font-weight:600;">
                                <?php echo $status_labels[$order->status] ?? $order->status; ?>
                            </span>
                        </td>
                        <td><?php echo date_i18n('j F Y - H:i', strtotime($order->created_at)); ?></td>
                        <td>
                            <a href="<?php echo admin_url('admin.php?page=dima-shop-orders&action=view&id=' . $order->id); ?>" class="button button-small">
                                مشاهده
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
    
    <!-- لینک‌های سریع -->
    <div class="dima-quick-links">
        <h2>⚡ دسترسی سریع</h2>
        <div class="quick-links-grid">
            <a href="<?php echo admin_url('post-new.php?post_type=dima_product'); ?>" class="quick-link">
                <span class="ql-icon">➕</span>
                <span class="ql-text">افزودن محصول جدید</span>
            </a>
            <a href="<?php echo admin_url('admin.php?page=dima-shop-reports'); ?>" class="quick-link">
                <span class="ql-icon">📊</span>
                <span class="ql-text">گزارشات فروش</span>
            </a>
            <a href="<?php echo admin_url('admin.php?page=dima-shop-coupons'); ?>" class="quick-link">
                <span class="ql-icon">️</span>
                <span class="ql-text">مدیریت کوپن‌ها</span>
            </a>
            <a href="<?php echo admin_url('admin.php?page=dima-shop-settings'); ?>" class="quick-link">
                <span class="ql-icon">⚙️</span>
                <span class="ql-text">تنظیمات پلاگین</span>
            </a>
            <a href="<?php echo admin_url('admin.php?page=dima-shop-ai'); ?>" class="quick-link">
                <span class="ql-icon">🤖</span>
                <span class="ql-text">تولید محتوا با AI</span>
            </a>
            <a href="<?php echo admin_url('admin.php?page=dima-shop-updates'); ?>" class="quick-link">
                <span class="ql-icon">🔄</span>
                <span class="ql-text">بروزرسانی‌ها</span>
            </a>
        </div>
    </div>
</div>

<style>
.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);
    transition: transform 0.3s;
}
.dima-stat-card:hover { transform: translateY(-4px); }
.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;
    border-radius: 16px;
    padding: 24px;
    margin-bottom: 24px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    border: 1px solid #e5e7eb;
}
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 2px solid #f3f4f6;
}
.section-header h2 { margin: 0; font-size: 18px; color: #1f2937; }

.empty-state {
    text-align: center;
    padding: 50px 20px;
    background: #f9fafb;
    border-radius: 12px;
    border: 2px dashed #e5e7eb;
}
.empty-state .empty-icon { font-size: 56px; margin-bottom: 15px; opacity: 0.5; }
.empty-state h3 { margin: 10px 0; color: #374151; }
.empty-state p { color: #6b7280; }

.dima-quick-links {
    background: white;
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    border: 1px solid #e5e7eb;
}
.dima-quick-links h2 { margin: 0 0 20px 0; font-size: 18px; color: #1f2937; }
.quick-links-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 12px;
}
.quick-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px;
    background: #f9fafb;
    border-radius: 12px;
    text-decoration: none;
    color: #374151;
    transition: all 0.2s;
    border: 2px solid transparent;
}
.quick-link:hover {
    background: white;
    border-color: #6366f1;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.15);
}
.ql-icon { font-size: 28px; }
.ql-text { font-weight: 600; font-size: 14px; }
</style>