<?php
$memberId = intval($_SESSION['member_id'] ?? 0);
cmsEnsureMemberMessageTable($db);
$page = max(1, intval($_GET['p'] ?? 1));
$pageSize = 15;
$offset = ($page - 1) * $pageSize;
$counts = $db->fetch("SELECT COUNT(*) AS total, SUM(is_read = 0) AS unread FROM {$db->table('member_message')} WHERE to_id = ?", [$memberId]);
$total = intval($counts['total'] ?? 0);
$unread = intval($counts['unread'] ?? 0);
$totalPages = max(1, (int) ceil($total / $pageSize));
$messages = $db->fetchAll("SELECT * FROM {$db->table('member_message')} WHERE to_id = ? ORDER BY id DESC LIMIT {$offset}, {$pageSize}", [$memberId]);
?>
<div class="page-header"><h1 class="page-title">站内消息</h1><?php if ($unread): ?><form method="post"><input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getMemberCsrfToken()); ?>"><input type="hidden" name="message_action" value="read_all"><input type="hidden" name="p" value="<?php echo $page; ?>"><button class="message-read-all">全部已读</button></form><?php endif; ?></div>
<div class="page-content message-page">
    <?php if (!$messages): ?><div class="message-empty">暂无站内消息</div><?php endif; ?>
    <?php foreach ($messages as $message): ?>
        <article class="message-card <?php echo intval($message['is_read']) ? '' : 'is-unread'; ?>">
            <div class="message-head"><h3><?php echo htmlspecialchars($message['title'] ?: '系统消息'); ?></h3><?php if (!intval($message['is_read'])): ?><span>未读</span><?php endif; ?></div>
            <div class="message-content"><?php echo nl2br(htmlspecialchars($message['content'])); ?></div>
            <div class="message-foot"><time><?php echo date('Y-m-d H:i', intval($message['create_time'])); ?></time><?php if (!intval($message['is_read'])): ?><form method="post"><input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getMemberCsrfToken()); ?>"><input type="hidden" name="message_action" value="read"><input type="hidden" name="id" value="<?php echo intval($message['id']); ?>"><input type="hidden" name="p" value="<?php echo $page; ?>"><button>标为已读</button></form><?php endif; ?></div>
        </article>
    <?php endforeach; ?>
    <?php if ($totalPages > 1): ?><div class="message-pagination"><?php if ($page > 1): ?><a href="?page=messages&p=<?php echo $page - 1; ?>">上一页</a><?php endif; ?><span><?php echo $page; ?> / <?php echo $totalPages; ?></span><?php if ($page < $totalPages): ?><a href="?page=messages&p=<?php echo $page + 1; ?>">下一页</a><?php endif; ?></div><?php endif; ?>
</div>
<style>
.page-header{display:flex;align-items:center;justify-content:space-between}.message-read-all,.message-foot button{padding:8px 12px;border:1px solid #dbe4ee;border-radius:9px;background:#fff;color:#475569;cursor:pointer}.message-page{display:grid;gap:12px;padding:22px}.message-card{padding:18px 20px;border:1px solid #e2e8f0;border-radius:14px;background:#fff}.message-card.is-unread{border-left:4px solid #10b981}.message-head,.message-foot{display:flex;align-items:center;justify-content:space-between;gap:12px}.message-head h3{margin:0;color:#1e293b;font-size:15px}.message-head span{padding:4px 8px;border-radius:999px;background:#ecfdf5;color:#047857;font-size:11px}.message-content{margin:12px 0;color:#475569;line-height:1.75}.message-foot time{color:#94a3b8;font-size:12px}.message-foot form{margin:0}.message-empty{padding:60px;text-align:center;color:#94a3b8}.message-pagination{display:flex;align-items:center;justify-content:center;gap:12px;margin-top:8px;color:#64748b;font-size:13px}.message-pagination a{padding:8px 12px;border:1px solid #dbe4ee;border-radius:9px;background:#fff;color:#475569;text-decoration:none}
</style>
