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

// Mark message as read
if (isset($_GET['mark_read'])) {
    $id = (int)$_GET['mark_read'];
    $conn->query("UPDATE patient_doctor_messages SET is_read = 1 WHERE id = $id");
    header("Location: manage_messages.php");
    exit();
}

// Delete message
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $conn->query("DELETE FROM patient_doctor_messages WHERE id = $id");
    header("Location: manage_messages.php");
    exit();
}

// Get all messages
$messages = $conn->query("
    SELECT pm.*, 
           p.first_name as patient_first, p.last_name as patient_last, p.patient_id,
           d.name as doctor_name, d.doctor_id
    FROM patient_doctor_messages pm
    LEFT JOIN patients p ON pm.patient_id = p.patient_id
    LEFT JOIN doctors d ON pm.doctor_id = d.doctor_id
    ORDER BY pm.created_at DESC
");

$unread_count = $conn->query("SELECT COUNT(*) as cnt FROM patient_doctor_messages WHERE is_read = 0")->fetch_assoc()['cnt'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Messages - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { background: #0a3d2f; font-family: 'Segoe UI', sans-serif; }
        .navbar { background: #064e3b !important; }
        .card { border-radius: 20px; background: rgba(255,255,255,0.95); }
        .message-row { border-bottom: 1px solid #eee; padding: 15px; transition: 0.2s; }
        .message-row:hover { background: #f9f9f9; }
        .message-unread { background: #f0fdf4; border-left: 4px solid #0f6b4c; }
        .badge-unread { background: #dc3545; color: white; border-radius: 50%; padding: 2px 6px; font-size: 10px; }
    </style>
</head>
<body>
<nav class="navbar navbar-dark">
    <div class="container">
        <span class="navbar-brand"><i class="fas fa-envelope"></i> Manage Messages</span>
        <div>
            <?php if($unread_count > 0): ?>
                <span class="badge bg-danger me-2"><?= $unread_count ?> unread</span>
            <?php endif; ?>
            <a href="dashboard.php" class="btn btn-light btn-sm">Back</a>
        </div>
    </div>
</nav>

<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-success text-white">
            <h5>All Patient-Doctor Messages</h5>
        </div>
        <div class="card-body p-0">
            <?php if($messages && $messages->num_rows > 0): ?>
                <?php while($m = $messages->fetch_assoc()): ?>
                    <div class="message-row <?= !$m['is_read'] ? 'message-unread' : '' ?>">
                        <div class="row align-items-center">
                            <div class="col-md-3">
                                <strong>
                                    <?php if($m['sender'] == 'patient'): ?>
                                        <i class="fas fa-user-circle text-primary"></i> <?= htmlspecialchars($m['patient_first'] . ' ' . $m['patient_last']) ?>
                                    <?php else: ?>
                                        <i class="fas fa-user-md text-success"></i> Dr. <?= htmlspecialchars($m['doctor_name']) ?>
                                    <?php endif; ?>
                                </strong>
                                <br>
                                <small class="text-muted">To: 
                                    <?php if($m['sender'] == 'patient'): ?>
                                        Dr. <?= htmlspecialchars($m['doctor_name']) ?>
                                    <?php else: ?>
                                        <?= htmlspecialchars($m['patient_first'] . ' ' . $m['patient_last']) ?>
                                    <?php endif; ?>
                                </small>
                            </div>
                            <div class="col-md-5">
                                <div class="small text-muted"><?= date('M d, Y H:i', strtotime($m['created_at'])) ?></div>
                                <div><?= htmlspecialchars(substr($m['message'], 0, 80)) ?>...</div>
                            </div>
                            <div class="col-md-2">
                                <?php if(!$m['is_read']): ?>
                                    <span class="badge bg-warning text-dark">Unread</span>
                                <?php else: ?>
                                    <span class="badge bg-secondary">Read</span>
                                <?php endif; ?>
                            </div>
                            <div class="col-md-2 text-end">
                                <a href="view_message.php?id=<?= $m['id'] ?>" class="btn btn-sm btn-primary">
                                    <i class="fas fa-eye"></i> View
                                </a>
                                <a href="?delete=<?= $m['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this message?')">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="text-center p-5 text-muted">
                    <i class="fas fa-inbox fa-3x mb-3"></i>
                    <p>No messages found.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>