<?php include "../includes/header.php"; ?>
<?php include "../includes/sidebar.php"; ?>

<?php
$id = $_GET['id'];
$message = $conn->query("SELECT * FROM messages WHERE id=$id")->fetch_assoc();
$conn->query("UPDATE messages SET is_read=1 WHERE id=$id");

if ($_POST) {
    $reply = $_POST['reply'];
    $to = $message['email'];
    $subject = "Reply: " . $message['subject'];
    $headers = "From: admin@yourdomain.com";

    mail($to, $subject, $reply, $headers);
    $success = true;
}
?>

<h2>Message from <?= $message['name'] ?></h2>

<div class="bg-white p-4 rounded mb-4">
    <p><strong>Email:</strong> <?= $message['email'] ?></p>
    <p><strong>Subject:</strong> <?= $message['subject'] ?></p>
    <p><?= nl2br($message['message']) ?></p>
</div>

<h4>Reply</h4>

<?php if(isset($success)): ?>
<div class="alert alert-success">Reply sent successfully</div>
<?php endif; ?>

<form method="post" class="bg-white p-4 rounded">
    <textarea name="reply" class="form-control mb-3" rows="4" placeholder="Type your reply..." required></textarea>
    <button class="btn btn-dark">Send Reply</button>
</form>

<?php include "../includes/footer.php"; ?>
