<?php
ob_start();

// my_bids.php - User's Complete Bid History
// File location: /bidwheels.net/user/my_bids.php

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

requireLogin();

$user_id = $_SESSION['user_id'];

// Get all user's bids
$all_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,
           (SELECT image_path FROM product_images WHERE product_id = p.id AND is_featured = 1 LIMIT 1) as product_image
    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
");

// Get stats
$total_bids = count($all_bids);
$active_bids_count = 0;
$won_bids_count = 0;
$lost_bids_count = 0;
$total_spent = 0;

foreach ($all_bids as $bid) {
    $auction_ended = (strtotime($bid['end_time']) < time());
    $is_winning = ($bid['amount'] == $bid['current_highest']);
    
    if (!$auction_ended && $is_winning && !$bid['is_reserved']) {
        $active_bids_count++;
    }
    if ($auction_ended && $is_winning) {
        $won_bids_count++;
        $total_spent += $bid['amount'];
    }
    if ($auction_ended && !$is_winning) {
        $lost_bids_count++;
    }
}

// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 15;
$offset = ($page - 1) * $per_page;
$total_pages = ceil($total_bids / $per_page);
$paginated_bids = array_slice($all_bids, $offset, $per_page);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Bids - <?php echo SITE_NAME; ?></title>
    
    <style>
        .bids-page {
            background: linear-gradient(135deg, #EFEBE0 0%, #e5e0d4 100%);
            min-height: 100vh;
            padding: 40px 0;
        }
        
        .bids-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 24px;
        }
        
        /* Header */
        .page-header {
            margin-bottom: 30px;
        }
        
        .page-header h1 {
            font-size: 32px;
            font-weight: 800;
            background: linear-gradient(135deg, #1a1a1a 0%, #FF8C3E 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 8px;
        }
        
        .page-header p {
            color: #666;
            font-size: 14px;
        }
        
        /* Stats Cards */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(5, 1fr);
            gap: 15px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: white;
            border-radius: 16px;
            padding: 15px 12px;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.04);
            transition: all 0.3s;
        }
        
        .stat-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(255,140,62,0.1);
        }
        
        .stat-card .number {
            font-size: 28px;
            font-weight: 800;
            color: #FF8C3E;
            line-height: 1.2;
        }
        
        .stat-card .label {
            font-size: 12px;
            color: #888;
            margin-top: 5px;
        }
        
        /* Filter Bar */
        .filter-bar {
            display: flex;
            gap: 10px;
            margin-bottom: 25px;
            flex-wrap: wrap;
        }
        
        .filter-btn {
            padding: 8px 20px;
            background: white;
            border: none;
            border-radius: 30px;
            font-size: 13px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            color: #666;
        }
        
        .filter-btn.active {
            background: #FF8C3E;
            color: white;
        }
        
        .filter-btn:hover:not(.active) {
            background: #f0f0f0;
        }
        
        /* Bids Table */
        .bids-table {
            width: 100%;
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.04);
        }
        
        .bids-table th,
        .bids-table td {
            padding: 14px 16px;
            text-align: left;
            vertical-align: middle;
        }
        
        .bids-table th {
            background: #f8f9fa;
            font-weight: 600;
            color: #333;
            font-size: 13px;
        }
        
        .bids-table td {
            border-bottom: 1px solid #f0f0f0;
            font-size: 14px;
            color: #555;
        }
        
        .bids-table tr:last-child td {
            border-bottom: none;
        }
        
        .bids-table tr:hover td {
            background: #fef5ee;
        }
        
        /* Product Cell */
        .product-cell {
            display: flex;
            align-items: center;
            gap: 12px;
        }
        
        .product-thumb {
            width: 50px;
            height: 50px;
            border-radius: 10px;
            object-fit: cover;
            background: #f0f0f0;
        }
        
        .product-info {
            flex: 1;
        }
        
        .product-name {
            font-weight: 600;
            color: #1a1a1a;
            text-decoration: none;
        }
        
        .product-name:hover {
            color: #FF8C3E;
        }
        
        .product-category {
            font-size: 11px;
            color: #888;
            margin-top: 3px;
        }
        
        /* Bid Amount */
        .bid-amount {
            font-weight: 700;
            color: #FF8C3E;
        }
        
        /* Status Badges */
        .status-winning {
            background: #d4edda;
            color: #155724;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-outbid {
            background: #f8d7da;
            color: #721c24;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-won {
            background: #d4edda;
            color: #155724;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-lost {
            background: #e2e3e5;
            color: #383d41;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-reserved {
            background: #fff3cd;
            color: #856404;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-ended {
            background: #e2e3e5;
            color: #383d41;
            padding: 4px 12px;
            border-radius: 30px;
            font-size: 11px;
            font-weight: 600;
            display: inline-block;
        }
        
        /* Buttons */
        .btn-action {
            background: #FF8C3E;
            color: white;
            padding: 5px 14px;
            border-radius: 30px;
            text-decoration: none;
            font-size: 12px;
            font-weight: 600;
            display: inline-block;
            transition: all 0.3s;
        }
        
        .btn-action:hover {
            background: #e67a22;
        }
        
        .btn-outline-action {
            background: transparent;
            border: 1px solid #FF8C3E;
            color: #FF8C3E;
            padding: 4px 13px;
            border-radius: 30px;
            text-decoration: none;
            font-size: 12px;
            font-weight: 600;
            display: inline-block;
            transition: all 0.3s;
        }
        
        .btn-outline-action:hover {
            background: #FF8C3E;
            color: white;
        }
        
        /* Pagination */
        .pagination {
            display: flex;
            justify-content: center;
            gap: 8px;
            margin-top: 30px;
            flex-wrap: wrap;
        }
        
        .pagination a {
            padding: 8px 14px;
            background: white;
            color: #333;
            text-decoration: none;
            border-radius: 10px;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        .pagination a:hover {
            background: #FF8C3E;
            color: white;
        }
        
        .pagination a.active {
            background: #FF8C3E;
            color: white;
        }
        
        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 60px;
            background: white;
            border-radius: 20px;
        }
        
        .empty-state i {
            font-size: 60px;
            color: #ddd;
            margin-bottom: 15px;
        }
        
        .empty-state h3 {
            font-size: 20px;
            color: #333;
            margin-bottom: 10px;
        }
        
        .empty-state p {
            color: #888;
            margin-bottom: 20px;
        }
        
        /* Responsive */
        @media (max-width: 1200px) {
            .stats-grid {
                grid-template-columns: repeat(3, 1fr);
            }
        }
        
        @media (max-width: 992px) {
            .bids-table th,
            .bids-table td {
                padding: 10px 12px;
            }
            
            .product-thumb {
                width: 40px;
                height: 40px;
            }
            
            .product-name {
                font-size: 13px;
            }
        }
        
        @media (max-width: 768px) {
            .bids-container {
                padding: 0 16px;
            }
            
            .page-header h1 {
                font-size: 28px;
            }
            
            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
                gap: 10px;
            }
            
            .stat-card .number {
                font-size: 22px;
            }
            
            .bids-table {
                display: block;
                overflow-x: auto;
            }
            
            .filter-bar {
                justify-content: center;
            }
        }
        
        @media (max-width: 480px) {
            .stats-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>

<div class="bids-page">
    <div class="bids-container">
        <!-- Header -->
        <div class="page-header">
            <h1>My Bids</h1>
            <p>Track all your bidding activity, from active auctions to won items</p>
        </div>
        
        <!-- Stats Cards -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="number"><?php echo $total_bids; ?></div>
                <div class="label">Total Bids</div>
            </div>
            <div class="stat-card">
                <div class="number"><?php echo $active_bids_count; ?></div>
                <div class="label">Active Bids</div>
            </div>
            <div class="stat-card">
                <div class="number"><?php echo $won_bids_count; ?></div>
                <div class="label">Auctions Won</div>
            </div>
            <div class="stat-card">
                <div class="number"><?php echo $lost_bids_count; ?></div>
                <div class="label">Lost</div>
            </div>
            <div class="stat-card">
                <div class="number"><?php echo formatPrice($total_spent); ?></div>
                <div class="label">Total Spent</div>
            </div>
        </div>
        
        <!-- Filter Buttons -->
        <div class="filter-bar">
            <button class="filter-btn active" data-filter="all">All Bids</button>
            <button class="filter-btn" data-filter="active">Active Bids</button>
            <button class="filter-btn" data-filter="won">Won</button>
            <button class="filter-btn" data-filter="lost">Lost</button>
            <button class="filter-btn" data-filter="outbid">Outbid</button>
        </div>
        
        <!-- Bids Table -->
        <?php if (count($paginated_bids) > 0): ?>
        <table class="bids-table" id="bidsTable">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Bid Amount</th>
                    <th>Current Highest</th>
                    <th>Status</th>
                    <th>Placed On</th>
                    <th>Ends / Ended</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($paginated_bids as $bid): ?>
                    <?php
                    $auction_ended = (strtotime($bid['end_time']) < time());
                    $is_winning = ($bid['amount'] == $bid['current_highest']);
                    $is_reserved = $bid['is_reserved'];
                    
                    if ($is_reserved) {
                        $status = 'reserved';
                        $status_text = 'Reserved';
                        $status_class = 'status-reserved';
                    } elseif ($auction_ended) {
                        if ($is_winning) {
                            $status = 'won';
                            $status_text = 'Won';
                            $status_class = 'status-won';
                        } else {
                            $status = 'lost';
                            $status_text = 'Lost';
                            $status_class = 'status-lost';
                        }
                    } else {
                        if ($is_winning) {
                            $status = 'active';
                            $status_text = 'Winning';
                            $status_class = 'status-winning';
                        } else {
                            $status = 'outbid';
                            $status_text = 'Outbid';
                            $status_class = 'status-outbid';
                        }
                    }
                    ?>
                    <tr data-status="<?php echo $status; ?>">
                        <td>
                            <div class="product-cell">
                                <img class="product-thumb" src="<?php echo $bid['product_image'] ? SITE_URL . '/uploads/products/' . $bid['product_image'] : SITE_URL . '/assets/images/no-image.jpg'; ?>" alt="<?php echo htmlspecialchars($bid['product_name']); ?>">
                                <div class="product-info">
                                    <a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" class="product-name"><?php echo htmlspecialchars($bid['product_name']); ?></a>
                                </div>
                            </div>
                        </td>
                        <td class="bid-amount"><?php echo formatPrice($bid['amount']); ?></td>
                        <td><?php echo formatPrice($bid['current_highest']); ?></td>
                        <td><span class="<?php echo $status_class; ?>"><?php echo $status_text; ?></span></td>
                        <td><?php echo date('M d, Y', strtotime($bid['created_at'])); ?></td>
                        <td>
                            <?php if ($auction_ended): ?>
                                <?php echo date('M d, Y', strtotime($bid['end_time'])); ?>
                            <?php else: ?>
                                <?php echo getRemainingTime($bid['end_time']); ?>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($status == 'won'): ?>
                                <a href="<?php echo SITE_URL; ?>/user/payment.php?product_id=<?php echo $bid['product_id']; ?>" class="btn-action">Pay Now</a>
                            <?php elseif ($status == 'outbid'): ?>
                                <a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" class="btn-outline-action">Bid Again</a>
                            <?php elseif ($status == 'active'): ?>
                                <a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" class="btn-action">Monitor</a>
                            <?php else: ?>
                                <a href="<?php echo SITE_URL; ?>/product/detail.php?id=<?php echo $bid['product_id']; ?>" class="btn-outline-action">View</a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <!-- Pagination -->
        <?php if ($total_pages > 1): ?>
        <div class="pagination">
            <?php if ($page > 1): ?>
                <a href="?page=<?php echo $page - 1; ?>"><i class="fas fa-chevron-left"></i> Prev</a>
            <?php endif; ?>
            
            <?php
            $start_page = max(1, $page - 2);
            $end_page = min($total_pages, $page + 2);
            
            if ($start_page > 1) {
                echo '<a href="?page=1">1</a>';
                if ($start_page > 2) echo '<span style="padding: 8px 6px;">...</span>';
            }
            
            for ($i = $start_page; $i <= $end_page; $i++): ?>
                <a href="?page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
            <?php endfor;
            
            if ($end_page < $total_pages) {
                if ($end_page < $total_pages - 1) echo '<span style="padding: 8px 6px;">...</span>';
                echo '<a href="?page=' . $total_pages . '">' . $total_pages . '</a>';
            }
            ?>
            
            <?php if ($page < $total_pages): ?>
                <a href="?page=<?php echo $page + 1; ?>">Next <i class="fas fa-chevron-right"></i></a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
        
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-gavel"></i>
            <h3>No bids yet</h3>
            <p>You haven't placed any bids yet. Start bidding on your dream vehicle!</p>
            <a href="<?php echo SITE_URL; ?>/product/index.php" class="btn-action" style="padding: 10px 25px;">Browse Vehicles</a>
        </div>
        <?php endif; ?>
    </div>
</div>

<script>
// Filter functionality
const filterBtns = document.querySelectorAll('.filter-btn');
const tableRows = document.querySelectorAll('#bidsTable tbody tr');

filterBtns.forEach(btn => {
    btn.addEventListener('click', () => {
        // Update active button
        filterBtns.forEach(b => b.classList.remove('active'));
        btn.classList.add('active');
        
        const filter = btn.getAttribute('data-filter');
        
        // Filter rows
        tableRows.forEach(row => {
            if (filter === 'all') {
                row.style.display = '';
            } else {
                const status = row.getAttribute('data-status');
                if (status === filter) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            }
        });
    });
});
</script>

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