<?php
require_once __DIR__ . '/../functions.php';
requireAdmin();
$page_title = 'Dashboard';
$db = db();

$totalRevenue = $db->query("SELECT COALESCE(SUM(total), 0) FROM orders WHERE status != 'cancelled'")->fetchColumn();
$totalOrders = $db->query("SELECT COUNT(*) FROM orders")->fetchColumn();
$totalProducts = $db->query("SELECT COUNT(*) FROM products")->fetchColumn();
$totalUsers = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
$pendingOrders = $db->query("SELECT COUNT(*) FROM orders WHERE status = 'pending'")->fetchColumn();
$recentOrders = $db->query("SELECT * FROM orders ORDER BY created_at DESC LIMIT 10")->fetchAll();

require_once __DIR__ . '/../includes/admin-header.php';
?>
<div class="grid grid-cols-1 md:grid-cols-5 gap-4 mb-8">
    <div class="bg-white rounded-xl border border-gray-200 p-5"><p class="text-sm text-gray-500">Revenue</p><p class="text-2xl font-bold text-gray-800 mt-1"><?= formatPrice($totalRevenue) ?></p></div>
    <div class="bg-white rounded-xl border border-gray-200 p-5"><p class="text-sm text-gray-500">Orders</p><p class="text-2xl font-bold text-gray-800 mt-1"><?= $totalOrders ?></p></div>
    <div class="bg-white rounded-xl border border-gray-200 p-5"><p class="text-sm text-gray-500">Products</p><p class="text-2xl font-bold text-gray-800 mt-1"><?= $totalProducts ?></p></div>
    <div class="bg-white rounded-xl border border-gray-200 p-5"><p class="text-sm text-gray-500">Users</p><p class="text-2xl font-bold text-gray-800 mt-1"><?= $totalUsers ?></p></div>
    <div class="bg-white rounded-xl border border-gray-200 p-5"><p class="text-sm text-gray-500">Pending Orders</p><p class="text-2xl font-bold text-yellow-600 mt-1"><?= $pendingOrders ?></p></div>
</div>
<div class="bg-white rounded-xl border border-gray-200 p-6">
    <div class="flex items-center justify-between mb-4"><h2 class="font-semibold text-gray-800">Recent Orders</h2><a href="orders.php" class="text-sm text-primary-600 hover:underline">View All</a></div>
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead><tr class="text-left text-gray-500 border-b"><th class="pb-3 font-medium">Order</th><th class="pb-3 font-medium">Customer</th><th class="pb-3 font-medium">Date</th><th class="pb-3 font-medium">Status</th><th class="pb-3 font-medium">Total</th><th class="pb-3 font-medium"></th></tr></thead>
            <tbody>
                <?php foreach ($recentOrders as $o): ?>
                <tr class="border-b last:border-0">
                    <td class="py-3 font-medium"><?= h($o['order_number']) ?></td>
                    <td class="py-3"><?= h($o['shipping_name'] ?: 'N/A') ?></td>
                    <td class="py-3 text-gray-500"><?= date('d M Y', strtotime($o['created_at'])) ?></td>
                    <td class="py-3"><?= getStatusBadge($o['status']) ?></td>
                    <td class="py-3 font-medium"><?= formatPrice($o['total']) ?></td>
                    <td class="py-3"><a href="orders.php?id=<?= $o['id'] ?>" class="text-primary-600 hover:underline">View</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php require_once __DIR__ . '/../includes/admin-footer.php'; ?>
