<?php
$pageTitle = "Manage Users";
require_once 'sidebar.php';

if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    $action = $_GET['action'];
    if ($action === 'block') {
        mysqli_query($conn, "UPDATE users SET status='blocked' WHERE id=$id AND role='user'");
        redirect(BASE_URL.'/admin/users.php', 'User blocked.');
    } elseif ($action === 'unblock') {
        mysqli_query($conn, "UPDATE users SET status='active' WHERE id=$id AND role='user'");
        redirect(BASE_URL.'/admin/users.php', 'User unblocked.');
    } elseif ($action === 'delete') {
        mysqli_query($conn, "DELETE FROM users WHERE id=$id AND role='user'");
        redirect(BASE_URL.'/admin/users.php', 'User deleted.');
    }
}

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

<div class="page-header">
    <h1>Manage Users</h1>
    <p style="color:#888; font-size:14px;"><?php echo mysqli_num_rows($users); ?> registered users</p>
</div>

<?php showFlash(); ?>

<div class="table-wrapper">
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Bookings</th>
                <th>Status</th>
                <th>Joined</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (mysqli_num_rows($users) > 0): ?>
                <?php while ($u = mysqli_fetch_assoc($users)): ?>
                    <tr>
                        <td>#<?php echo $u['id']; ?></td>
                        <td><strong><?php echo htmlspecialchars($u['name']); ?></strong></td>
                        <td><?php echo htmlspecialchars($u['email']); ?></td>
                        <td><?php echo htmlspecialchars($u['phone']); ?></td>
                        <td style="text-align:center;"><strong style="color:var(--bronze);"><?php echo $u['booking_count']; ?></strong></td>
                        <td>
                            <?php $badge = $u['status']=='active'?'confirmed':'cancelled'; ?>
                            <span class="badge badge-<?php echo $badge; ?>"><?php echo ucfirst($u['status']); ?></span>
                        </td>
                        <td><?php echo date('d M Y', strtotime($u['created_at'])); ?></td>
                        <td>
                            <?php if ($u['status'] === 'active'): ?>
                                <a href="?action=block&id=<?php echo $u['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Block this user?')">Block</a>
                            <?php else: ?>
                                <a href="?action=unblock&id=<?php echo $u['id']; ?>" class="btn btn-sm btn-success">Unblock</a>
                            <?php endif; ?>
                            <a href="?action=delete&id=<?php echo $u['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this user?')">Delete</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="8" style="text-align:center; padding:40px; color:#888;">
                    <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">❦</div>
                    No users yet.
                </td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

</main></div></body></html>
