<?php
include 'includes/header.php';
include 'includes/sidebar.php';
require '../includes/db.php';

// ===============================
// ?? SEND REPLY
// ===============================
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $user_id = $_POST['user_id'];
    $msg     = $_POST['msg'];

    if(!empty($msg)){
        $stmt = $conn->prepare("
            INSERT INTO messages (sender_id, receiver_id, message)
            VALUES (?, ?, ?)
        ");
        $stmt->bind_param("iis", $_SESSION['admin_id'], $user_id, $msg);
        $stmt->execute();
    }
}

// ===============================
// ??? DELETE MESSAGE
// ===============================
if (isset($_GET['delete'])) {

    $id = (int)$_GET['delete'];

    $stmt = $conn->prepare("DELETE FROM messages WHERE id=?");
    $stmt->bind_param("i", $id);
    $stmt->execute();

    header("Location: messages.php");
    exit;
}

// ===============================
// ?? FETCH WITH USER EMAIL
// ===============================
$res = mysqli_query($conn,"
SELECT m.*, u.email 
FROM messages m
LEFT JOIN users u ON u.id = m.sender_id
ORDER BY m.id DESC
");
?>

<div class="container-fluid">

    <div class="row">
        <div class="col-xl-7 col-lg-10 mx-auto">

            <div class="card p-3">

                <!-- Header -->
                <h4 class="text-primary mb-4">?? Messages</h4>

                <!-- Messages -->
                <div style="max-height:500px; overflow-y:auto;">

                    <?php while($m=mysqli_fetch_assoc($res)): ?>

                        <div class="mb-3 p-3 rounded" style="background:#020617; border:1px solid #1e293b;">

                            <!-- USER EMAIL -->
                            <small class="text-info">
                                <?php echo $m['email'] ?? 'Admin'; ?>
                            </small>

                            <!-- MESSAGE -->
                            <p class="mb-2 text-white">
                                <?php echo htmlspecialchars($m['message']); ?>
                            </p>

                            <!-- ACTIONS -->
                            <div class="d-flex justify-content-between align-items-center">

                                <!-- REPLY -->
                                <form method="POST" class="d-flex gap-2 w-75">

                                    <input type="hidden" name="user_id" value="<?php echo $m['sender_id']; ?>">

                                    <input 
                                        type="text" 
                                        name="msg" 
                                        class="form-control form-control-sm"
                                        placeholder="Reply..."
                                        required
                                    >

                                    <button class="btn btn-sm btn-primary">
                                        Send
                                    </button>

                                </form>

                                <!-- DELETE -->
                                <a href="?delete=<?php echo $m['id']; ?>" 
                                   class="btn btn-sm btn-danger ms-2"
                                   onclick="return confirm('Delete this message?')">
                                   Delete
                                </a>

                            </div>

                        </div>

                    <?php endwhile; ?>

                </div>

            </div>

        </div>
    </div>

</div>

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