<?php
$pageTitle = "My Messages";
require_once 'sidebar.php';

$uid = $_SESSION['user_id'];

if (isset($_GET['read'])) {
    $id = (int)$_GET['read'];
    mysqli_query($conn, "UPDATE messages SET is_read=1 WHERE id=$id AND receiver_id=$uid");
    redirect(BASE_URL.'/user/my-messages.php');
}

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    mysqli_query($conn, "DELETE FROM messages WHERE id=$id AND (sender_id=$uid OR receiver_id=$uid)");
    redirect(BASE_URL.'/user/my-messages.php', 'Message deleted.');
}

$tab = $_GET['tab'] ?? 'inbox';

if ($tab === 'sent') {
    $messages = mysqli_query($conn, "SELECT m.*, r.name as other_name FROM messages m 
                                     JOIN users r ON m.receiver_id=r.id 
                                     WHERE m.sender_id=$uid ORDER BY m.created_at DESC");
} else {
    $messages = mysqli_query($conn, "SELECT m.*, s.name as other_name FROM messages m 
                                     JOIN users s ON m.sender_id=s.id 
                                     WHERE m.receiver_id=$uid ORDER BY m.is_read ASC, m.created_at DESC");
}
?>

<div class="page-header">
    <h1>My Messages</h1>
    <div class="filter-group">
        <a href="?tab=inbox" class="filter-btn <?php echo $tab=='inbox'?'active':''; ?>">📥 Inbox</a>
        <a href="?tab=sent" class="filter-btn <?php echo $tab=='sent'?'active':''; ?>">📤 Sent</a>
        <a href="contact-admin.php" class="btn btn-sm btn-gold">+ New Message</a>
    </div>
</div>

<?php showFlash(); ?>

<div class="table-wrapper">
    <table>
        <thead>
            <tr>
                <th><?php echo $tab=='sent' ? 'To' : 'From'; ?></th>
                <th>Subject</th>
                <th>Message</th>
                <th>Date</th>
                <?php if ($tab=='inbox'): ?><th>Status</th><?php endif; ?>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php if (mysqli_num_rows($messages) > 0): ?>
                <?php while ($m = mysqli_fetch_assoc($messages)): ?>
                    <tr style="<?php echo ($tab=='inbox' && $m['is_read']==0)?'background:#fef8ec;':''; ?>">
                        <td><strong><?php echo htmlspecialchars($m['other_name']); ?></strong></td>
                        <td><strong style="color:var(--maroon);"><?php echo htmlspecialchars($m['subject']); ?></strong></td>
                        <td><?php echo nl2br(htmlspecialchars(substr($m['message'], 0, 120))); ?><?php echo strlen($m['message'])>120?'...':''; ?></td>
                        <td><small><?php echo date('d M Y H:i', strtotime($m['created_at'])); ?></small></td>
                        <?php if ($tab=='inbox'): ?>
                            <td>
                                <?php if ($m['is_read']==0): ?>
                                    <span class="badge badge-pending">New</span>
                                <?php else: ?>
                                    <span class="badge badge-confirmed">Read</span>
                                <?php endif; ?>
                            </td>
                        <?php endif; ?>
                        <td>
                            <?php if ($tab=='inbox' && $m['is_read']==0): ?>
                                <a href="?read=<?php echo $m['id']; ?>" class="btn btn-sm">Mark Read</a>
                            <?php endif; ?>
                            <a href="?delete=<?php echo $m['id']; ?>&tab=<?php echo $tab; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete?')">🗑</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="<?php echo $tab=='inbox'?6:5; ?>" style="text-align:center; padding:40px; color:#888;">
                    <div style="font-size:40px; color:var(--bronze); margin-bottom:10px;">❦</div>
                    No messages <?php echo $tab=='sent'?'sent':'received'; ?> yet.
                </td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

</main></div></body></html>
