<?php
// bids.php - View All Bids
// File location: /bidwheels.net/admin/bids.php

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

requireAdmin();

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * 50;

$bids = fetchAll("
    SELECT b.*, p.name as product_name, u.name as user_name, u.is_dummy
    FROM bids b
    LEFT JOIN products p ON b.product_id = p.id
    LEFT JOIN users u ON b.user_id = u.id
    ORDER BY b.created_at DESC
    LIMIT $offset, 50
");

$total = getCount('bids');
$total_pages = ceil($total / 50);
?>

<div class="admin-page">
    <h1 class="page-title">All Bids</h1>
    
    <div class="stats-box">
        <div class="stat">
            <span class="stat-label">Total Bids</span>
            <span class="stat-value"><?php echo $total; ?></span>
        </div>
        <div class="stat">
            <span class="stat-label">Real User Bids</span>
            <span class="stat-value"><?php echo getCount('bids', 'is_system_bid = 0'); ?></span>
        </div>
        <div class="stat">
            <span class="stat-label">System Bids</span>
            <span class="stat-value"><?php echo getCount('bids', 'is_system_bid = 1'); ?></span>
        </div>
    </div>
    
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Product</th>
                    <th>Bidder</th>
                    <th>Amount</th>
                    <th>Type</th>
                    <th>Time</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($bids) > 0): ?>
                    <?php foreach ($bids as $bid): ?>
                        <tr>
                            <td><?php echo $bid['id']; ?></td>
                            <td><a href="edit_product.php?id=<?php echo $bid['product_id']; ?>"><?php echo htmlspecialchars($bid['product_name']); ?></a></td>
                            <td>
                                <?php echo htmlspecialchars($bid['user_name'] ?? 'System'); ?>
                                <?php if ($bid['is_dummy']): ?>
                                    <span class="badge badge-info">Dummy</span>
                                <?php endif; ?>
                            </td>
                            <td class="bid-amount"><?php echo formatPrice($bid['amount']); ?></td>
                            <td>
                                <?php if ($bid['is_system_bid']): ?>
                                    <span class="badge badge-warning">Auto Bid</span>
                                <?php else: ?>
                                    <span class="badge badge-success">User Bid</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo date('M d, Y g:i A', strtotime($bid['created_at'])); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="6" style="text-align: center;">No bids found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <?php if ($total_pages > 1): ?>
        <div class="pagination">
            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <a href="?page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.stats-box {
    background: white;
    padding: 20px;
    border-radius: 5px;
    margin-bottom: 25px;
    display: flex;
    justify-content: center;
    gap: 50px;
}
.stat {
    text-align: center;
}
.stat-label {
    display: block;
    font-size: 14px;
    color: #666;
}
.stat-value {
    display: block;
    font-size: 36px;
    font-weight: bold;
    color: #e63946;
}
.badge {
    padding: 3px 6px;
    border-radius: 3px;
    font-size: 10px;
    font-weight: bold;
    margin-left: 5px;
}
.badge-success {
    background: #d4edda;
    color: #155724;
}
.badge-warning {
    background: #fff3cd;
    color: #856404;
}
.badge-info {
    background: #d1ecf1;
    color: #0c5460;
}
.bid-amount {
    color: #e63946;
    font-weight: bold;
}
</style>

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