<?php
require_once '../includes/functions.php';
requireLogin();
requireAdmin();

$msg = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['toggle_status'])) {
    $user_id = $_POST['user_id'];
    $current_status = $_POST['current_status'];
    $new_status = $current_status === 'active' ? 'blocked' : 'active';
    $stmt = $pdo->prepare("UPDATE users SET status = ? WHERE id = ?");
    $stmt->execute([$new_status, $user_id]);
    $msg = "<div class='alert alert-success'>User status updated successfully.</div>";
}

$users = $pdo->query("SELECT * FROM users WHERE role = 'user' ORDER BY created_at DESC")->fetchAll();
include '../includes/header.php';
?>

<div class="row animate-fade-in">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="fw-bold m-0"><i class="fas fa-users text-primary me-2"></i> Manage <span class="text-primary">Users</span></h2>
                <p class="text-muted small">View, edit, or manage user accounts and their status.</p>
            </div>
            <a href="add_user.php" class="btn btn-premium rounded-pill">
                <i class="fas fa-user-plus me-2"></i> Add New User
            </a>
        </div>

        <?php echo $msg; ?>

        <div class="glass-card overflow-hidden">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">User Information</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Balance</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold">Status</th>
                            <th class="px-4 py-3 border-0 small text-uppercase text-muted fw-bold text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($users)): ?>
                            <tr>
                                <td colspan="4" class="text-center py-5 text-muted">No users found.</td>
                            </tr>
                        <?php endif; ?>
                        <?php foreach ($users as $u): ?>
                        <tr>
                            <td class="px-4 py-3">
                                <div class="d-flex align-items-center">
                                    <div class="bg-primary bg-opacity-10 p-2 rounded-3 me-3">
                                        <i class="fas fa-user text-primary"></i>
                                    </div>
                                    <div>
                                        <div class="fw-bold text-dark"><?php echo htmlspecialchars($u['name']); ?></div>
                                        <div class="small text-muted"><?php echo htmlspecialchars($u['username']); ?> | <?php echo htmlspecialchars($u['email']); ?></div>
                                        <div class="small text-muted"><?php echo htmlspecialchars($u['phone']); ?></div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-4 py-3">
                                <span class="fw-bold text-primary">₹<?php echo number_format($u['balance'], 2); ?></span>
                            </td>
                            <td class="px-4 py-3">
                                <span class="badge <?php echo $u['status'] === 'active' ? 'bg-success-subtle text-success border-success' : 'bg-danger-subtle text-danger border-danger'; ?> border px-3 py-2 rounded-pill">
                                    <?php echo strtoupper($u['status']); ?>
                                </span>
                            </td>
                            <td class="px-4 py-3 text-end">
                                <div class="d-flex justify-content-end gap-2">
                                    <a href="edit_user.php?id=<?php echo $u['id']; ?>" class="btn btn-sm btn-outline-primary rounded-pill px-3">
                                        <i class="fas fa-edit me-1"></i> Edit
                                    </a>
                                    
                                    <form method="POST" class="d-inline" onsubmit="return confirm('Change user status?');">
                                        <input type="hidden" name="user_id" value="<?php echo $u['id']; ?>">
                                        <input type="hidden" name="current_status" value="<?php echo $u['status']; ?>">
                                        <button type="submit" name="toggle_status" class="btn btn-sm <?php echo $u['status'] === 'active' ? 'btn-outline-danger' : 'btn-outline-success'; ?> rounded-pill px-3">
                                            <i class="fas <?php echo $u['status'] === 'active' ? 'fa-user-slash' : 'fa-user-check'; ?> me-1"></i>
                                            <?php echo $u['status'] === 'active' ? 'Block' : 'Unblock'; ?>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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