<?php
require_once 'includes/header.php';

$pdo = db();

if (isset($_POST['delete_user'])) {
    $id = $_POST['user_id'];
    if ($id != $_SESSION['user_id']) {
        $stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
        $stmt->execute([$id]);
        echo "<div style='padding: 1rem; background: #f44336; color: white;'>User Deleted</div>";
    }
}

if (isset($_POST['update_role'])) {
    $id = $_POST['user_id'];
    $role = $_POST['role'];
    if ($id != $_SESSION['user_id']) { // Prevent changing own role to something lower accidentally
        $stmt = $pdo->prepare("UPDATE users SET role = ? WHERE id = ?");
        $stmt->execute([$role, $id]);
        echo "<div style='padding: 1rem; background: #4caf50; color: white;'>User Role Updated</div>";
    }
}

$stmt = $pdo->query("SELECT * FROM users ORDER BY created_at DESC");
$users = $stmt->fetchAll();
?>

<div class="page-header">
    <h1>User Management</h1>
</div>

<div style="overflow-x: auto;">
    <table class="data-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Username</th>
                <th>Role</th>
                <th>Joined</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($users as $user): ?>
            <tr>
                <td>#<?php echo $user['id']; ?></td>
                <td>
                    <?php echo htmlspecialchars($user['username']); ?><br>
                    <small style="color:var(--text-secondary);"><?php echo htmlspecialchars($user['email'] ?? ''); ?></small>
                </td>
                <td>
                    <?php if ($user['id'] == $_SESSION['user_id']): ?>
                        <span class="badge badge-admin"><?php echo htmlspecialchars($user['role']); ?></span>
                    <?php else: ?>
                        <form method="POST" style="display:inline;">
                            <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                            <select name="role" onchange="this.form.submit()" style="padding:0.2rem; border-radius:4px; background:var(--bg-color); color:var(--text-primary); border:1px solid var(--border-color);">
                                <?php
                                $roles = ['Guest','Customer','Dealer','Employee','Manager','Admin','Super Admin'];
                                foreach ($roles as $r) {
                                    $selected = ($user['role'] === $r) ? 'selected' : '';
                                    echo "<option value='$r' $selected>$r</option>";
                                }
                                ?>
                            </select>
                            <input type="hidden" name="update_role" value="1">
                        </form>
                    <?php endif; ?>
                </td>
                <td><?php echo date('M j, Y', strtotime($user['created_at'])); ?></td>
                <td>
                    <?php if ($user['id'] != $_SESSION['user_id']): ?>
                    <form method="POST" onsubmit="return confirm('Are you sure?');" style="display: inline;">
                        <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                        <button type="submit" name="delete_user" class="btn-sm btn-danger">Delete</button>
                    </form>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

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