<?php
session_start();
if (!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';

$chats = $conn->query("
    SELECT c.*, d1.name as sender_name, d2.name as receiver_name 
    FROM doctor_doctor_chat c
    LEFT JOIN doctors d1 ON c.sender_id = d1.doctor_id
    LEFT JOIN doctors d2 ON c.receiver_id = d2.doctor_id
    ORDER BY c.created_at DESC
");
?>
<!DOCTYPE html>
<html>
<head><title>Doctor-Doctor Chats</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"></head>
<body>
<div class="container mt-4">
    <h2>Doctor-Doctor Conversations</h2>
    <table class="table table-bordered">
        <thead><tr><th>From</th><th>To</th><th>Message</th><th>Date</th><th>Read</th></tr></thead>
        <tbody>
        <?php while($c = $chats->fetch_assoc()): ?>
        <tr>
            <td>Dr. <?= $c['sender_name'] ?></td>
            <td>Dr. <?= $c['receiver_name'] ?></td>
            <td><?= htmlspecialchars($c['message']) ?></td>
            <td><?= date('M d, H:i', strtotime($c['created_at'])) ?></td>
            <td><?= $c['is_read'] ? 'Yes' : 'No' ?></td>
        </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
    <a href="dashboard.php" class="btn btn-secondary">Back</a>
</div>
</body>
</html>