<?php
/**
 * 我的留言
 */

// 分页
$page = max(1, intval($_GET['p'] ?? 1));
$page_size = 10;
$offset = ($page - 1) * $page_size;

// 按会员标识查询（name字段存会员名，is_show控制显示）
$member_name = $_SESSION['member_name'] ?? '';
$where = "name = ? AND is_show = 1";
$params = [$member_name];

$total = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('guestbook')} WHERE {$where}", $params)['cnt'];
$rows = $db->fetchAll("SELECT * FROM {$db->table('guestbook')} WHERE {$where} ORDER BY id DESC LIMIT {$offset}, {$page_size}", $params);

$total_pages = ceil($total / $page_size);
?>
<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: ?>
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>留言内容</th>
                <th>留言时间</th>
                <th>状态</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo htmlspecialchars(mb_substr($row['content'], 0, 50)); ?></td>
                <td><?php echo date('Y-m-d H:i', $row['add_time']); ?></td>
                <td>
                    <?php
                    $status_map = [0 => ['待审核', 'status-pending'], 1 => ['已通过', 'status-approved'], 2 => ['已拒绝', 'status-rejected']];
                    $s = $status_map[$row['status']] ?? ['未知', ''];
                    ?>
                    <span class="status <?php echo $s[1]; ?>"><?php echo $s[0]; ?></span>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php if ($total_pages > 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 <= $total_pages; $i++): ?>
        <a href="?page=guestbook&p=<?php echo $i; ?>" class="<?php echo $i === $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
        <?php endfor; ?>
        <?php if ($page < $total_pages): ?><a href="?page=guestbook&p=<?php echo $page + 1; ?>">下一页</a><?php endif; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>