fetchAll($sql, $params); $totalRow = $db->fetch( "SELECT COUNT(DISTINCT c.user_id) AS cnt FROM conversations c JOIN users u ON u.id = c.user_id WHERE c.canal = 'turnero'" . ($search !== '' ? " AND (u.name LIKE :s1 OR u.phone_number LIKE :s2)" : ''), $search !== '' ? [':s1' => '%'.$search.'%', ':s2' => '%'.$search.'%'] : [] ); $data = array_map(function($r) { return [ 'user_id' => intval($r['user_id']), 'name' => $r['name'], 'phone_number' => $r['phone_number'], 'avatar_url' => $r['avatar_url'] ?? null, 'last_message' => $r['last_message'] ?? '', 'last_direction' => $r['last_direction'] ?? 'incoming', 'last_message_type' => $r['last_message_type'] ?? 'text', 'last_time' => $r['last_time'] ?? null, 'unread_count' => intval($r['unread_count']), ]; }, $rows ?: []); $total = intval($totalRow['cnt'] ?? 0); $hasMore = ($page * $limit) < $total; echo json_encode(['success' => true, 'data' => $data, 'total' => $total, 'has_more' => $hasMore, 'page' => $page]); } catch (Exception $e) { error_log('chat_get_list.php: ' . $e->getMessage()); http_response_code(500); echo json_encode(['success' => false, 'error' => 'Error interno']); }