<?php
// users.php - Manage Users
// File location: /bidwheels.net/admin/users.php

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

requireAdmin();

// Handle delete user
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $user = fetchOne("SELECT is_dummy FROM users WHERE id = $id");
    
    if ($user && $user['is_dummy'] == 1) {
        // Delete dummy user's bids and comments first
        delete('bids', "user_id = $id");
        delete('comments', "user_id = $id");
        delete('users', "id = $id");
        setSuccess('Dummy user deleted successfully.');
    } elseif ($user && $user['is_dummy'] == 0) {
        setError('Cannot delete real users for demo purposes.');
    }
    redirect('/admin/users.php');
}

// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * 20;
$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';

if ($filter == 'dummy') {
    $where = "is_dummy = 1";
} elseif ($filter == 'real') {
    $where = "is_dummy = 0";
} else {
    $where = "1=1";
}

$users = fetchAll("
    SELECT u.*, 
           (SELECT COUNT(*) FROM bids WHERE user_id = u.id) as total_bids,
           (SELECT COUNT(*) FROM comments WHERE user_id = u.id) as total_comments
    FROM users u
    WHERE $where
    ORDER BY u.created_at DESC
    LIMIT $offset, 20
");

$total = getCount('users', $where);
$total_pages = ceil($total / 20);
?>

<div class="admin-page">
    <div class="page-header">
        <h1 class="page-title">Manage Users</h1>
        <a href="../dummy/create_accounts.php" class="btn btn-primary"><i class="fas fa-robot"></i> Create Dummy Users</a>
    </div>
    
    <?php echo getMessage(); ?>
    
    <div class="filter-tabs">
        <a href="?filter=all" class="filter-tab <?php echo $filter == 'all' ? 'active' : ''; ?>">All Users</a>
        <a href="?filter=real" class="filter-tab <?php echo $filter == 'real' ? 'active' : ''; ?>">Real Users</a>
        <a href="?filter=dummy" class="filter-tab <?php echo $filter == 'dummy' ? 'active' : ''; ?>">Dummy Users</a>
    </div>
    
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Phone</th>
                    <th>Type</th>
                    <th>Bids</th>
                    <th>Comments</th>
                    <th>Joined</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($users) > 0): ?>
                    <?php foreach ($users as $user): ?>
                        <tr>
                            <td><?php echo $user['id']; ?></td>
                            <td><?php echo htmlspecialchars($user['name']); ?></td>
                            <td><?php echo htmlspecialchars($user['email']); ?></td>
                            <td><?php echo htmlspecialchars($user['phone']); ?></td>
                            <td>
                                <?php if ($user['role'] == 'admin'): ?>
                                    <span class="badge badge-primary">Admin</span>
                                <?php elseif ($user['is_dummy']): ?>
                                    <span class="badge badge-info">Dummy</span>
                                <?php else: ?>
                                    <span class="badge badge-success">Real User</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo $user['total_bids']; ?></td>
                            <td><?php echo $user['total_comments']; ?></td>
                            <td><?php echo date('M d, Y', strtotime($user['created_at'])); ?></td>
                            <td class="actions">
                                <?php if ($user['is_dummy'] == 1 && $user['role'] != 'admin'): ?>
                                    <a href="?delete=<?php echo $user['id']; ?>" class="btn-icon btn-danger" onclick="return confirm('Delete this dummy user?')"><i class="fas fa-trash"></i></a>
                                <?php else: ?>
                                    <span style="color: #999;">Cannot delete</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="9" style="text-align: center;">No users 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; ?>&filter=<?php echo $filter; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.filter-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}
.filter-tab {
    padding: 8px 20px;
    background: #f8f9fa;
    color: #333;
    text-decoration: none;
    border-radius: 5px;
}
.filter-tab.active {
    background: #e63946;
    color: white;
}
.badge {
    padding: 4px 8px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: bold;
}
.badge-primary {
    background: #007bff;
    color: white;
}
.badge-success {
    background: #28a745;
    color: white;
}
.badge-info {
    background: #17a2b8;
    color: white;
}
</style>

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