<?php
require 'auth_check.php';
require '../config/db.php';

// 1. DELETE USER
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $pdo->prepare("DELETE FROM users WHERE id = ?")->execute([$id]);
    header("Location: users.php?msg=deleted");
    exit();
}

// 2. FETCH USERS & STATS
$users = $pdo->query("
    SELECT u.*, 
           COUNT(o.id) as order_count, 
           COALESCE(SUM(o.total_amount), 0) as total_spent 
    FROM users u 
    LEFT JOIN orders o ON u.id = o.user_id AND o.status = 'completed'
    GROUP BY u.id 
    ORDER BY u.created_at DESC
")->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Manage Users</title>
    <link rel="stylesheet" href="admin_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .badge { padding: 3px 8px; border-radius: 4px; font-size: 11px; font-weight: bold; }
        .badge-admin { background: #8e44ad; color: white; }
        .badge-user { background: #95a5a6; color: white; }
        .vip { color: #f39c12; font-weight: bold; }
        .btn-view { background: #3498db; color: white; padding: 5px 10px; border-radius: 4px; text-decoration: none; font-size: 12px; }
    </style>
</head>
<body>

    <?php include 'sidebar.php'; ?>

    <div class="content">
        <h1>Registered Users (<?php echo count($users); ?>)</h1>

        <?php if(isset($_GET['msg'])): ?>
            <div style="padding:10px; background:#d4edda; color:#155724; border-radius:5px; margin-bottom:20px;">User deleted successfully.</div>
        <?php endif; ?>

        <table style="background: white;">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name / Email</th>
                    <th>Role</th>
                    <th>Joined</th>
                    <th>Orders</th>
                    <th>Total Spent</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($users as $u): ?>
                <tr>
                    <td>#<?php echo $u['id']; ?></td>
                    <td>
                        <strong><?php echo htmlspecialchars($u['name']); ?></strong><br>
                        <small style="color:#777;"><?php echo htmlspecialchars($u['email']); ?></small>
                    </td>
                    <td>
                        <?php if($u['role'] == 'admin'): ?>
                            <span class="badge badge-admin">ADMIN</span>
                        <?php else: ?>
                            <span class="badge badge-user">CUSTOMER</span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo date('d M Y', strtotime($u['created_at'])); ?></td>
                    <td><?php echo $u['order_count']; ?></td>
                    <td>
                        <?php if($u['total_spent'] > 0): ?>
                            <span class="vip">₹<?php echo number_format($u['total_spent']); ?></span>
                        <?php else: ?>
                            ₹0
                        <?php endif; ?>
                    </td>
                    <td>
                        <a href="user_history.php?id=<?php echo $u['id']; ?>" class="btn-view"><i class="fas fa-eye"></i> History</a>
                        <a href="login_as_user.php?id=<?php echo $u['id']; ?>" class="btn-view" style="background: #2c3e50; margin-left: 5px;" title="Login as User" target="_blank">
    <i class="fas fa-user-secret"></i>
</a>
                        
                        <a href="user_edit.php?id=<?php echo $u['id']; ?>" class="btn-view" style="background: #f39c12; margin-left: 5px;">
    <i class="fas fa-edit"></i> Edit
</a>
                        
                        <?php if($u['role'] != 'admin'): ?>
                            <a href="users.php?delete=<?php echo $u['id']; ?>" style="color: red; font-size: 14px; margin-left: 10px;" onclick="return confirm('Delete user?')">
                                <i class="fas fa-trash"></i>
                            </a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

</body>
</html>