<?php
session_start();
require_once __DIR__ . "/../config/db.php";

/* ===============================
   SECURITY CHECK
=================================*/
if (!isset($_SESSION['user_id'])) {
    header("Location: ../login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

/* ===============================
   FETCH USER INBOX
=================================*/
$stmt = mysqli_prepare($conn, "
    SELECT id, title, message, is_read, created_at
    FROM inbox_messages
    WHERE user_id = ?
    ORDER BY created_at DESC
");
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

/* ===============================
   MARK AS READ (optional auto)
=================================*/
mysqli_query($conn, "
    UPDATE inbox_messages
    SET is_read = 1
    WHERE user_id = $user_id AND is_read = 0
");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Inbox - QRCareTrack</title>

    <link rel="stylesheet" href="../assets/style.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">

    <style>
        .inbox-container {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 10px rgba(0,0,0,0.05);
        }

        .inbox-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .inbox-header h2 {
            font-size: 22px;
        }

        .inbox-table {
            width: 100%;
            border-collapse: collapse;
        }

        .inbox-table th {
            background: #f4f6f9;
            padding: 12px;
            text-align: left;
            font-size: 14px;
        }

        .inbox-table td {
            padding: 12px;
            border-bottom: 1px solid #eee;
            font-size: 14px;
        }

        .unread {
            font-weight: bold;
            background: #f9f9f9;
        }

        .read {
            color: #777;
        }

        .badge-new {
            background: #ff4d4d;
            color: white;
            padding: 3px 8px;
            border-radius: 20px;
            font-size: 11px;
        }

        .msg-preview {
            max-width: 400px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .btn-view {
            padding: 5px 10px;
            background: #2bb8a9;
            color: white;
            border-radius: 6px;
            text-decoration: none;
            font-size: 12px;
        }

        .btn-view:hover {
            background: #239b8e;
        }

        .btn-back {
            display: inline-block;
            margin-bottom: 20px;
            padding: 8px 14px;
            background: #6c757d;
            color: white;
            text-decoration: none;
            border-radius: 8px;
            font-size: 14px;
            transition: 0.3s;
        }

        .btn-back:hover {
            background: #5a6268;
        }
    </style>
</head>

<body>

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

<div class="main">

    <!-- BACK BUTTON -->
    <a href="../dashboard.php" class="btn-back">
        <i class="fas fa-arrow-left"></i> Back
    </a>

    <div class="inbox-container">

        <div class="inbox-header">
            <h2><i class="fas fa-envelope"></i> Inbox</h2>
        </div>

        <table class="inbox-table">

            <tr>
                <th>Status</th>
                <th>Title</th>
                <th>Message</th>
                <th>Date</th>
                <th>Action</th>
            </tr>

            <?php if (mysqli_num_rows($result) > 0): ?>
                <?php while ($row = mysqli_fetch_assoc($result)): ?>
                    <tr class="<?= $row['is_read'] ? 'read' : 'unread' ?>">

                        <td>
                            <?php if (!$row['is_read']): ?>
                                <span class="badge-new">NEW</span>
                            <?php else: ?>
                                ✔
                            <?php endif; ?>
                        </td>

                        <td><?= htmlspecialchars($row['title']) ?></td>

                        <td class="msg-preview">
                            <?= htmlspecialchars($row['message']) ?>
                        </td>

                        <td>
                            <?= date("d M Y h:i A", strtotime($row['created_at'])) ?>
                        </td>

                        <td>
                            <a class="btn-view"
                               href="view_message.php?id=<?= $row['id'] ?>">
                               View
                            </a>
                        </td>

                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="5" style="text-align:center; padding:20px;">
                        No messages found
                    </td>
                </tr>
            <?php endif; ?>

        </table>

    </div>

</div>

</body>
</html>