<?php
require_once dirname(__DIR__, 2) . '/core/lib/product_review_service.php';
\Core\Lib\ProductReviewService::ensureTable($db);
$memberId = intval($_SESSION['member_id'] ?? 0);
$page = max(1, intval($_GET['p'] ?? 1));
$pageSize = 15;
$offset = ($page - 1) * $pageSize;
$total = intval($db->fetch("SELECT COUNT(*) AS cnt FROM {$db->table('product_review')} WHERE member_id = ?", [$memberId])['cnt'] ?? 0);
$totalPages = max(1, (int) ceil($total / $pageSize));
$reviews = $db->fetchAll("SELECT r.*, p.title AS product_title FROM {$db->table('product_review')} r LEFT JOIN {$db->table('product')} p ON p.id = r.product_id WHERE r.member_id = ? ORDER BY r.id DESC LIMIT {$offset}, {$pageSize}", [$memberId]);
$statusMap = [0 => ['待审核', 'pending'], 1 => ['已显示', 'success'], 2 => ['未通过', 'rejected']];
?>
<div class="page-header"><h1 class="page-title">我的评价</h1></div>
<div class="page-content review-page">
    <?php if (!$reviews): ?><div class="review-empty">暂无评价记录</div><?php endif; ?>
    <?php foreach ($reviews as $review): $status = $statusMap[intval($review['status'])] ?? ['未知', 'pending']; $rating = max(0, min(5, intval($review['rating']))); ?>
        <article class="review-card">
            <div class="review-head"><a href="<?php echo htmlspecialchars(cmsBuildFrontendUrl('product', intval($review['product_id']))); ?>" target="_blank"><?php echo htmlspecialchars($review['product_title'] ?: '商品已删除'); ?></a><span class="is-<?php echo $status[1]; ?>"><?php echo $status[0]; ?></span></div>
            <div class="review-stars"><?php echo str_repeat('★', $rating) . str_repeat('☆', 5 - $rating); ?></div>
            <p><?php echo nl2br(htmlspecialchars($review['content'])); ?></p>
            <?php if (trim((string) ($review['reply'] ?? '')) !== ''): ?><div class="review-reply"><strong>商家回复：</strong><?php echo nl2br(htmlspecialchars($review['reply'])); ?></div><?php endif; ?>
            <time><?php echo date('Y-m-d H:i', intval($review['create_time'])); ?></time>
        </article>
    <?php endforeach; ?>
    <?php if ($totalPages > 1): ?><div class="review-pagination"><?php if ($page > 1): ?><a href="?page=reviews&p=<?php echo $page - 1; ?>">上一页</a><?php endif; ?><span><?php echo $page; ?> / <?php echo $totalPages; ?></span><?php if ($page < $totalPages): ?><a href="?page=reviews&p=<?php echo $page + 1; ?>">下一页</a><?php endif; ?></div><?php endif; ?>
</div>
<style>
.review-page{display:grid;gap:12px;padding:22px}.review-card{padding:18px 20px;border:1px solid #e2e8f0;border-radius:14px;background:#fff}.review-head{display:flex;justify-content:space-between;gap:12px}.review-head a{color:#1e293b;font-weight:600;text-decoration:none}.review-head span{padding:4px 8px;border-radius:999px;font-size:11px}.review-head .is-pending{background:#fff7ed;color:#c2410c}.review-head .is-success{background:#ecfdf5;color:#047857}.review-head .is-rejected{background:#fef2f2;color:#b91c1c}.review-stars{margin:10px 0 6px;color:#f59e0b;letter-spacing:2px}.review-card p{margin:0;color:#475569;line-height:1.75}.review-card time{display:block;margin-top:10px;color:#94a3b8;font-size:12px}.review-reply{margin-top:12px;padding:12px;border-radius:10px;background:#f8fafc;color:#64748b;line-height:1.65}.review-empty{padding:60px;text-align:center;color:#94a3b8}.review-pagination{display:flex;align-items:center;justify-content:center;gap:12px;margin-top:8px;color:#64748b;font-size:13px}.review-pagination a{padding:8px 12px;border:1px solid #dbe4ee;border-radius:9px;background:#fff;color:#475569;text-decoration:none}
</style>
