<?php
require '../config/db.php';

// Settings থেকে ডাটা ফেচ করা
$settings = $pdo->query("SELECT * FROM settings WHERE id = 1")->fetch();
$site_name = $settings['site_name'] ?? 'LudoPro';
$currency = $settings['currency'] ?? '৳';
$logo = $settings['logo'] ?? 'logo.png';

// লগইন চেক
if (!isset($_SESSION['admin_logged_in'])) { 
    header("Location: index.php"); 
    exit; 
}

$msg = ""; 
$msgType = "";

// ১. ইউজার আপডেট লজিক
if (isset($_POST['update_user'])) {
    $uid = $_POST['user_id'];
    $bal = $_POST['balance'];
    $win = $_POST['win_balance'];
    $stat = $_POST['status'];
    
    try {
        if (!empty($_POST['new_pass'])) {
            $hash = password_hash($_POST['new_pass'], PASSWORD_DEFAULT);
            $sql = "UPDATE users SET balance=?, win_balance=?, status=?, password=? WHERE id=?";
            $params = [$bal, $win, $stat, $hash, $uid];
        } else {
            $sql = "UPDATE users SET balance=?, win_balance=?, status=? WHERE id=?";
            $params = [$bal, $win, $stat, $uid];
        }
        
        if($pdo->prepare($sql)->execute($params)) {
            // ইউজার নোটিফিকেশন (যদি ইমেইল/এসএমএস সিস্টেম থাকে)
            $msg = "User Updated Successfully!"; 
            $msgType = "success";
        } else {
            $msg = "Something went wrong!"; 
            $msgType = "error";
        }
    } catch(Exception $e) {
        $msg = "Database error occurred!"; 
        $msgType = "error";
    }
}

// ২. একাধিক ইউজার আপডেট (বাল্ক অ্যাকশন)
if (isset($_POST['bulk_action']) && isset($_POST['selected_ids'])) {
    $ids = $_POST['selected_ids'];
    $action = $_POST['bulk_action'];
    
    if(!empty($ids)) {
        $placeholders = implode(',', array_fill(0, count($ids), '?'));
        
        if($action == 'activate_selected') {
            $pdo->prepare("UPDATE users SET status='active' WHERE id IN ($placeholders)")->execute($ids);
            $msg = count($ids) . " users activated successfully!"; 
            $msgType = "success";
        } elseif($action == 'ban_selected') {
            $pdo->prepare("UPDATE users SET status='banned' WHERE id IN ($placeholders)")->execute($ids);
            $msg = count($ids) . " users banned successfully!"; 
            $msgType = "info";
        }
    }
}

// ৩. ডাটা ফেচিং (সর্বশেষ ১০০ ইউজার)
$users = $pdo->query("SELECT * FROM users ORDER BY id DESC LIMIT 100")->fetchAll();

// ৪. পরিসংখ্যান
$total_users = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
$active_users = $pdo->query("SELECT COUNT(*) FROM users WHERE status='active'")->fetchColumn();
$banned_users = $pdo->query("SELECT COUNT(*) FROM users WHERE status='banned'")->fetchColumn();
$total_balance = $pdo->query("SELECT SUM(balance + win_balance) FROM users")->fetchColumn();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Management | <?= htmlspecialchars($site_name) ?> Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    
    <style>
        * {
            font-family: 'Inter', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #0a0f1e 0%, #0b1120 100%);
            color: #e2e8f0;
        }
        
        /* গ্লাসমর্ফিজম ইফেক্ট */
        .glass-card {
            background: rgba(15, 23, 42, 0.6);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(56, 189, 248, 0.1);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .glass-card:hover {
            border-color: rgba(56, 189, 248, 0.3);
            transform: translateY(-2px);
            box-shadow: 0 20px 40px -15px rgba(56, 189, 248, 0.2);
        }
        
        /* নেভিগেশন হোভার ইফেক্ট */
        .nav-item {
            position: relative;
            overflow: hidden;
        }
        
        .nav-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 3px;
            background: linear-gradient(180deg, #3b82f6, #8b5cf6);
            transform: scaleY(0);
            transition: transform 0.2s ease;
        }
        
        .nav-item:hover::before,
        .nav-item.active::before {
            transform: scaleY(1);
        }
        
        .nav-item.active {
            background: linear-gradient(90deg, rgba(59, 130, 246, 0.1), transparent);
        }
        
        /* স্ট্যাটাস ব্যাজ */
        .status-badge {
            padding: 4px 10px;
            border-radius: 100px;
            font-size: 11px;
            font-weight: 600;
            letter-spacing: 0.3px;
            text-transform: uppercase;
            display: inline-flex;
            align-items: center;
            gap: 4px;
        }
        
        .status-badge.active {
            background: rgba(16, 185, 129, 0.1);
            color: #34d399;
            border: 1px solid rgba(16, 185, 129, 0.2);
        }
        
        .status-badge.banned {
            background: rgba(239, 68, 68, 0.1);
            color: #f87171;
            border: 1px solid rgba(239, 68, 68, 0.2);
        }
        
        .status-badge.pending {
            background: rgba(245, 158, 11, 0.1);
            color: #fbbf24;
            border: 1px solid rgba(245, 158, 11, 0.2);
        }
        
        /* গ্রেডিয়েন্ট টেক্সট */
        .gradient-text {
            background: linear-gradient(135deg, #60a5fa, #c084fc);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        /* কাস্টম স্ক্রলবার */
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        
        ::-webkit-scrollbar-track {
            background: #0f172a;
        }
        
        ::-webkit-scrollbar-thumb {
            background: #334155;
            border-radius: 100px;
        }
        
        ::-webkit-scrollbar-thumb:hover {
            background: #475569;
        }
        
        /* টেবিল স্ট্রাইপ */
        .table-row-hover:hover {
            background: rgba(59, 130, 246, 0.05);
        }
        
        /* অ্যাকশন বাটন */
        .action-btn {
            width: 36px;
            height: 36px;
            border-radius: 10px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            transition: all 0.2s ease;
        }
        
        /* ইউজার অ্যাভাটার */
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 12px;
            background: linear-gradient(135deg, #3b82f6, #8b5cf6);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 16px;
            color: white;
        }
    </style>
</head>
<body class="flex min-h-screen overflow-hidden">
    
    <!-- সাইডবার - settings থেকে ডাইনামিক -->
    <aside class="w-72 bg-slate-900/50 backdrop-blur-xl border-r border-slate-800/50 hidden lg:flex flex-col fixed h-screen overflow-y-auto">
        <!-- লোগো সেকশন -->
        <div class="p-6 border-b border-slate-800/50">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-gradient-to-br from-blue-500 to-purple-600 rounded-xl flex items-center justify-center shadow-lg shadow-blue-500/20">
                    <?php if($logo && file_exists("../assets/img/".$logo)): ?>
                        <img src="../assets/img/<?= $logo ?>" class="w-8 h-8 object-contain" alt="Logo">
                    <?php else: ?>
                        <i class="fas fa-crown text-white text-lg"></i>
                    <?php endif; ?>
                </div>
                <div>
                    <h1 class="text-2xl font-black tracking-tight">
                        <span class="text-white"><?= htmlspecialchars(substr($site_name, 0, 4)) ?></span>
                        <span class="gradient-text"><?= htmlspecialchars(substr($site_name, 4)) ?></span>
                    </h1>
                    <p class="text-[10px] text-slate-500 tracking-wider">ADMIN PANEL v2.0</p>
                </div>
            </div>
        </div>
        
        <!-- নেভিগেশন মেনু -->
        <nav class="flex-1 p-4 space-y-1">
            <div class="text-[10px] font-semibold text-slate-500 uppercase tracking-wider px-4 py-2">Main Menu</div>
            
            <a href="dashboard.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-home w-5"></i>
                <span class="text-sm font-medium">Dashboard</span>
            </a>
            
            <a href="tournaments.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-trophy w-5"></i>
                <span class="text-sm font-medium">Tournaments</span>
            </a>
            
            <a href="users.php" class="nav-item active flex items-center gap-3 px-4 py-3 text-white rounded-xl transition-all duration-200">
                <i class="fas fa-users w-5 text-blue-400"></i>
                <span class="text-sm font-medium">Users</span>
                <span class="ml-auto bg-blue-500/20 text-blue-400 text-[10px] px-2 py-0.5 rounded-full"><?= $total_users ?></span>
            </a>
            
            <div class="text-[10px] font-semibold text-slate-500 uppercase tracking-wider px-4 py-2 mt-4">Transactions</div>
            
            <a href="deposits.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-arrow-down w-5 text-emerald-400"></i>
                <span class="text-sm font-medium">Deposits</span>
            </a>
            
            <a href="withdraws.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-arrow-up w-5 text-rose-400"></i>
                <span class="text-sm font-medium">Withdrawals</span>
            </a>
            
            <div class="text-[10px] font-semibold text-slate-500 uppercase tracking-wider px-4 py-2 mt-4">Settings</div>
            
            <a href="settings.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-cog w-5"></i>
                <span class="text-sm font-medium">Settings</span>
            </a>
        </nav>
        
        <!-- ইউজার প্রোফাইল -->
        <div class="p-4 border-t border-slate-800/50 mt-auto">
            <div class="flex items-center gap-3 p-3 bg-slate-800/30 rounded-2xl">
                <div class="w-10 h-10 bg-gradient-to-br from-blue-600 to-purple-600 rounded-xl flex items-center justify-center">
                    <span class="text-white font-bold text-lg">A</span>
                </div>
                <div class="flex-1">
                    <p class="text-sm font-semibold text-white">Admin</p>
                    <p class="text-[10px] text-slate-500">Super Admin</p>
                </div>
                <a href="index.php?logout=1" class="w-8 h-8 bg-rose-500/10 text-rose-400 rounded-lg flex items-center justify-center hover:bg-rose-500/20 transition-colors" title="Logout">
                    <i class="fas fa-sign-out-alt text-sm"></i>
                </a>
            </div>
        </div>
    </aside>

    <!-- মেইন কন্টেন্ট এরিয়া -->
    <main class="flex-1 lg:ml-72 p-6 lg:p-8 overflow-y-auto h-screen">
        
        <!-- টপ বার -->
        <header class="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-8">
            <div>
                <h1 class="text-3xl font-black tracking-tight">
                    <span class="text-white">User</span>
                    <span class="gradient-text">Management</span>
                </h1>
                <p class="text-slate-500 text-sm mt-1">Manage users, balances, and account status.</p>
            </div>
            
            <div class="flex items-center gap-3">
                <div class="glass-card px-4 py-2.5 rounded-xl flex items-center gap-3">
                    <i class="far fa-calendar-alt text-blue-400"></i>
                    <span class="text-sm font-medium text-white"><?= date('l, d M Y') ?></span>
                </div>
                <div class="glass-card w-10 h-10 rounded-xl flex items-center justify-center">
                    <i class="far fa-bell text-slate-400"></i>
                </div>
            </div>
        </header>

        <!-- এলার্ট মেসেজ -->
        <?php if($msg): ?>
        <script>
            Swal.fire({
                icon: '<?= $msgType ?>',
                title: '<?= $msg ?>',
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                background: '#1e293b',
                color: '#fff',
                iconColor: '<?= $msgType == "success" ? "#34d399" : ($msgType == "error" ? "#f87171" : "#60a5fa") ?>'
            });
        </script>
        <?php endif; ?>

        <!-- স্ট্যাটিস্টিক্যাল কার্ডস -->
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Total Users</p>
                        <h2 class="text-3xl font-black text-white mt-2"><?= number_format($total_users) ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-blue-500/10 rounded-xl flex items-center justify-center text-blue-400 text-xl">
                        <i class="fas fa-users"></i>
                    </div>
                </div>
            </div>
            
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Active Users</p>
                        <h2 class="text-3xl font-black text-white mt-2 text-emerald-400"><?= number_format($active_users) ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-emerald-500/10 rounded-xl flex items-center justify-center text-emerald-400 text-xl">
                        <i class="fas fa-check-circle"></i>
                    </div>
                </div>
            </div>
            
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Banned Users</p>
                        <h2 class="text-3xl font-black text-white mt-2 text-rose-400"><?= number_format($banned_users) ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-rose-500/10 rounded-xl flex items-center justify-center text-rose-400 text-xl">
                        <i class="fas fa-ban"></i>
                    </div>
                </div>
            </div>
            
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Total Balance</p>
                        <h2 class="text-3xl font-black text-white mt-2"><?= htmlspecialchars($currency) ?><?= number_format($total_balance ?? 0, 0) ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-purple-500/10 rounded-xl flex items-center justify-center text-purple-400 text-xl">
                        <i class="fas fa-coins"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- বাল্ক অ্যাকশন ফর্ম -->
        <?php if(!empty($users)): ?>
        <form method="POST" id="bulkForm" class="mb-6">
            <div class="glass-card p-4 rounded-xl flex flex-wrap gap-4 items-center justify-between">
                <div class="flex items-center gap-3">
                    <input type="checkbox" id="selectAll" class="w-4 h-4 rounded border-slate-600 bg-slate-800 text-blue-600 focus:ring-blue-500">
                    <label for="selectAll" class="text-sm text-slate-300">Select All</label>
                    <span class="text-xs text-slate-500" id="selectedCount">0 selected</span>
                </div>
                
                <div class="flex gap-3">
                    <select name="bulk_action" class="bg-slate-800 border border-slate-700 rounded-xl px-4 py-2 text-sm text-white" required>
                        <option value="">Bulk Action</option>
                        <option value="activate_selected">Activate Selected</option>
                        <option value="ban_selected">Ban Selected</option>
                    </select>
                    
                    <button type="submit" onclick="return confirmBulkAction()" class="bg-blue-600 hover:bg-blue-500 text-white px-6 py-2 rounded-xl text-sm font-bold transition">
                        Apply to Selected
                    </button>
                </div>
            </div>
        </form>
        <?php endif; ?>

        <!-- ইউজার টেবিল -->
        <div class="glass-card rounded-2xl overflow-hidden border border-slate-700">
            <div class="p-6 border-b border-slate-700/50 bg-slate-800/30">
                <div class="flex flex-col md:flex-row md:items-center justify-between gap-4">
                    <div class="flex items-center gap-2">
                        <i class="fas fa-list text-blue-400"></i>
                        <h2 class="font-bold text-white">User List (Last 100)</h2>
                    </div>
                    
                    <div class="relative w-full md:w-80">
                        <i class="fas fa-search absolute left-4 top-1/2 -translate-y-1/2 text-slate-500 text-sm"></i>
                        <input type="text" id="userSearch" onkeyup="filterUsers()" placeholder="Search by name, phone or ID..." 
                               class="w-full bg-slate-900/50 border border-slate-700 rounded-xl pl-10 pr-4 py-2.5 text-sm text-white placeholder-slate-500 focus:border-blue-500 outline-none transition">
                    </div>
                </div>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full text-left">
                    <thead>
                        <tr class="bg-slate-800/50 text-[11px] text-slate-400 uppercase">
                            <th class="px-6 py-4 w-10">
                                <span class="sr-only">Select</span>
                            </th>
                            <th class="px-6 py-4 font-semibold">User Information</th>
                            <th class="px-6 py-4 font-semibold">Wallets</th>
                            <th class="px-6 py-4 font-semibold">Status</th>
                            <th class="px-6 py-4 font-semibold">Joined</th>
                            <th class="px-6 py-4 font-semibold text-right">Actions</th>
                        </tr>
                    </thead>
                    <tbody id="userTableBody" class="divide-y divide-slate-800/50">
                        <?php foreach($users as $u): ?>
                        <tr class="user-row table-row-hover transition-colors">
                            <td class="px-6 py-4">
                                <input type="checkbox" name="selected_ids[]" value="<?= $u['id'] ?>" form="bulkForm" class="row-checkbox w-4 h-4 rounded border-slate-600 bg-slate-800 text-blue-600 focus:ring-blue-500">
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center gap-3">
                                    <div class="user-avatar">
                                        <?= strtoupper(substr($u['username'], 0, 1)) ?>
                                    </div>
                                    <div>
                                        <div class="font-bold text-white username"><?= htmlspecialchars($u['username']) ?></div>
                                        <div class="text-xs text-slate-500 flex items-center gap-2 mt-0.5">
                                            <span class="flex items-center gap-1">
                                                <i class="fas fa-phone-alt text-[8px]"></i>
                                                <span class="phone"><?= $u['phone'] ?? 'No Phone' ?></span>
                                            </span>
                                            <span class="text-slate-600">•</span>
                                            <span class="text-[10px] font-mono text-slate-600">ID: <?= $u['id'] ?></span>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <div class="space-y-1">
                                    <div class="text-sm font-bold text-emerald-400">Main: <?= htmlspecialchars($currency) ?><?= number_format($u['balance'], 2) ?></div>
                                    <div class="text-xs text-blue-400">Winning: <?= htmlspecialchars($currency) ?><?= number_format($u['win_balance'], 2) ?></div>
                                    <div class="text-[10px] text-slate-500">Total: <?= htmlspecialchars($currency) ?><?= number_format($u['balance'] + $u['win_balance'], 2) ?></div>
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <span class="status-badge <?= $u['status'] ?>">
                                    <i class="fas fa-circle text-[6px]"></i>
                                    <?= ucfirst($u['status']) ?>
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                <div class="text-sm text-white"><?= date('d M Y', strtotime($u['created_at'] ?? 'now')) ?></div>
                                <div class="text-xs text-slate-500"><?= date('h:i A', strtotime($u['created_at'] ?? 'now')) ?></div>
                            </td>
                            <td class="px-6 py-4 text-right">
                                <button onclick='openEditModal(<?= json_encode($u) ?>)' 
                                        class="action-btn bg-blue-500/10 hover:bg-blue-500 text-blue-400 hover:text-white transition-all group"
                                        title="Edit User">
                                    <i class="fas fa-edit"></i>
                                </button>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        
                        <?php if(empty($users)): ?>
                        <tr>
                            <td colspan="6" class="px-6 py-16 text-center">
                                <div class="flex flex-col items-center gap-3">
                                    <div class="w-20 h-20 bg-slate-800/50 rounded-full flex items-center justify-center text-4xl">
                                        👥
                                    </div>
                                    <h3 class="text-lg font-semibold text-white">No Users Found</h3>
                                    <p class="text-sm text-slate-500">There are no users in the system yet.</p>
                                </div>
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            
            <?php if(!empty($users)): ?>
            <div class="p-4 border-t border-slate-700/50 bg-slate-800/30 text-xs text-slate-500 flex justify-between items-center">
                <span><i class="fas fa-info-circle mr-1"></i> Showing last 100 users. Use search to find specific users.</span>
                <span class="text-blue-400">Total: <?= $total_users ?> users</span>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- ফুটার -->
        <div class="mt-12 text-center text-xs text-slate-600 border-t border-slate-800/50 pt-6">
            <p>© 2024 <?= htmlspecialchars($site_name) ?> Admin Panel. All rights reserved. | User Management System</p>
        </div>
    </main>

    <!-- এডিট ইউজার মোডাল -->
    <div id="editModal" class="hidden fixed inset-0 bg-black/80 backdrop-blur-sm flex items-center justify-center z-50 p-4">
        <div class="glass-card rounded-2xl w-full max-w-md border border-slate-700">
            <div class="p-6 border-b border-slate-700 flex justify-between items-center">
                <h2 class="text-xl font-bold text-white flex items-center gap-2">
                    <i class="fas fa-user-edit text-blue-500"></i>
                    Edit User
                </h2>
                <button onclick="document.getElementById('editModal').classList.add('hidden')" class="w-8 h-8 bg-slate-700 rounded-lg hover:bg-slate-600 transition flex items-center justify-center">
                    <i class="fas fa-times text-slate-400"></i>
                </button>
            </div>
            
            <div class="p-6">
                <div class="mb-4 p-3 bg-blue-500/10 rounded-xl border border-blue-500/20">
                    <p class="text-xs text-slate-400">Editing user:</p>
                    <p id="targetUser" class="text-lg font-bold text-white"></p>
                </div>
                
                <form method="POST" class="space-y-4">
                    <input type="hidden" name="user_id" id="editUserId">
                    
                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label class="text-[10px] font-bold text-slate-500 uppercase ml-1">Main Balance (<?= htmlspecialchars($currency) ?>)</label>
                            <input type="number" step="0.01" name="balance" id="editBalance" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 text-white outline-none focus:border-blue-500 mt-1">
                        </div>
                        <div>
                            <label class="text-[10px] font-bold text-slate-500 uppercase ml-1">Win Balance (<?= htmlspecialchars($currency) ?>)</label>
                            <input type="number" step="0.01" name="win_balance" id="editWin" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 text-white outline-none focus:border-blue-500 mt-1">
                        </div>
                    </div>

                    <div>
                        <label class="text-[10px] font-bold text-slate-500 uppercase ml-1">Account Status</label>
                        <select name="status" id="editStatus" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 text-white outline-none focus:border-blue-500 mt-1">
                            <option value="active">Active</option>
                            <option value="banned">Banned</option>
                        </select>
                    </div>

                    <div>
                        <label class="text-[10px] font-bold text-slate-500 uppercase ml-1">Reset Password</label>
                        <input type="text" name="new_pass" placeholder="Leave blank to keep current password" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 text-white outline-none focus:border-blue-500 mt-1">
                        <p class="text-[9px] text-slate-600 mt-1">Min. 6 characters</p>
                    </div>

                    <div class="flex gap-3 pt-4">
                        <button type="button" onclick="document.getElementById('editModal').classList.add('hidden')" class="flex-1 py-3 bg-slate-700 hover:bg-slate-600 rounded-xl font-bold text-white transition">
                            Cancel
                        </button>
                        <button name="update_user" class="flex-1 py-3 bg-gradient-to-r from-emerald-600 to-teal-600 hover:from-emerald-500 hover:to-teal-500 rounded-xl font-bold text-white shadow-lg shadow-emerald-600/20 transition">
                            Update User
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- মোবাইল মেনু টগল -->
    <button id="mobileMenuToggle" class="lg:hidden fixed bottom-6 right-6 w-14 h-14 bg-gradient-to-br from-blue-600 to-purple-600 rounded-2xl shadow-xl flex items-center justify-center text-white text-xl z-50">
        <i class="fas fa-bars"></i>
    </button>

    <script>
        // মোবাইল মেনু টগল
        const mobileToggle = document.getElementById('mobileMenuToggle');
        const sidebar = document.querySelector('aside');
        
        if(mobileToggle && sidebar) {
            mobileToggle.addEventListener('click', function() {
                sidebar.classList.toggle('hidden');
                sidebar.classList.toggle('flex');
                sidebar.classList.toggle('fixed');
                sidebar.classList.toggle('inset-0');
                sidebar.classList.toggle('z-40');
                sidebar.classList.toggle('w-full');
                sidebar.classList.toggle('lg:w-72');
                sidebar.classList.toggle('lg:flex');
            });
        }
        
        // ইউজার এডিট মোডাল ওপেন
        function openEditModal(u) {
            document.getElementById('editModal').classList.remove('hidden');
            document.getElementById('editUserId').value = u.id;
            document.getElementById('targetUser').innerText = "@" + u.username + " (ID: " + u.id + ")";
            document.getElementById('editBalance').value = u.balance;
            document.getElementById('editWin').value = u.win_balance;
            document.getElementById('editStatus').value = u.status;
        }

        // রিয়েলটাইম সার্চ ফিল্টার
        function filterUsers() {
            let input = document.getElementById('userSearch').value.toLowerCase();
            let rows = document.getElementsByClassName('user-row');
            let visibleCount = 0;
            
            for (let row of rows) {
                let name = row.querySelector('.username').innerText.toLowerCase();
                let phone = row.querySelector('.phone').innerText.toLowerCase();
                let id = row.querySelector('.text-slate-600')?.innerText.toLowerCase() || '';
                
                if (name.includes(input) || phone.includes(input) || id.includes(input)) {
                    row.style.display = "";
                    visibleCount++;
                } else {
                    row.style.display = "none";
                }
            }
        }
        
        // সিলেক্ট অল ফাংশন
        const selectAllCheckbox = document.getElementById('selectAll');
        if(selectAllCheckbox) {
            selectAllCheckbox.addEventListener('change', function() {
                const checkboxes = document.querySelectorAll('.row-checkbox');
                checkboxes.forEach(cb => cb.checked = this.checked);
                updateSelectedCount();
            });
        }
        
        // সিলেক্টেড কাউন্ট আপডেট
        document.querySelectorAll('.row-checkbox').forEach(cb => {
            cb.addEventListener('change', updateSelectedCount);
        });
        
        function updateSelectedCount() {
            const selected = document.querySelectorAll('.row-checkbox:checked').length;
            document.getElementById('selectedCount').innerText = selected + ' selected';
        }
        
        // বাল্ক অ্যাকশন কনফার্মেশন
        function confirmBulkAction() {
            const selected = document.querySelectorAll('.row-checkbox:checked').length;
            if(selected === 0) {
                Swal.fire({
                    icon: 'warning',
                    title: 'No users selected',
                    text: 'Please select at least one user.',
                    background: '#1e293b',
                    color: '#fff'
                });
                return false;
            }
            
            const action = document.querySelector('select[name="bulk_action"]').value;
            if(!action) {
                Swal.fire({
                    icon: 'warning',
                    title: 'No action selected',
                    text: 'Please select an action to perform.',
                    background: '#1e293b',
                    color: '#fff'
                });
                return false;
            }
            
            return confirm('Are you sure you want to ' + action.replace('_', ' ') + ' ' + selected + ' user(s)?');
        }
        
        // মোডাল বন্ধ করতে Escape কী প্রেস
        document.addEventListener('keydown', function(e) {
            if(e.key === 'Escape') {
                document.getElementById('editModal').classList.add('hidden');
            }
        });
        
        // মোডালের বাইরে ক্লিক করলে বন্ধ
        window.onclick = function(event) {
            const modal = document.getElementById('editModal');
            if(event.target == modal) {
                modal.classList.add('hidden');
            }
        }
    </script>
</body>
</html>