<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require_once '../../function/connect.php';

// cek login
if (!isset($_SESSION['id']) || $_SESSION['status'] != "login") {
    echo "Harus login dulu";
    exit;
}

$user_id = $_SESSION['id'];
// ambil pesan
$query = mysqli_query($koneksi, "
    SELECT * FROM messages 
    WHERE user_id='$user_id' 
    ORDER BY id DESC
");

// update jadi read
mysqli_query($koneksi, "
    UPDATE messages 
    SET status='read' 
    WHERE user_id='$user_id'
");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Pesan Masuk</title>
    <style>
        body {
            font-family: Arial;
            background:#f5f5f5;
            padding:10px;
        }
        .msg {
            background:#fff;
            padding:12px;
            margin-bottom:10px;
            border-radius:6px;
            box-shadow:0 0 5px rgba(0,0,0,0.1);
        }
        .title {
            font-weight:bold;
            margin-bottom:5px;
        }
        .date {
            font-size:12px;
            color:#888;
        }
    </style>
</head>
<body>

<h3>📩 Pesan Masuk</h3>

<?php while($row = mysqli_fetch_assoc($query)) { ?>
    <div class="msg">
        <div class="title"><?= $row['title']; ?></div>
        <div><?= $row['message']; ?></div>
        <div class="date"><?= $row['created_at']; ?></div>
    </div>
<?php } ?>

</body>
</html>