<?php
require_once __DIR__ . '/../functions.php';
requireAuth();
$page_title = 'My Dashboard';
$db = db();
$user = getCurrentUser();

$stmt = $db->prepare("SELECT COUNT(*) FROM orders WHERE user_id = ?");
$stmt->execute([$user['id']]);
$orderCount = $stmt->fetchColumn();
$stmt = $db->prepare("SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC LIMIT 5");
$stmt->execute([$user['id']]);
$recentOrders = $stmt->fetchAll();
$stmt = $db->prepare("SELECT COUNT(*) FROM wishlists WHERE user_id = ?");
$stmt->execute([$user['id']]);
$wishlistCount = $stmt->fetchColumn();

$page_title = 'My Dashboard';
require_once __DIR__ . '/../includes/header.php';
?>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-2xl font-bold text-gray-800">My Dashboard</h1>
        <span class="text-sm text-gray-500">Welcome, <?= h($user['name']) ?></span>
    </div>
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
        <div class="bg-white rounded-xl border border-gray-200 p-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-primary-50 rounded-full flex items-center justify-center"><i class="fas fa-shopping-bag text-primary-600 text-xl"></i></div>
                <div><p class="text-2xl font-bold text-gray-800"><?= $orderCount ?></p><p class="text-sm text-gray-500">Total Orders</p></div>
            </div>
        </div>
        <div class="bg-white rounded-xl border border-gray-200 p-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-green-50 rounded-full flex items-center justify-center"><i class="fas fa-heart text-green-600 text-xl"></i></div>
                <div><p class="text-2xl font-bold text-gray-800"><?= $wishlistCount ?></p><p class="text-sm text-gray-500">Wishlist Items</p></div>
            </div>
        </div>
        <div class="bg-white rounded-xl border border-gray-200 p-6">
            <div class="flex items-center gap-4">
                <div class="w-12 h-12 bg-yellow-50 rounded-full flex items-center justify-center"><i class="fas fa-user text-yellow-600 text-xl"></i></div>
                <div><p class="text-sm font-medium text-gray-800"><?= h($user['name']) ?></p><p class="text-sm text-gray-500"><?= h($user['email']) ?></p></div>
            </div>
        </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>
        <?php if (count($recentOrders) === 0): ?>
        <p class="text-gray-500 text-sm">No orders yet. <a href="../catalog.php" class="text-primary-600 hover:underline">Start shopping</a></p>
        <?php else: ?>
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead><tr class="text-left text-gray-500 border-b"><th class="pb-2 font-medium">Order</th><th class="pb-2 font-medium">Date</th><th class="pb-2 font-medium">Status</th><th class="pb-2 font-medium">Total</th><th class="pb-2 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 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="order-detail.php?id=<?= $o['id'] ?>" class="text-primary-600 hover:underline">View</a></td></tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
