<?php
/**
 * GlowSpace User Dashboard
 * Premium user dashboard with statistics and quick actions
 */

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/db.php';

session_start();
auth()->requireLogin();

$user = auth()->getCurrentUser();
$settings = getSettings();

// Get recent orders
$recentOrders = db()->fetchAll(
    "SELECT o.*, 
     (SELECT COUNT(*) FROM order_items WHERE order_id = o.order_id) as item_count
     FROM orders o 
     WHERE o.user_id = ? 
     ORDER BY o.created_at DESC LIMIT 5",
    [$user['id']]
);

// Get recommended products (trending and new arrivals)
$recommendedProducts = getProducts([
    'status' => 1,
    'limit' => 4
]);

// Get recently purchased products
$recentlyPurchased = db()->fetchAll(
    "SELECT DISTINCT oi.product_id, p.title, p.offer_price, p.price,
     (SELECT image_path FROM product_images WHERE product_id = p.product_id LIMIT 1) as image
     FROM order_items oi
     JOIN orders o ON oi.order_id = o.order_id
     JOIN products p ON oi.product_id = p.product_id
     WHERE o.user_id = ? AND o.status = 'delivered'
     ORDER BY o.created_at DESC LIMIT 4",
    [$user['id']]
);

$currency = $settings['currency'] ?? '৳';
$primaryColor = $settings['primary_color'] ?? '#D4AF37';
$secondaryColor = $settings['secondary_color'] ?? '#F5A623';
$accentColor = $settings['accent_color'] ?? '#FF4D6D';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - GlowSpace</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .welcome-banner {
            background: linear-gradient(135deg, <?php echo $primaryColor; ?> 0%, <?php echo $secondaryColor; ?> 100%);
            border-radius: 20px;
            padding: 40px;
            margin-bottom: 30px;
            color: white;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }

        .welcome-banner h1 {
            font-size: 36px;
            margin-bottom: 10px;
        }

        .welcome-banner p {
            font-size: 18px;
            opacity: 0.9;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            transition: all 0.3s;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 50px rgba(0,0,0,0.15);
        }

        .stat-card .icon {
            width: 60px;
            height: 60px;
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            margin-bottom: 15px;
        }

        .stat-card .icon.blue {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .stat-card .icon.green {
            background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
            color: white;
        }

        .stat-card .icon.orange {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
        }

        .stat-card .icon.purple {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
        }

        .stat-card h3 {
            font-size: 36px;
            color: #333;
            margin-bottom: 5px;
        }

        .stat-card p {
            color: #666;
            font-size: 14px;
        }

        .content-grid {
            display: grid;
            grid-template-columns: 2fr 1fr;
            gap: 30px;
        }

        .card {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }

        .card h2 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .card h2 a {
            font-size: 14px;
            color: <?php echo $primaryColor; ?>;
            text-decoration: none;
        }

        .order-item {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 15px;
            border-left: 4px solid <?php echo $primaryColor; ?>;
            transition: all 0.3s;
        }

        .order-item:hover {
            transform: translateX(5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .order-item .order-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }

        .order-item .order-id {
            font-weight: bold;
            color: #333;
        }

        .order-item .order-status {
            padding: 5px 15px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }

        .order-item .order-status.pending {
            background: #fff3cd;
            color: #856404;
        }

        .order-item .order-status.confirmed {
            background: #d1ecf1;
            color: #0c5460;
        }

        .order-item .order-status.processing {
            background: #cce5ff;
            color: #004085;
        }

        .order-item .order-status.shipped {
            background: #e2e3e5;
            color: #383d41;
        }

        .order-item .order-status.delivered {
            background: #d4edda;
            color: #155724;
        }

        .order-item .order-status.cancelled {
            background: #f8d7da;
            color: #721c24;
        }

        .order-item .order-details {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .order-item .order-info {
            color: #666;
            font-size: 14px;
        }

        .order-item .order-total {
            font-size: 18px;
            font-weight: bold;
            color: <?php echo $primaryColor; ?>;
        }

        .product-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
        }

        .product-card {
            background: #f8f9fa;
            border-radius: 15px;
            overflow: hidden;
            transition: all 0.3s;
        }

        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 30px rgba(0,0,0,0.15);
        }

        .product-card img {
            width: 100%;
            height: 150px;
            object-fit: cover;
        }

        .product-card .product-info {
            padding: 15px;
        }

        .product-card h4 {
            font-size: 14px;
            color: #333;
            margin-bottom: 10px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .product-card .price {
            font-size: 18px;
            font-weight: bold;
            color: <?php echo $primaryColor; ?>;
        }

        .quick-actions {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
        }

        .quick-action-btn {
            background: linear-gradient(135deg, <?php echo $primaryColor; ?> 0%, <?php echo $secondaryColor; ?> 100%);
            color: white;
            border: none;
            padding: 20px;
            border-radius: 15px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            text-decoration: none;
            text-align: center;
            transition: all 0.3s;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 10px;
        }

        .quick-action-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }

        .quick-action-btn i {
            font-size: 24px;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #999;
        }

        .empty-state i {
            font-size: 48px;
            margin-bottom: 15px;
        }

        @media (max-width: 768px) {
            .content-grid {
                grid-template-columns: 1fr;
            }

            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
            }

            .welcome-banner h1 {
                font-size: 28px;
            }

            .welcome-banner p {
                font-size: 16px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="welcome-banner">
            <h1>Welcome back, <?php echo htmlspecialchars($user['full_name']); ?>! 👋</h1>
            <p>Here's what's happening with your account today.</p>
        </div>

        <div class="stats-grid">
            <div class="stat-card">
                <div class="icon blue">
                    <i class="fas fa-shopping-bag"></i>
                </div>
                <h3><?php echo $user['stats']['total_orders']; ?></h3>
                <p>Total Orders</p>
            </div>
            <div class="stat-card">
                <div class="icon green">
                    <i class="fas fa-check-circle"></i>
                </div>
                <h3><?php echo $user['stats']['completed_orders']; ?></h3>
                <p>Completed Orders</p>
            </div>
            <div class="stat-card">
                <div class="icon orange">
                    <i class="fas fa-clock"></i>
                </div>
                <h3><?php echo $user['stats']['pending_orders']; ?></h3>
                <p>Pending Orders</p>
            </div>
            <div class="stat-card">
                <div class="icon purple">
                    <i class="fas fa-heart"></i>
                </div>
                <h3><?php echo $user['stats']['wishlist_count']; ?></h3>
                <p>Wishlist Items</p>
            </div>
        </div>

        <div class="content-grid">
            <div class="main-content">
                <div class="card">
                    <h2>
                        Recent Orders
                        <a href="orders.php">View All <i class="fas fa-arrow-right"></i></a>
                    </h2>
                    <?php if (empty($recentOrders)): ?>
                        <div class="empty-state">
                            <i class="fas fa-shopping-bag"></i>
                            <p>No orders yet. Start shopping to see your orders here!</p>
                        </div>
                    <?php else: ?>
                        <?php foreach ($recentOrders as $order): ?>
                            <div class="order-item">
                                <div class="order-header">
                                    <span class="order-id">#<?php echo htmlspecialchars($order['order_id']); ?></span>
                                    <span class="order-status <?php echo $order['status']; ?>"><?php echo ucfirst($order['status']); ?></span>
                                </div>
                                <div class="order-details">
                                    <div class="order-info">
                                        <p><i class="fas fa-box"></i> <?php echo $order['item_count']; ?> item(s)</p>
                                        <p><i class="fas fa-calendar"></i> <?php echo date('M d, Y', strtotime($order['created_at'])); ?></p>
                                    </div>
                                    <div class="order-total"><?php echo $currency; ?><?php echo number_format($order['total']); ?></div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>

                <div class="card">
                    <h2>Recommended for You</h2>
                    <div class="product-grid">
                        <?php foreach ($recommendedProducts as $product): ?>
                            <div class="product-card">
                                <img src="<?php echo imgUrl($product['images'][0] ?? ''); ?>" alt="<?php echo htmlspecialchars($product['title']); ?>">
                                <div class="product-info">
                                    <h4><?php echo htmlspecialchars($product['title']); ?></h4>
                                    <div class="price"><?php echo $currency; ?><?php echo number_format($product['offer_price'] ?: $product['price']); ?></div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>

            <div class="sidebar">
                <div class="card">
                    <h2>Quick Actions</h2>
                    <div class="quick-actions">
                        <a href="index.php" class="quick-action-btn">
                            <i class="fas fa-shopping-cart"></i>
                            <span>Shop Now</span>
                        </a>
                        <a href="orders.php" class="quick-action-btn">
                            <i class="fas fa-box"></i>
                            <span>My Orders</span>
                        </a>
                        <a href="wishlist.php" class="quick-action-btn">
                            <i class="fas fa-heart"></i>
                            <span>Wishlist</span>
                        </a>
                        <a href="profile.php" class="quick-action-btn">
                            <i class="fas fa-user"></i>
                            <span>Profile</span>
                        </a>
                        <a href="settings.php" class="quick-action-btn">
                            <i class="fas fa-cog"></i>
                            <span>Settings</span>
                        </a>
                    </div>
                </div>

                <?php if (!empty($recentlyPurchased)): ?>
                    <div class="card">
                        <h2>Recently Purchased</h2>
                        <div class="product-grid">
                            <?php foreach ($recentlyPurchased as $product): ?>
                                <div class="product-card">
                                    <img src="<?php echo imgUrl($product['image'] ?? ''); ?>" alt="<?php echo htmlspecialchars($product['title']); ?>">
                                    <div class="product-info">
                                        <h4><?php echo htmlspecialchars(substr($product['title'], 0, 30)); ?>...</h4>
                                        <div class="price"><?php echo $currency; ?><?php echo number_format($product['offer_price'] ?: $product['price']); ?></div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endif; ?>

                <div class="card">
                    <h2>Account Info</h2>
                    <div style="color: #666; font-size: 14px;">
                        <p style="margin-bottom: 10px;"><i class="fas fa-envelope"></i> <?php echo htmlspecialchars($user['email']); ?></p>
                        <p style="margin-bottom: 10px;"><i class="fas fa-calendar"></i> Member since <?php echo date('F Y', strtotime($user['created_at'])); ?></p>
                        <p><i class="fas fa-circle" style="color: #28a745; font-size: 10px;"></i> Account Active</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
