<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */

$memberId = intval($_SESSION['member_id'] ?? 0);
$guestbookTable = $db->table('guestbook');
$page = max(1, intval($_GET['p'] ?? 1));
$pageSize = 10;
$offset = ($page - 1) * $pageSize;

$total = 0;
$rows = [];
$totalPages = 1;
if ($memberId > 0) {
    $totalRow = $db->fetch(
        "SELECT COUNT(*) as cnt FROM {$guestbookTable} WHERE member_id = ?",
        [$memberId]
    );
    $total = intval($totalRow['cnt'] ?? 0);
    $rows = $db->fetchAll(
        "SELECT * FROM {$guestbookTable} WHERE member_id = ? ORDER BY id DESC LIMIT {$offset}, {$pageSize}",
        [$memberId]
    );
    $totalPages = max(1, (int) ceil($total / $pageSize));
}
?>
<div class="page-header">
    <h1 class="page-title">
        <i class="icon-message-square"></i>
        我的留言
    </h1>
</div>
<div class="page-content">
    <?php if (empty($rows)): ?>
    <div class="empty-state">
        <div class="empty-state-icon">💬</div>
        <div class="empty-state-text">暂无留言记录</div>
    </div>
    <?php else: ?>
    <div class="guestbook-list">
        <?php foreach ($rows as $row): ?>
        <?php
        $isRead = intval($row['status'] ?? 0) >= 1 || intval($row['is_show'] ?? 0) === 1 || !empty($row['reply']);
        $isReplied = !empty($row['reply']) || intval($row['status'] ?? 0) === 2 || !empty($row['reply_time']);
        ?>
        <div class="guestbook-card">
            <div class="guestbook-head">
                <div>
                    <div class="guestbook-title"><?php echo htmlspecialchars($row['title'] ?: '未命名留言'); ?></div>
                    <div class="guestbook-meta">
                        <span>提交时间：<?php echo !empty($row['create_time']) ? date('Y-m-d H:i', intval($row['create_time'])) : '-'; ?></span>
                        <span>ID：<?php echo intval($row['id']); ?></span>
                    </div>
                </div>
                <div class="guestbook-statuses">
                    <span class="status-badge <?php echo $isRead ? 'status-read' : 'status-unread'; ?>"><?php echo $isRead ? '已读' : '未读'; ?></span>
                    <span class="status-badge <?php echo $isReplied ? 'status-replied' : 'status-unreplied'; ?>"><?php echo $isReplied ? '已回复' : '未回复'; ?></span>
                </div>
            </div>
            <div class="guestbook-body">
                <div class="guestbook-label">留言内容</div>
                <div class="guestbook-content"><?php echo nl2br(htmlspecialchars($row['content'] ?? '')); ?></div>
            </div>
            <?php if (!empty($row['reply'])): ?>
            <div class="guestbook-reply">
                <div class="guestbook-label">管理员回复</div>
                <div class="guestbook-reply-content"><?php echo nl2br(htmlspecialchars($row['reply'])); ?></div>
                <div class="guestbook-reply-time">回复时间：<?php echo !empty($row['reply_time']) ? date('Y-m-d H:i', intval($row['reply_time'])) : '-'; ?></div>
            </div>
            <?php endif; ?>
        </div>
        <?php endforeach; ?>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="pagination">
        <?php if ($page > 1): ?><a href="?page=guestbook&p=<?php echo $page - 1; ?>">上一页</a><?php endif; ?>
        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
        <a href="?page=guestbook&p=<?php echo $i; ?>" class="<?php echo $i === $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
        <?php endfor; ?>
        <?php if ($page < $totalPages): ?><a href="?page=guestbook&p=<?php echo $page + 1; ?>">下一页</a><?php endif; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>
<style>
.guestbook-list {
    display: grid;
    gap: 16px;
}
.guestbook-card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 14px;
    padding: 18px;
    box-shadow: 0 8px 22px rgba(15, 23, 42, 0.05);
}
.guestbook-head {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: flex-start;
    margin-bottom: 14px;
}
.guestbook-title {
    font-size: 16px;
    font-weight: 600;
    color: #0f172a;
    margin-bottom: 6px;
}
.guestbook-meta {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    font-size: 12px;
    color: #64748b;
}
.guestbook-statuses {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}
.status-badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
}
.status-read {
    background: #ecfdf5;
    color: #047857;
}
.status-unread {
    background: #fff7ed;
    color: #c2410c;
}
.status-replied {
    background: #eff6ff;
    color: #1d4ed8;
}
.status-unreplied {
    background: #f8fafc;
    color: #64748b;
}
.guestbook-label {
    font-size: 12px;
    color: #64748b;
    margin-bottom: 6px;
}
.guestbook-content {
    color: #334155;
    line-height: 1.7;
    white-space: normal;
    word-break: break-word;
}
.guestbook-reply {
    margin-top: 14px;
    padding: 14px;
    background: #f8fafc;
    border-radius: 12px;
    border: 1px solid #e2e8f0;
}
.guestbook-reply-content {
    color: #0f172a;
    line-height: 1.7;
    word-break: break-word;
}
.guestbook-reply-time {
    margin-top: 8px;
    font-size: 12px;
    color: #64748b;
}
@media (max-width: 768px) {
    .guestbook-head {
        flex-direction: column;
    }
    .guestbook-statuses {
        justify-content: flex-start;
    }
}
</style>
