<?php
// /public_html/admin/users.php
session_start();
require '../config/db_connect.php';

// 1. Security Check
$user_id = $_SESSION['user_id'] ?? 0;
$check_admin = $conn->query("SELECT role FROM users WHERE id = '$user_id'")->fetch_assoc();
if (($check_admin['role'] ?? '') !== 'admin') { die("ACCESS DENIED"); }

// 2. Handle Delete Request
$msg = "";
if (isset($_GET['delete'])) {
    $delete_id = intval($_GET['delete']);
    if ($delete_id != $user_id) { // Prevent deleting yourself
        $conn->query("DELETE FROM users WHERE id = $delete_id");
        $conn->query("DELETE FROM qrcodes WHERE user_id = $delete_id"); // Delete their QRs too
        $msg = "User deleted successfully.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Manage Users - Admin</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
</head>
<body class="bg-light">

    <?php include 'includes/navbar.php'; ?>

    <div class="container mt-5">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>User Management</h2>
            <a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
        </div>

        <?php if(!empty($msg)) echo "<div class='alert alert-warning'>$msg</div>"; ?>

        <div class="card shadow-sm">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-striped mb-0 align-middle">
                        <thead class="table-dark">
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Role</th>
                                <th>Joined</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $users = $conn->query("SELECT * FROM users ORDER BY id DESC");
                            while($row = $users->fetch_assoc()):
                            ?>
                            <tr>
                                <td>#<?php echo $row['id']; ?></td>
                                <td><?php echo htmlspecialchars($row['name']); ?></td>
                                <td><?php echo htmlspecialchars($row['email']); ?></td>
                                <td>
                                    <?php if($row['role'] == 'admin'): ?>
                                        <span class="badge bg-danger">ADMIN</span>
                                    <?php elseif($row['role'] == 'reseller'): ?>
                                        <span class="badge bg-primary">AGENCY</span>
                                    <?php else: ?>
                                        <span class="badge bg-success">USER</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo date('M d, Y', strtotime($row['created_at'])); ?></td>
                                <td class="text-end">
                                    <?php if($row['id'] != $user_id): // Don't show actions on yourself ?>
                                        
                                        <a href="auth_user.php?id=<?php echo $row['id']; ?>" class="btn btn-sm btn-outline-dark" title="Log in as this user" target="_blank">
                                            <i class="bi bi-box-arrow-in-right"></i>
                                        </a>

                                        <a href="?delete=<?php echo $row['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this user AND all their QR codes?');" title="Delete User">
                                            <i class="bi bi-trash"></i>
                                        </a>

                                    <?php else: ?>
                                        <span class="text-muted small">Current User</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <?php include 'includes/footer.php'; ?>