<?php
include 'db.php';

$result = $conn->query("SELECT customer_ip, SUM(CASE WHEN is_read=0 THEN 1 ELSE 0 END) AS unread
                        FROM messages GROUP BY customer_ip ORDER BY MAX(created_at) DESC");

while ($row = $result->fetch_assoc()) {
  $ip = $row['customer_ip'];
  $unread = $row['unread'] > 0 ? 'unread' : '';
  echo "<div class='customer $unread' data-ip='$ip'>$ip</div>";
}
?>
