<?php
include 'includes/conn.php';
include 'includes/functions.php';

// Always define $users to prevent errors
$users = [];

$q = isset($_GET['q']) ? trim($_GET['q']) : '';

if ($q !== '') {
    $stmt = $pdo->prepare("
        SELECT * FROM users
        WHERE first_name LIKE ? 
        OR last_name LIKE ? 
        OR username LIKE ? 
        OR email LIKE ?
        OR current_class LIKE ?
        OR role LIKE ?
        ORDER BY id DESC
    ");
    $searchTerm = "%$q%";
    $stmt->execute([$searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm]);

    // Fetch results
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);

} else {
    // Default: last 6 users
    $stmt = $pdo->query("SELECT * FROM users ORDER BY id DESC LIMIT 6");

    // Fetch results
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>Photo</th>
            <th>Name</th>
            <th>Username</th>
            <th>Email</th>
            <th>Status</th>
            <th>Role</th>
            <th>Class</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php if (!empty($users)): ?>
            <?php foreach ($users as $user): ?>
                <tr>
                    <td>
                        <?php if (!empty($user['photo'])): ?>
                            <img src="data:image/jpeg;base64,<?php echo base64_encode($user['photo']); ?>" 
                                 alt="User Photo"
                                 style="width:40px; height:40px; border-radius:50%; object-fit:cover;">
                        <?php else: ?>
                            <img src="user.png"
                                 alt="Default Photo"
                                 style="width:40px; height:40px; border-radius:50%; object-fit:cover;">
                        <?php endif; ?>
                    </td>
                    <td><?php echo htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?></td>
                    <td><?php echo htmlspecialchars($user['username']); ?></td>
                    <td><?php echo htmlspecialchars($user['email']); ?></td>
                    <td>
                        <span class="badge <?= $user['status'] === 'active' ? 'bg-success' : 'bg-danger' ?>">
                        <?= ucfirst($user['status']) ?>
                        </span>
                    </td>
                    <td>
                        <span class="badge bg-<?php 
                            switch ($user['role']) {
                                case 'admin': echo 'danger'; break;
                                case 'teacher': echo 'success'; break;
                                case 'student': echo 'primary'; break;
                                default: echo 'secondary'; break;
                            }
                        ?>"><?php echo $user['role']; ?></span>
                    </td>
                    <td><?php echo htmlspecialchars($user['current_class']); ?></td>
                    <td>
                        <button                                 
                                class="btn btn-sm btn-outline-primary"
                                data-bs-toggle="modal"
                                data-bs-target="#editUserModal"
                                data-id="<?= $user['id'] ?>"
                                data-username="<?= htmlspecialchars($user['username']) ?>"
                                data-firstname="<?= htmlspecialchars($user['first_name']) ?>"
                                data-lastname="<?= htmlspecialchars($user['last_name']) ?>"
                                data-email="<?= htmlspecialchars($user['email']) ?>"
                                data-date_of_birth="<?= $user['date_of_birth'] ?>"
                                data-role="<?= $user['role'] ?>"
                                data-status="<?= $user['status'] ?>">
                            <i class="fas fa-edit"></i>
                        </button>

                        <button class="btn btn-sm btn-outline-warning"
                                data-bs-toggle="modal"
                                data-bs-target="#changePasswordModal"
                                data-id="<?php echo $user['id']; ?>"
                                data-username="<?php echo $user['username']; ?>">
                            <i class="fas fa-key"></i>
                        </button>
                        <a href="?action=delete_user&id=<?php echo $user['id']; ?>"
                           class="btn btn-sm btn-outline-danger"
                           onclick="return confirm('Are you sure you want to delete this user? This action cannot be undone.')">
                            <i class="fas fa-trash"></i>
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td colspan="7" class="text-center text-muted">Not found</td>
            </tr>
        <?php endif; ?>
    </tbody>
</table>
