<?php
require_once __DIR__ . '/includes/admin_bootstrap.php';

// Stats
$totalOrders = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();
$pendingOrders = $pdo->query("SELECT COUNT(*) FROM orders WHERE status='pending'")->fetchColumn();
$totalRevenue = $pdo->query("SELECT SUM(total_amount) FROM orders WHERE status='delivered'")->fetchColumn() ?? 0;
$totalProducts = $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();
$lowStock = $pdo->query("SELECT COUNT(*) FROM products WHERE stock <= 5 AND stock > 0")->fetchColumn();
$outOfStock = $pdo->query("SELECT COUNT(*) FROM products WHERE stock = 0")->fetchColumn();
$totalUsers = $pdo->query("SELECT COUNT(*) FROM users WHERE role='user'")->fetchColumn();
$totalExpenses = $pdo->query("SELECT SUM(amount) FROM expenses")->fetchColumn() ?? 0;

// Recent orders
$recentOrders = $pdo->query("SELECT * FROM orders ORDER BY created_at DESC LIMIT 8")->fetchAll();

// Recent reviews
$recentReviews = $pdo->query("SELECT r.*, u.name as user_name, p.name as product_name FROM reviews r LEFT JOIN users u ON r.user_id=u.id LEFT JOIN products p ON r.product_id=p.id WHERE r.status='pending' LIMIT 5")->fetchAll();

// ── HTML output starts ──
include __DIR__ . '/includes/admin_header.php';

?>

<h2 style="color:var(--navy);margin-bottom:20px;">ড্যাশবোর্ড ওভারভিউ</h2>

<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon"><i class="fas fa-shopping-cart"></i></div>
        <div class="stat-info">
            <div class="stat-value"><?= $totalOrders ?></div>
            <div class="stat-label">মোট অর্ডার</div>
        </div>
    </div>
    <div class="stat-card" style="border-left-color:#e53e3e;">
        <div class="stat-icon" style="color:#e53e3e;"><i class="fas fa-clock"></i></div>
        <div class="stat-info">
            <div class="stat-value"><?= $pendingOrders ?></div>
            <div class="stat-label">অপেক্ষমান অর্ডার</div>
        </div>
    </div>
    <div class="stat-card" style="border-left-color:#38a169;">
        <div class="stat-icon" style="color:#38a169;"><i class="fas fa-money-bill-wave"></i></div>
        <div class="stat-info">
            <div class="stat-value">৳<?= number_format($totalRevenue, 0) ?></div>
            <div class="stat-label">মোট রাজস্ব</div>
        </div>
    </div>
    <div class="stat-card" style="border-left-color:#3182ce;">
        <div class="stat-icon" style="color:#3182ce;"><i class="fas fa-users"></i></div>
        <div class="stat-info">
            <div class="stat-value"><?= $totalUsers ?></div>
            <div class="stat-label">নিবন্ধিত গ্রাহক</div>
        </div>
    </div>
</div>

<div style="display:grid;grid-template-columns:1fr 340px;gap:24px;align-items:start;flex-wrap:wrap;">

    <!-- Recent Orders -->
    <div class="card">
        <div class="card-header">
            <h3>সাম্প্রতিক অর্ডার</h3>
            <a href="<?= BASE_URL ?>/admin/orders.php" class="topbar-btn btn-navy">সব দেখুন</a>
        </div>
        <div class="table-wrap">
            <table>
                <thead><tr>
                    <th>অর্ডার নং</th>
                    <th>গ্রাহক</th>
                    <th>মোট</th>
                    <th>স্ট্যাটাস</th>
                    <th>তারিখ</th>
                </tr></thead>
                <tbody>
                <?php foreach ($recentOrders as $o): ?>
                    <tr>
                        <td><a href="<?= BASE_URL ?>/admin/orders.php?view=<?= $o['id'] ?>" style="color:var(--navy);font-weight:700;">#<?= str_pad($o['id'],6,'0',STR_PAD_LEFT) ?></a></td>
                        <td><?= e($o['customer_name']) ?></td>
                        <td>৳<?= number_format($o['total_amount'],2) ?></td>
                        <td><span class="badge badge-<?= $o['status'] ?>"><?= match($o['status']) {
                            'pending'=>'অপেক্ষমান','processing'=>'প্রক্রিয়াধীন',
                            'shipped'=>'পাঠানো হয়েছে','delivered'=>'ডেলিভারি হয়েছে',
                            'cancelled'=>'বাতিল',default=>$o['status']
                        } ?></span></td>
                        <td><?= date('d/m/Y', strtotime($o['created_at'])) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Quick Stats -->
    <div>
        <div class="card" style="margin-bottom:16px;">
            <div class="card-header"><h3>স্টক সতর্কতা</h3></div>
            <div class="card-body" style="padding:16px;">
                <div style="display:flex;justify-content:space-between;padding:10px 0;border-bottom:1px solid var(--gray-100);">
                    <span style="color:var(--gray-600);">কম স্টক (≤5)</span>
                    <span class="stock-low"><?= $lowStock ?> টি পণ্য</span>
                </div>
                <div style="display:flex;justify-content:space-between;padding:10px 0;border-bottom:1px solid var(--gray-100);">
                    <span style="color:var(--gray-600);">স্টক শেষ</span>
                    <span class="stock-low"><?= $outOfStock ?> টি পণ্য</span>
                </div>
                <div style="display:flex;justify-content:space-between;padding:10px 0;">
                    <span style="color:var(--gray-600);">মোট পণ্য</span>
                    <span style="font-weight:700;color:var(--navy);"><?= $totalProducts ?> টি</span>
                </div>
                <a href="<?= BASE_URL ?>/admin/stock.php" class="topbar-btn btn-navy" style="display:block;text-align:center;margin-top:12px;">স্টক ম্যানেজ করুন</a>
            </div>
        </div>

        <?php if (!empty($recentReviews)): ?>
        <div class="card">
            <div class="card-header"><h3>অপেক্ষমান রিভিউ</h3><a href="<?= BASE_URL ?>/admin/reviews.php" class="topbar-btn btn-navy">সব দেখুন</a></div>
            <div class="card-body" style="padding:16px;">
                <?php foreach ($recentReviews as $rv): ?>
                <div style="padding:10px 0;border-bottom:1px solid var(--gray-100);">
                    <div style="font-weight:600;font-size:0.88rem;color:var(--navy);"><?= e($rv['product_name']) ?></div>
                    <div style="font-size:0.8rem;color:var(--gray-600);"><?= e($rv['user_name']) ?> — <?= str_repeat('★',$rv['rating']) ?></div>
                    <div style="font-size:0.82rem;margin-top:4px;"><?= e(mb_substr($rv['comment'],0,60)) ?>...</div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <div class="card" style="margin-top:16px;">
            <div class="card-header"><h3>দ্রুত লিংক</h3></div>
            <div class="card-body" style="padding:16px;display:grid;grid-template-columns:1fr 1fr;gap:8px;">
                <a href="<?= BASE_URL ?>/admin/products.php?action=add" class="topbar-btn btn-gold"><i class="fas fa-plus"></i> পণ্য যোগ</a>
                <a href="<?= BASE_URL ?>/admin/categories.php?action=add" class="topbar-btn btn-navy"><i class="fas fa-plus"></i> ক্যাটেগরি</a>
                <a href="<?= BASE_URL ?>/admin/investors.php?action=add" class="topbar-btn btn-navy"><i class="fas fa-plus"></i> ইনভেস্টর</a>
                <a href="<?= BASE_URL ?>/admin/expenses.php?action=add" class="topbar-btn btn-navy"><i class="fas fa-plus"></i> ব্যয় যোগ</a>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/includes/admin_footer.php'; ?>
