<?php
// index.php - Admin Dashboard
// File location: /bidwheels.net/admin/index.php

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

requireAdmin();

// Get statistics
$total_users = getCount('users', "is_dummy = 0");
$total_dummy_users = getCount('users', "is_dummy = 1");
$total_products = getCount('products');
$total_active_products = getCount('products', "is_reserved = 0 AND end_time > NOW()");
$total_reserved = getCount('products', "is_reserved = 1");
$total_bids = getCount('bids');
$total_comments = getCount('comments');
$total_payments = getCount('payments');
$total_blog_posts = getCount('blog_posts');

// Recent products
$recent_products = fetchAll("
    SELECT p.*, c.name as category_name,
           (SELECT COUNT(*) FROM bids WHERE product_id = p.id) as bid_count
    FROM products p
    LEFT JOIN categories c ON p.category_id = c.id
    ORDER BY p.created_at DESC
    LIMIT 5
");

// Recent bids
$recent_bids = fetchAll("
    SELECT b.*, p.name as product_name, u.name as user_name
    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 10
");

// Recent users
$recent_users = fetchAll("
    SELECT * FROM users 
    WHERE is_dummy = 0
    ORDER BY created_at DESC 
    LIMIT 5
");
?>

<div class="admin-dashboard">
    <h1 class="page-title">Dashboard Overview</h1>
    
    <!-- Stats Cards -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-users"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_users; ?></div>
                <div class="stat-label">Real Users</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-robot"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_dummy_users; ?></div>
                <div class="stat-label">Dummy Users</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-car"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_products; ?></div>
                <div class="stat-label">Total Products</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-clock"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_active_products; ?></div>
                <div class="stat-label">Active Listings</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-ban"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_reserved; ?></div>
                <div class="stat-label">Reserved</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-gavel"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_bids; ?></div>
                <div class="stat-label">Total Bids</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-comments"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_comments; ?></div>
                <div class="stat-label">Comments</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-credit-card"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_payments; ?></div>
                <div class="stat-label">Payments</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon"><i class="fas fa-blog"></i></div>
            <div class="stat-info">
                <div class="stat-number"><?php echo $total_blog_posts; ?></div>
                <div class="stat-label">Blog Posts</div>
            </div>
        </div>
    </div>
    
    <!-- Recent Products -->
    <div class="admin-section">
        <div class="section-header">
            <h2>Recent Listings</h2>
            <a href="products.php" class="btn-link">View All <i class="fas fa-arrow-right"></i></a>
        </div>
        <div class="table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Starting Price</th>
                        <th>Bids</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recent_products as $product): ?>
                        <tr>
                            <td><?php echo $product['id']; ?></td>
                            <td><?php echo htmlspecialchars($product['name']); ?></td>
                            <td><?php echo htmlspecialchars($product['category_name']); ?></td>
                            <td><?php echo formatPrice($product['starting_price']); ?></td>
                            <td><?php echo $product['bid_count']; ?></td>
                            <td>
                                <?php if ($product['is_reserved']): ?>
                                    <span class="badge badge-danger">Reserved</span>
                                <?php elseif (strtotime($product['end_time']) < time()): ?>
                                    <span class="badge badge-dark">Ended</span>
                                <?php else: ?>
                                    <span class="badge badge-success">Active</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="edit_product.php?id=<?php echo $product['id']; ?>" class="btn-icon"><i class="fas fa-edit"></i></a>
                                <a href="delete_product.php?id=<?php echo $product['id']; ?>" class="btn-icon btn-danger" onclick="return confirm('Delete this product?')"><i class="fas fa-trash"></i></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- Recent Bids -->
    <div class="admin-section">
        <div class="section-header">
            <h2>Recent Bids</h2>
            <a href="bids.php" class="btn-link">View All <i class="fas fa-arrow-right"></i></a>
        </div>
        <div class="table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Bidder</th>
                        <th>Amount</th>
                        <th>Type</th>
                        <th>Time</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recent_bids as $bid): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($bid['product_name']); ?></td>
                            <td><?php echo htmlspecialchars($bid['user_name'] ?? 'System'); ?></td>
                            <td class="bid-amount"><?php echo formatPrice($bid['amount']); ?></td>
                            <td><?php echo $bid['is_system_bid'] ? '<span class="badge badge-info">Auto</span>' : '<span class="badge badge-primary">User</span>'; ?></td>
                            <td><?php echo timeAgo($bid['created_at']); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <!-- Recent Users -->
    <div class="admin-section">
        <div class="section-header">
            <h2>Recent Registrations</h2>
            <a href="users.php" class="btn-link">View All <i class="fas fa-arrow-right"></i></a>
        </div>
        <div class="table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Registered</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recent_users as $user): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($user['name']); ?></td>
                            <td><?php echo htmlspecialchars($user['email']); ?></td>
                            <td><?php echo htmlspecialchars($user['phone']); ?></td>
                            <td><?php echo date('M d, Y', strtotime($user['created_at'])); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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