<?php
$pageTitle = 'User Management';
require_once 'includes/header.php';

// Handle status change
if (isset($_GET['action']) && isset($_GET['id'])) {
    $userId = intval($_GET['id']);
    $action = sanitize($_GET['action']);
    if (in_array($action, ['active', 'inactive', 'banned'])) {
        $stmt = $db->prepare("UPDATE users SET status = ? WHERE id = ? AND role = 'user'");
        $stmt->execute([$action, $userId]);
        logActivity($_SESSION['user_id'], 'user_status_changed', "Changed user #$userId status to $action");
        setFlash('success', 'User status updated.');
        redirect(ADMIN_URL . 'users.php');
    }
}

$users = $db->query("
    SELECT u.*, 
    (SELECT COUNT(*) FROM bookings b WHERE b.user_id = u.id) as booking_count
    FROM users u WHERE u.role = 'user' ORDER BY u.created_at DESC
")->fetchAll();
?>

<div class="page-header">
    <h1><i class="fas fa-users text-primary"></i> User Management</h1>
    <span style="color:var(--gray);">Total Users: <?= count($users) ?></span>
</div>

<div class="admin-card">
    <div class="card-body" style="padding:0;">
        <div class="table-responsive">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>User</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Bookings</th>
                        <th>Status</th>
                        <th>Joined</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($users as $i => $u): ?>
                        <tr>
                            <td><?= $i + 1 ?></td>
                            <td>
                                <div style="display:flex;align-items:center;gap:10px;">
                                    <img src="https://ui-avatars.com/api/?name=<?= urlencode($u['name']) ?>&background=FF6B35&color=fff&size=36" alt="" style="width:36px;height:36px;border-radius:50%;">
                                    <strong><?= sanitize($u['name']) ?></strong>
                                </div>
                            </td>
                            <td><?= sanitize($u['email']) ?></td>
                            <td><?= sanitize($u['phone'] ?: '-') ?></td>
                            <td><?= $u['booking_count'] ?></td>
                            <td><span class="status-badge status-<?= $u['status'] ?>"><?= ucfirst($u['status']) ?></span></td>
                            <td><?= formatDate($u['created_at']) ?></td>
                            <td>
                                <div class="table-actions">
                                    <?php if ($u['status'] === 'active'): ?>
                                        <a href="?action=banned&id=<?= $u['id'] ?>" class="action-btn delete" title="Ban User" data-confirm="Ban this user?"><i class="fas fa-ban"></i></a>
                                    <?php elseif ($u['status'] === 'banned'): ?>
                                        <a href="?action=active&id=<?= $u['id'] ?>" class="action-btn view" title="Activate"><i class="fas fa-check"></i></a>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (empty($users)): ?>
                        <tr><td colspan="8" style="text-align:center;color:var(--gray);padding:30px;">No users found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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