<?php
// admin/users.php - Complete User Management System
session_start();
require_once '../includes/config.php';
require_once '../includes/sms_config.php';

if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin') {
    header('Location: ../auth.php');
    exit;
}

$message = '';
$msg_type = 'info';
$tab = $_GET['tab'] ?? 'all'; // all, students, instructors, admins, suspended

// ==================== HANDLE ACTIONS ====================

// Add User
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_user'])) {
    $full_name = trim($_POST['full_name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone'] ?? '');
    $password = $_POST['password'] ?? '123456';
    $role = $_POST['role'] ?? 'student';
    $bio = trim($_POST['bio'] ?? '');
    
    if(empty($full_name) || empty($email)) {
        $message = 'নাম এবং ইমেইল প্রয়োজন';
        $msg_type = 'danger';
    } else {
        // Check if email exists
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->execute([$email]);
        if($stmt->fetch()) {
            $message = 'এই ইমেইল ইতিমধ্যে ব্যবহৃত হয়েছে';
            $msg_type = 'danger';
        } else {
            $hash = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("INSERT INTO users (full_name, email, phone, password, role, bio) VALUES (?, ?, ?, ?, ?, ?)");
            $stmt->execute([$full_name, $email, $phone, $hash, $role, $bio]);
            
            // Send welcome SMS
            if(!empty($phone) && isset($sms)) {
                $sms->sendWelcomeSMS($phone, $full_name);
            }
            
            $message = 'ইউজার সফলভাবে তৈরি হয়েছে!';
            $msg_type = 'success';
        }
    }
}

// Edit User
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit_user'])) {
    $user_id = $_POST['user_id'];
    $full_name = trim($_POST['full_name']);
    $phone = trim($_POST['phone'] ?? '');
    $role = $_POST['role'] ?? 'student';
    $bio = trim($_POST['bio'] ?? '');
    $status = $_POST['status'] ?? 'active';
    
    $stmt = $pdo->prepare("UPDATE users SET full_name = ?, phone = ?, role = ?, bio = ?, status = ? WHERE id = ?");
    $stmt->execute([$full_name, $phone, $role, $bio, $status, $user_id]);
    
    $message = 'ইউজার আপডেট হয়েছে!';
    $msg_type = 'success';
}

// Change Password
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['change_user_password'])) {
    $user_id = $_POST['user_id'];
    $new_password = $_POST['new_password'];
    
    if(strlen($new_password) >= 6) {
        $hash = password_hash($new_password, PASSWORD_DEFAULT);
        $stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
        $stmt->execute([$hash, $user_id]);
        $message = 'পাসওয়ার্ড পরিবর্তন হয়েছে!';
        $msg_type = 'success';
    }
}

// Suspend/Activate
if(isset($_GET['action']) && isset($_GET['id'])) {
    $user_id = $_GET['id'];
    
    if($_GET['action'] == 'suspend') {
        $pdo->prepare("UPDATE users SET status = 'suspended' WHERE id = ?")->execute([$user_id]);
        $message = 'ইউজার সাসপেন্ড করা হয়েছে';
    } elseif($_GET['action'] == 'activate') {
        $pdo->prepare("UPDATE users SET status = 'active' WHERE id = ?")->execute([$user_id]);
        $message = 'ইউজার অ্যাক্টিভেট করা হয়েছে';
    } elseif($_GET['action'] == 'delete') {
        // Get user data for SMS
        $stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
        $stmt->execute([$user_id]);
        $user = $stmt->fetch();
        
        if($user) {
            // Delete related data
            $pdo->prepare("DELETE FROM enrollments WHERE user_id = ?")->execute([$user_id]);
            $pdo->prepare("DELETE FROM lesson_progress WHERE user_id = ?")->execute([$user_id]);
            $pdo->prepare("DELETE FROM quiz_attempts WHERE user_id = ?")->execute([$user_id]);
            $pdo->prepare("DELETE FROM quiz_results WHERE user_id = ?")->execute([$user_id]);
            $pdo->prepare("DELETE FROM reviews WHERE user_id = ?")->execute([$user_id]);
            $pdo->prepare("DELETE FROM sms_logs WHERE user_id = ?")->execute([$user_id]);
            
            // Delete user
            $pdo->prepare("DELETE FROM users WHERE id = ?")->execute([$user_id]);
            
            // Send SMS
            if(!empty($user['phone']) && isset($sms)) {
                $sms->sendSMS($user['phone'], "আপনার অ্যাকাউন্ট ডিলিট করা হয়েছে। - বাংলা LMS");
            }
            
            $message = 'ইউজার ডিলিট করা হয়েছে';
            $msg_type = 'warning';
        }
    }
    
    header("Location: users.php?tab=$tab&msg=" . urlencode($message) . "&type=" . $msg_type);
    exit;
}

// Bulk Actions
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['bulk_action']) && isset($_POST['selected_users'])) {
    $selected = $_POST['selected_users'];
    $action = $_POST['bulk_action'];
    $placeholders = implode(',', array_fill(0, count($selected), '?'));
    
    if($action == 'suspend') {
        $pdo->prepare("UPDATE users SET status = 'suspended' WHERE id IN ($placeholders)")->execute($selected);
        $message = count($selected) . ' জন ইউজার সাসপেন্ড হয়েছে';
    } elseif($action == 'activate') {
        $pdo->prepare("UPDATE users SET status = 'active' WHERE id IN ($placeholders)")->execute($selected);
        $message = count($selected) . ' জন ইউজার অ্যাক্টিভেট হয়েছে';
    } elseif($action == 'delete') {
        foreach($selected as $uid) {
            $pdo->prepare("DELETE FROM enrollments WHERE user_id = ?")->execute([$uid]);
            $pdo->prepare("DELETE FROM lesson_progress WHERE user_id = ?")->execute([$uid]);
            $pdo->prepare("DELETE FROM quiz_attempts WHERE user_id = ?")->execute([$uid]);
            $pdo->prepare("DELETE FROM users WHERE id = ?")->execute([$uid]);
        }
        $message = count($selected) . ' জন ইউজার ডিলিট হয়েছে';
    }
    
    header("Location: users.php?tab=$tab&msg=" . urlencode($message) . "&type=success");
    exit;
}

// ==================== GET USERS ====================
$msg = $_GET['msg'] ?? '';
$msg_type = $_GET['type'] ?? 'info';
$search = $_GET['search'] ?? '';
$role_filter = $_GET['filter'] ?? '';

$query = "SELECT u.*, 
          (SELECT COUNT(*) FROM enrollments WHERE user_id = u.id AND payment_status = 'completed') as enroll_count,
          (SELECT COALESCE(SUM(amount_paid),0) FROM enrollments WHERE user_id = u.id AND payment_status = 'completed') as total_spent
          FROM users u WHERE 1=1";
$params = [];

if($tab == 'students' || $role_filter == 'student') {
    $query .= " AND u.role = 'student'";
} elseif($tab == 'instructors' || $role_filter == 'instructor') {
    $query .= " AND u.role = 'instructor'";
} elseif($tab == 'admins' || $role_filter == 'admin') {
    $query .= " AND u.role = 'admin'";
} elseif($tab == 'suspended') {
    $query .= " AND u.status = 'suspended'";
}

if($search) {
    $query .= " AND (u.full_name LIKE ? OR u.email LIKE ? OR u.phone LIKE ?)";
    $params = array_merge($params, ["%$search%", "%$search%", "%$search%"]);
}

$query .= " ORDER BY u.created_at DESC LIMIT 100";

$stmt = $pdo->prepare($query);
$stmt->execute($params);
$users = $stmt->fetchAll();

// Stats
$total_users = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
$total_students = $pdo->query("SELECT COUNT(*) FROM users WHERE role='student'")->fetchColumn();
$total_instructors = $pdo->query("SELECT COUNT(*) FROM users WHERE role='instructor'")->fetchColumn();
$total_admins = $pdo->query("SELECT COUNT(*) FROM users WHERE role='admin'")->fetchColumn();
$total_suspended = $pdo->query("SELECT COUNT(*) FROM users WHERE status='suspended'")->fetchColumn();

// Get user for edit modal
$edit_user = null;
if(isset($_GET['edit'])) {
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->execute([$_GET['edit']]);
    $edit_user = $stmt->fetch();
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ইউজার ম্যানেজমেন্ট - অ্যাডমিন প্যানেল</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #6C5CE7; --gradient: linear-gradient(135deg, #667eea, #764ba2); --shadow: 0 3px 15px rgba(0,0,0,0.08); }
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { background: #f0f2f8; font-family: 'Segoe UI','Hind Siliguri',Arial,sans-serif; }
        
        .navbar { background: white; box-shadow: 0 2px 10px rgba(0,0,0,0.06); padding: 12px 0; position: sticky; top: 0; z-index: 100; }
        .navbar-brand { font-weight: 700; color: var(--primary) !important; text-decoration: none; }
        
        .main-card { background: white; border-radius: 15px; padding: 25px; box-shadow: var(--shadow); margin-bottom: 20px; }
        
        .stat-mini { background: white; border-radius: 12px; padding: 15px; text-align: center; transition: 0.3s; cursor: pointer; border: 2px solid transparent; }
        .stat-mini:hover { border-color: var(--primary); transform: translateY(-3px); }
        .stat-mini.active { border-color: var(--primary); background: #f8f9ff; }
        .stat-mini h4 { font-weight: 700; margin: 0; }
        .stat-mini small { color: #888; }
        
        .tab-btn { padding: 8px 20px; border-radius: 50px; border: 2px solid #e0e0e0; background: white; font-weight: 600; font-size: 0.85rem; cursor: pointer; transition: 0.3s; color: #666; text-decoration: none; display: inline-block; }
        .tab-btn.active, .tab-btn:hover { background: var(--gradient); color: white; border-color: transparent; }
        
        .user-avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: 700; font-size: 1rem; }
        
        .form-control, .form-select { border-radius: 10px; padding: 10px 15px; border: 2px solid #e0e0e0; }
        .form-control:focus, .form-select:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 3px rgba(108,92,231,0.08); }
        
        .btn { border-radius: 50px; padding: 8px 20px; font-weight: 600; transition: 0.3s; }
        .btn-primary { background: var(--gradient); border: none; color: white; }
        .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(108,92,231,0.3); }
        
        .badge-custom { padding: 4px 10px; border-radius: 20px; font-size: 0.75rem; font-weight: 600; }
        
        .modal-header { background: var(--gradient); color: white; border-radius: 15px 15px 0 0; }
        .modal-content { border-radius: 15px; border: none; }
        
        .table { font-size: 0.9rem; }
        .table th { background: #f8f9ff; font-weight: 600; }
        
        @media (max-width: 768px) {
            .tab-btn { padding: 6px 12px; font-size: 0.75rem; margin-bottom: 5px; }
        }
    </style>
</head>
<body>

<nav class="navbar">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <a href="dashboard.php" class="navbar-brand"><i class="fas fa-graduation-cap me-2"></i>অ্যাডমিন প্যানেল</a>
                <span class="badge bg-primary ms-2">ইউজার ম্যানেজমেন্ট</span>
            </div>
            <div class="d-flex gap-2">
                <a href="../dashboard.php" class="btn btn-outline-secondary btn-sm rounded-pill">ড্যাশবোর্ড</a>
                <a href="../logout.php" class="btn btn-sm rounded-pill" style="background:#ff4757;color:white;">লগআউট</a>
            </div>
        </div>
    </div>
</nav>

<div class="container py-4">
    
    <!-- Message -->
    <?php if($msg): ?>
        <div class="alert alert-<?= $msg_type ?> rounded-3"><?= htmlspecialchars($msg) ?></div>
    <?php endif; ?>
    
    <!-- Stats -->
    <div class="row g-3 mb-3">
        <div class="col-4 col-md-2">
            <a href="?tab=all" class="text-decoration-none">
                <div class="stat-mini <?= $tab=='all'?'active':'' ?>"><h4><?= $total_users ?></h4><small>সব ইউজার</small></div>
            </a>
        </div>
        <div class="col-4 col-md-2">
            <a href="?tab=students" class="text-decoration-none">
                <div class="stat-mini <?= $tab=='students'?'active':'' ?>"><h4 class="text-primary"><?= $total_students ?></h4><small>শিক্ষার্থী</small></div>
            </a>
        </div>
        <div class="col-4 col-md-2">
            <a href="?tab=instructors" class="text-decoration-none">
                <div class="stat-mini <?= $tab=='instructors'?'active':'' ?>"><h4 class="text-success"><?= $total_instructors ?></h4><small>ইন্সট্রাক্টর</small></div>
            </a>
        </div>
        <div class="col-4 col-md-2">
            <a href="?tab=admins" class="text-decoration-none">
                <div class="stat-mini <?= $tab=='admins'?'active':'' ?>"><h4 class="text-warning"><?= $total_admins ?></h4><small>অ্যাডমিন</small></div>
            </a>
        </div>
        <div class="col-4 col-md-2">
            <a href="?tab=suspended" class="text-decoration-none">
                <div class="stat-mini <?= $tab=='suspended'?'active':'' ?>"><h4 class="text-danger"><?= $total_suspended ?></h4><small>সাসপেন্ডেড</small></div>
            </a>
        </div>
        <div class="col-4 col-md-2 d-flex align-items-center">
            <button class="btn btn-primary btn-sm w-100" data-bs-toggle="modal" data-bs-target="#addUserModal">
                <i class="fas fa-plus me-1"></i> নতুন ইউজার
            </button>
        </div>
    </div>
    
    <!-- Main -->
    <div class="main-card">
        <!-- Search & Bulk Actions -->
        <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
            <form method="GET" class="d-flex gap-2">
                <input type="hidden" name="tab" value="<?= $tab ?>">
                <input type="text" name="search" class="form-control form-control-sm" placeholder="🔍 নাম, ইমেইল, ফোন..." value="<?= htmlspecialchars($search) ?>" style="width:250px;">
                <button type="submit" class="btn btn-outline-primary btn-sm">খুঁজুন</button>
            </form>
            
            <form method="POST" id="bulkForm" class="d-flex gap-2 align-items-center">
                <select name="bulk_action" class="form-select form-select-sm" style="width:150px;">
                    <option value="">বাল্ক অ্যাকশন</option>
                    <option value="suspend">⏸️ সাসপেন্ড</option>
                    <option value="activate">✅ অ্যাক্টিভেট</option>
                    <option value="delete">🗑️ ডিলিট</option>
                </select>
                <button type="submit" class="btn btn-outline-danger btn-sm">প্রয়োগ</button>
                <small class="text-muted" id="selectedCount">0 সিলেক্টেড</small>
            </form>
        </div>
        
        <!-- Users Table -->
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="selectAll" onchange="toggleAll(this)"></th>
                        <th>ইউজার</th>
                        <th>ইমেইল</th>
                        <th>ফোন</th>
                        <th>রোল</th>
                        <th>স্ট্যাটাস</th>
                        <th>এনরোলমেন্ট</th>
                        <th>খরচ</th>
                        <th>তারিখ</th>
                        <th>অ্যাকশন</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($users) > 0): ?>
                        <?php foreach($users as $u): 
                            $colors = ['student'=>'#6C5CE7','instructor'=>'#11998e','admin'=>'#e67e22'];
                            $color = $colors[$u['role']] ?? '#6C5CE7';
                            $initials = mb_substr($u['full_name'], 0, 1);
                        ?>
                            <tr>
                                <td><input type="checkbox" name="selected_users[]" value="<?= $u['id'] ?>" class="user-check" onchange="updateBulkCount()"></td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="user-avatar" style="background:<?= $color ?>;"><?= $initials ?></div>
                                        <div>
                                            <strong><?= htmlspecialchars(mb_substr($u['full_name'], 0, 20)) ?></strong>
                                            <?php if(mb_strlen($u['full_name']) > 20): ?>...<?php endif; ?>
                                        </div>
                                    </div>
                                </td>
                                <td><small><?= htmlspecialchars($u['email']) ?></small></td>
                                <td><?= $u['phone'] ?: 'N/A' ?></td>
                                <td>
                                    <span class="badge-custom" style="background:<?= $color ?>20;color:<?= $color ?>;">
                                        <?= $u['role'] == 'student' ? '🎓 শিক্ষার্থী' : ($u['role'] == 'instructor' ? '👨‍🏫 ইন্সট্রাক্টর' : '👑 অ্যাডমিন') ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge-custom bg-<?= ($u['status']??'active')=='suspended'?'danger':'success' ?>">
                                        <?= ($u['status']??'active')=='suspended'?'⏸️ সাসপেন্ডেড':'✅ সক্রিয়' ?>
                                    </span>
                                </td>
                                <td><?= $u['enroll_count'] ?> টি</td>
                                <td>৳<?= number_format($u['total_spent']) ?></td>
                                <td><small><?= date('d/m/y', strtotime($u['created_at'])) ?></small></td>
                                <td>
                                    <div class="btn-group">
                                        <a href="learner-details.php?id=<?= $u['id'] ?>" class="btn btn-outline-info btn-sm" title="বিস্তারিত">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <button class="btn btn-outline-primary btn-sm" title="এডিট" 
                                                onclick="editUser(<?= $u['id'] ?>, '<?= htmlspecialchars(addslashes($u['full_name'])) ?>', '<?= $u['email'] ?>', '<?= $u['phone'] ?>', '<?= $u['role'] ?>', '<?= $u['bio'] ?? '' ?>', '<?= $u['status'] ?? 'active' ?>')">
                                            <i class="fas fa-edit"></i>
                                        </button>
                                        <?php if(($u['status']??'active') != 'suspended'): ?>
                                            <a href="?action=suspend&id=<?= $u['id'] ?>&tab=<?= $tab ?>" class="btn btn-outline-warning btn-sm" title="সাসপেন্ড"
                                               onclick="return confirm('সাসপেন্ড করবেন?')">
                                                <i class="fas fa-pause"></i>
                                            </a>
                                        <?php else: ?>
                                            <a href="?action=activate&id=<?= $u['id'] ?>&tab=<?= $tab ?>" class="btn btn-outline-success btn-sm" title="অ্যাক্টিভেট">
                                                <i class="fas fa-play"></i>
                                            </a>
                                        <?php endif; ?>
                                        <a href="?action=delete&id=<?= $u['id'] ?>&tab=<?= $tab ?>" class="btn btn-outline-danger btn-sm" title="ডিলিট"
                                           onclick="return confirm('⚠️ সত্যিই ডিলিট করবেন? সব ডাটা ডিলিট হবে!')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="10" class="text-center py-5 text-muted">কোন ইউজার পাওয়া যায়নি</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-user-plus me-2"></i>নতুন ইউজার যোগ করুন</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <input type="hidden" name="add_user" value="1">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">পূর্ণ নাম *</label>
                            <input type="text" name="full_name" class="form-control" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">ইমেইল *</label>
                            <input type="email" name="email" class="form-control" required>
                        </div>
                        <div class="col-md-4 mb-3">
                            <label class="form-label">ফোন</label>
                            <input type="text" name="phone" class="form-control" placeholder="01XXXXXXXXX">
                        </div>
                        <div class="col-md-4 mb-3">
                            <label class="form-label">পাসওয়ার্ড</label>
                            <input type="password" name="password" class="form-control" value="123456">
                        </div>
                        <div class="col-md-4 mb-3">
                            <label class="form-label">রোল *</label>
                            <select name="role" class="form-select" required>
                                <option value="student">শিক্ষার্থী</option>
                                <option value="instructor">ইন্সট্রাক্টর</option>
                                <option value="admin">অ্যাডমিন</option>
                            </select>
                        </div>
                        <div class="col-12 mb-3">
                            <label class="form-label">বায়ো</label>
                            <textarea name="bio" class="form-control" rows="2"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary rounded-pill" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-primary rounded-pill">ইউজার তৈরি করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Edit User Modal -->
<div class="modal fade" id="editUserModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fas fa-edit me-2"></i>ইউজার এডিট করুন</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <input type="hidden" name="edit_user" value="1">
                <input type="hidden" name="user_id" id="editUserId">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">পূর্ণ নাম *</label>
                            <input type="text" name="full_name" id="editName" class="form-control" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">ফোন</label>
                            <input type="text" name="phone" id="editPhone" class="form-control">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">রোল</label>
                            <select name="role" id="editRole" class="form-select">
                                <option value="student">শিক্ষার্থী</option>
                                <option value="instructor">ইন্সট্রাক্টর</option>
                                <option value="admin">অ্যাডমিন</option>
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">স্ট্যাটাস</label>
                            <select name="status" id="editStatus" class="form-select">
                                <option value="active">সক্রিয়</option>
                                <option value="suspended">সাসপেন্ডেড</option>
                            </select>
                        </div>
                        <div class="col-12 mb-3">
                            <label class="form-label">বায়ো</label>
                            <textarea name="bio" id="editBio" class="form-control" rows="2"></textarea>
                        </div>
                    </div>
                    
                    <hr>
                    <h6>পাসওয়ার্ড পরিবর্তন</h6>
                    <input type="hidden" name="change_user_password" value="1">
                    <div class="row">
                        <div class="col-md-6">
                            <input type="password" name="new_password" class="form-control" placeholder="নতুন পাসওয়ার্ড (খালি রাখলে পরিবর্তন হবে না)" minlength="6">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary rounded-pill" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-primary rounded-pill">আপডেট করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function toggleAll(source) {
    document.querySelectorAll('.user-check').forEach(cb => cb.checked = source.checked);
    updateBulkCount();
}

function updateBulkCount() {
    const count = document.querySelectorAll('.user-check:checked').length;
    document.getElementById('selectedCount').textContent = count + ' সিলেক্টেড';
}

function editUser(id, name, email, phone, role, bio, status) {
    document.getElementById('editUserId').value = id;
    document.getElementById('editName').value = name;
    document.getElementById('editPhone').value = phone;
    document.getElementById('editRole').value = role;
    document.getElementById('editStatus').value = status || 'active';
    document.getElementById('editBio').value = bio || '';
    
    new bootstrap.Modal(document.getElementById('editUserModal')).show();
}
</script>
</body>
</html>