<?php
require 'db.php';
$customer_ip = $_POST['customer_ip'];
$mark_read = isset($_POST['mark_read']);

$result = $conn->query("SELECT * FROM messages WHERE customer_ip='$customer_ip' ORDER BY id ASC");

if(mysqli_num_rows($result) > 0){
    while ($row = $result->fetch_assoc()) {
        $class = strtolower($row['sender']);
        echo "<div class='message $class'>{$row['message']}</div>";
    }
}

if ($mark_read) {
  $conn->query("UPDATE messages SET is_read=1 WHERE customer_ip='$customer_ip' AND sender='Customer'");
}
?>
