<?php
ob_start();

// dashboard.php - User Dashboard with Premium Design
// File location: /bidwheels.net/user/dashboard.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once '../includes/header.php';

requireLogin();

$user_id = $_SESSION['user_id'];

// Get user information
$user = fetchOne("SELECT * FROM users WHERE id = $user_id");

// Get user's bids
$my_bids = fetchAll("
    SELECT b.*, p.name as product_name, p.id as product_id, p.end_time, p.is_reserved,
           (SELECT MAX(amount) FROM bids WHERE product_id = p.id) as current_highest
    FROM bids b
    LEFT JOIN products p ON b.product_id = p.id
    WHERE b.user_id = $user_id
    ORDER BY b.created_at DESC
    LIMIT 20
");

// Get total bids placed
$total_bids = getCount('bids', "user_id = $user_id");

// Get total comments
$total_comments = getCount('comments', "user_id = $user_id");

// Get won auctions
$won_auctions = fetchAll("
    SELECT p.*, b.amount as winning_bid, b.id as bid_id
    FROM products p
    INNER JOIN bids b ON p.id = b.product_id
    WHERE b.user_id = $user_id 
    AND p.end_time < NOW()
    AND b.amount = (SELECT MAX(amount) FROM bids WHERE product_id = p.id)
    ORDER BY p.end_time DESC
    LIMIT 5
");

// Get active bids (current highest bidder)
$active_bids = fetchAll("
    SELECT b.*, p.name as product_name, p.id as product_id, p.end_time,
           (SELECT MAX(amount) FROM bids WHERE product_id = p.id) as current_highest
    FROM bids b
    LEFT JOIN products p ON b.product_id = p.id
    WHERE b.user_id = $user_id 
    AND p.end_time > NOW()
    AND p.is_reserved = 0
    AND b.amount = (SELECT MAX(amount) FROM bids WHERE product_id = p.id)
    GROUP BY p.id
    ORDER BY b.created_at DESC
    LIMIT 5
");

// Get outbid bids
$outbid_bids = fetchAll("
    SELECT b.*, p.name as product_name, p.id as product_id, p.end_time,
           (SELECT MAX(amount) FROM bids WHERE product_id = p.id) as current_highest
    FROM bids b
    LEFT JOIN products p ON b.product_id = p.id
    WHERE b.user_id = $user_id 
    AND p.end_time > NOW()
    AND p.is_reserved = 0
    AND b.amount < (SELECT MAX(amount) FROM bids WHERE product_id = p.id)
    GROUP BY p.id
    ORDER BY b.created_at DESC
    LIMIT 5
");

// Get featured products for recommendations
$featured_products = fetchAll("
    SELECT p.*, c.name as category_name,
           (SELECT MAX(amount) FROM bids WHERE product_id = p.id) as highest_bid,
           (SELECT COUNT(*) FROM bids WHERE product_id = p.id) as total_bids
    FROM products p
    LEFT JOIN categories c ON p.category_id = c.id
    WHERE p.is_reserved = 0 AND p.end_time > NOW()
    ORDER BY RAND()
    LIMIT 6
");

// Get ending soon auctions
$ending_soon = fetchAll("
    SELECT p.*, c.name as category_name,
           (SELECT MAX(amount) FROM bids WHERE product_id = p.id) as highest_bid,
           (SELECT COUNT(*) FROM bids WHERE product_id = p.id) as total_bids
    FROM products p
    LEFT JOIN categories c ON p.category_id = c.id
    WHERE p.is_reserved = 0 AND p.end_time > NOW()
    ORDER BY p.end_time ASC
    LIMIT 6
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Dashboard - <?php echo SITE_NAME; ?></title>
    
    <style>
        .dashboard-page {
            background: linear-gradient(135deg, #EFEBE0 0%, #e5e0d4 100%);
            min-height: 100vh;
            padding: 30px 0;
        }
        
        .dashboard-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 24px;
        }
        
        /* Compact Stats Cards */
        .stats-row {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 15px;
            margin-bottom: 30px;
        }
        
        .stat-item {
            background: white;
            border-radius: 16px;
            padding: 15px 20px;
            display: flex;
            align-items: center;
            gap: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.04);
            transition: all 0.3s;
        }
        
        .stat-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(255,140,62,0.1);
        }
        
        .stat-icon {
            width: 45px;
            height: 45px;
            background: linear-gradient(135deg, #FF8C3E, #e67a22);
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .stat-icon i {
            font-size: 20px;
            color: white;
        }
        
        .stat-number {
            font-size: 24px;
            font-weight: 800;
            color: #1a1a1a;
            line-height: 1.2;
        }
        
        .stat-label {
            font-size: 11px;
            color: #888;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        /* Section Headers */
        .section-title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .section-title h2 {
            font-size: 18px;
            font-weight: 700;
            color: #1a1a1a;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .section-title h2 i {
            color: #FF8C3E;
        }
        
        .section-title a {
            color: #FF8C3E;
            text-decoration: none;
            font-size: 12px;
            font-weight: 500;
        }
        
        /* Products Grid */
        .products-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .product-card {
            background: white;
            border-radius: 16px;
            overflow: hidden;
            transition: all 0.3s;
            box-shadow: 0 2px 8px rgba(0,0,0,0.04);
        }
        
        .product-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 25px rgba(255,140,62,0.12);
        }
        
        .product-image {
            position: relative;
            aspect-ratio: 4/3;
            overflow: hidden;
            cursor: pointer;
        }
        
        .product-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.3s;
        }
        
        .product-card:hover .product-image img {
            transform: scale(1.05);
        }
        
        .product-badge {
            position: absolute;
            bottom: 8px;
            left: 8px;
            background: rgba(0,0,0,0.7);
            backdrop-filter: blur(4px);
            padding: 3px 8px;
            border-radius: 20px;
            font-size: 10px;
            color: white;
            display: flex;
            align-items: center;
            gap: 4px;
        }
        
        .product-badge i {
            color: #FF8C3E;
        }
        
        .product-info {
            padding: 12px;
        }
        
        .product-title {
            font-size: 14px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 5px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }
        
        .product-category {
            font-size: 10px;
            color: #FF8C3E;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            gap: 4px;
        }
        
        .product-price {
            display: flex;
            justify-content: space-between;
            align-items: baseline;
            margin-bottom: 8px;
        }
        
        .current-bid {
            font-size: 16px;
            font-weight: 700;
            color: #FF8C3E;
        }
        
        .starting-price {
            font-size: 10px;
            color: #aaa;
            text-decoration: line-through;
        }
        
        .product-time {
            font-size: 10px;
            color: #666;
            display: flex;
            align-items: center;
            gap: 4px;
            margin-bottom: 10px;
        }
        
        .product-btn {
            display: block;
            text-align: center;
            background: #1a1a1a;
            color: white;
            padding: 8px;
            border-radius: 30px;
            text-decoration: none;
            font-weight: 600;
            font-size: 12px;
            transition: all 0.3s;
        }
        
        .product-btn:hover {
            background: #FF8C3E;
        }
        
        /* Tables */
        .bids-table {
            width: 100%;
            background: white;
            border-radius: 16px;
            overflow: hidden;
        }
        
        .bids-table th,
        .bids-table td {
            padding: 12px 15px;
            text-align: left;
            font-size: 13px;
        }
        
        .bids-table th {
            background: #f8f9fa;
            font-weight: 600;
            color: #333;
        }
        
        .bids-table td {
            border-bottom: 1px solid #f0f0f0;
            color: #555;
        }
        
        .bids-table tr:last-child td {
            border-bottom: none;
        }
        
        .status-winning {
            background: #d4edda;
            color: #155724;
            padding: 3px 10px;
            border-radius: 20px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-outbid {
            background: #f8d7da;
            color: #721c24;
            padding: 3px 10px;
            border-radius: 20px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-won {
            background: #d4edda;
            color: #155724;
            padding: 3px 10px;
            border-radius: 20px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .btn-sm {
            background: #FF8C3E;
            color: white;
            padding: 4px 12px;
            border-radius: 20px;
            text-decoration: none;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .btn-sm-outline {
            background: transparent;
            border: 1px solid #FF8C3E;
            color: #FF8C3E;
            padding: 3px 11px;
            border-radius: 20px;
            text-decoration: none;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 30px;
            background: white;
            border-radius: 16px;
        }
        
        .empty-state i {
            font-size: 40px;
            color: #ddd;
            margin-bottom: 10px;
        }
        
        .empty-state p {
            color: #888;
            font-size: 13px;
        }
        
        /* Welcome Bar */
        .welcome-bar {
            background: white;
            border-radius: 16px;
            padding: 12px 20px;
            margin-bottom: 25px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .welcome-text h3 {
            font-size: 16px;
            font-weight: 600;
            color: #1a1a1a;
        }
        
        .welcome-text p {
            font-size: 12px;
            color: #888;
        }
        
        .profile-link {
            background: #f8f9fa;
            padding: 8px 18px;
            border-radius: 30px;
            text-decoration: none;
            color: #FF8C3E;
            font-size: 13px;
            font-weight: 500;
        }
        
        /* Responsive */
        @media (max-width: 1200px) {
            .products-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }
        
        @media (max-width: 992px) {
            .stats-row {
                grid-template-columns: repeat(2, 1fr);
            }
        }
        
        @media (max-width: 768px) {
            .dashboard-container {
                padding: 0 16px;
            }
            
            .stats-row {
                gap: 10px;
            }
            
            .stat-item {
                padding: 12px 15px;
            }
            
            .stat-number {
                font-size: 20px;
            }
            
            .products-grid {
                grid-template-columns: 1fr;
                gap: 15px;
            }
            
            .bids-table th,
            .bids-table td {
                padding: 10px 12px;
                font-size: 11px;
            }
            
            .welcome-bar {
                flex-direction: column;
                text-align: center;
            }
        }
        
        @media (max-width: 480px) {
            .stats-row {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>

<div class="dashboard-page">
    <div class="dashboard-container">
        <!-- Welcome Bar -->
        <div class="welcome-bar">
            <div class="welcome-text">
                <h3>Welcome back, <span style="color: #FF8C3E;"><?php echo htmlspecialchars($user['name']); ?></span>!</h3>
                <p><i class="fas fa-calendar-alt"></i> Member since <?php echo date('M d, Y', strtotime($user['created_at'])); ?></p>
            </div>
            <a href="profile.php" class="profile-link"><i class="fas fa-user-edit"></i> Edit Profile</a>
        </div>
        
        <!-- Compact Stats -->
        <div class="stats-row">
            <div class="stat-item">
                <div class="stat-icon"><i class="fas fa-gavel"></i></div>
                <div>
                    <div class="stat-number"><?php echo $total_bids; ?></div>
                    <div class="stat-label">Total Bids</div>
                </div>
            </div>
            <div class="stat-item">
                <div class="stat-icon"><i class="fas fa-trophy"></i></div>
                <div>
                    <div class="stat-number"><?php echo count($won_auctions); ?></div>
                    <div class="stat-label">Won</div>
                </div>
            </div>
            <div class="stat-item">
                <div class="stat-icon"><i class="fas fa-chart-line"></i></div>
                <div>
                    <div class="stat-number"><?php echo count($active_bids); ?></div>
                    <div class="stat-label">Leading</div>
                </div>
            </div>
            <div class="stat-item">
                <div class="stat-icon"><i class="fas fa-comment"></i></div>
                <div>
                    <div class="stat-number"><?php echo $total_comments; ?></div>
                    <div class="stat-label">Comments</div>
                </div>
            </div>
        </div>
        
        <!-- Active Bids Table -->
        <div class="section-title">
            <h2><i class="fas fa-tachometer-alt"></i> Active Bids (Leading)</h2>
            <a href="my_bids.php">View All <i class="fas fa-arrow-right"></i></a>
        </div>
        <?php if (count($active_bids) > 0): ?>
        <table class="bids-table">
            <thead>
                <tr><th>Product</th><th>My Bid</th><th>Current</th><th>Ends In</th><th>Action</th></tr>
            </thead>
            <tbody>
                <?php foreach ($active_bids as $bid): ?>
                <tr>
                    <td><a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" style="color:#333; text-decoration:none; font-weight:500;"><?php echo htmlspecialchars($bid['product_name']); ?></a></td>
                    <td class="bid-amount" style="color:#FF8C3E; font-weight:700;"><?php echo formatPrice($bid['amount']); ?></td>
                    <td><?php echo formatPrice($bid['current_highest']); ?></td>
                    <td><?php echo getRemainingTime($bid['end_time']); ?></td>
                    <td><a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" class="btn-sm">Monitor</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php else: ?>
        <div class="empty-state"><i class="fas fa-gavel"></i><p>No active bids yet.</p></div>
        <?php endif; ?>
        
        <!-- Outbid Bids Table -->
        <?php if (count($outbid_bids) > 0): ?>
        <div class="section-title" style="margin-top: 25px;">
            <h2><i class="fas fa-frown"></i> Outbid</h2>
            <a href="my_bids.php">View All <i class="fas fa-arrow-right"></i></a>
        </div>
        <table class="bids-table">
            <thead>
                <tr><th>Product</th><th>My Bid</th><th>Current</th><th>Ends In</th><th>Action</th></tr>
            </thead>
            <tbody>
                <?php foreach ($outbid_bids as $bid): ?>
                <tr>
                    <td><a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" style="color:#333; text-decoration:none; font-weight:500;"><?php echo htmlspecialchars($bid['product_name']); ?></a></td>
                    <td style="color:#FF8C3E; font-weight:700;"><?php echo formatPrice($bid['amount']); ?></td>
                    <td><?php echo formatPrice($bid['current_highest']); ?></td>
                    <td><?php echo getRemainingTime($bid['end_time']); ?></td>
                    <td><a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" class="btn-sm-outline">Bid Again</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
        
        <!-- Ending Soon Auctions -->
        <div class="section-title" style="margin-top: 30px;">
            <h2><i class="fas fa-clock"></i> Ending Soon</h2>
            <a href="<?php echo SITE_URL; ?>/product/index.php?sort=ending_soon">View All <i class="fas fa-arrow-right"></i></a>
        </div>
        <div class="products-grid">
            <?php foreach ($ending_soon as $product): ?>
            <div class="product-card">
                <div class="product-image" onclick="window.location.href='<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $product['id']; ?>'">
                    <img src="<?php echo getFeaturedImage($product['id']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                    <div class="product-badge"><i class="fas fa-gavel"></i> <?php echo $product['total_bids']; ?> bids</div>
                </div>
                <div class="product-info">
                    <h3 class="product-title"><?php echo htmlspecialchars($product['name']); ?></h3>
                    <div class="product-category"><i class="fas fa-tag"></i> <?php echo htmlspecialchars($product['category_name']); ?></div>
                    <div class="product-price">
                        <span class="current-bid"><?php echo formatPrice($product['highest_bid'] ?? $product['starting_price']); ?></span>
                        <span class="starting-price"><?php echo formatPrice($product['starting_price']); ?></span>
                    </div>
                    <div class="product-time"><i class="fas fa-clock"></i> <?php echo getRemainingTime($product['end_time']); ?></div>
                    <a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $product['id']; ?>" class="product-btn">Place Bid <i class="fas fa-arrow-right"></i></a>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Recommended for You -->
        <div class="section-title">
            <h2><i class="fas fa-star"></i> Recommended for You</h2>
            <a href="<?php echo SITE_URL; ?>/product/index.php">View All <i class="fas fa-arrow-right"></i></a>
        </div>
        <div class="products-grid">
            <?php foreach ($featured_products as $product): ?>
            <div class="product-card">
                <div class="product-image" onclick="window.location.href='<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $product['id']; ?>'">
                    <img src="<?php echo getFeaturedImage($product['id']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                    <div class="product-badge"><i class="fas fa-gavel"></i> <?php echo $product['total_bids']; ?> bids</div>
                </div>
                <div class="product-info">
                    <h3 class="product-title"><?php echo htmlspecialchars($product['name']); ?></h3>
                    <div class="product-category"><i class="fas fa-tag"></i> <?php echo htmlspecialchars($product['category_name']); ?></div>
                    <div class="product-price">
                        <span class="current-bid"><?php echo formatPrice($product['highest_bid'] ?? $product['starting_price']); ?></span>
                        <span class="starting-price"><?php echo formatPrice($product['starting_price']); ?></span>
                    </div>
                    <div class="product-time"><i class="fas fa-clock"></i> <?php echo getRemainingTime($product['end_time']); ?></div>
                    <a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $product['id']; ?>" class="product-btn">Place Bid <i class="fas fa-arrow-right"></i></a>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Won Auctions -->
        <?php if (count($won_auctions) > 0): ?>
        <div class="section-title" style="margin-top: 10px;">
            <h2><i class="fas fa-trophy"></i> Recently Won</h2>
        </div>
        <table class="bids-table">
            <thead><tr><th>Product</th><th>Winning Bid</th><th>Ended</th><th>Action</th></tr></thead>
            <tbody>
                <?php foreach ($won_auctions as $won): ?>
                <tr>
                    <td><a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $won['id']; ?>" style="color:#333; text-decoration:none; font-weight:500;"><?php echo htmlspecialchars($won['name']); ?></a></td>
                    <td style="color:#FF8C3E; font-weight:700;"><?php echo formatPrice($won['winning_bid']); ?></td>
                    <td><?php echo date('M d, Y', strtotime($won['end_time'])); ?></td>
                    <td><a href="<?php echo SITE_URL; ?>/user/payment.php?product_id=<?php echo $won['id']; ?>" class="btn-sm">Pay Now</a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>