<?php
require_once dirname(__DIR__) . '/config.php';
requireLogin();
$pageTitle = '我的回复';
$pdo = getDB();
$uid = (int)$_SESSION['user_id'];

$page     = max(1, intval($_GET['page'] ?? 1));
$per_page = 15;
$offset   = ($page - 1) * $per_page;

$count_stmt = $pdo->prepare("SELECT COUNT(*) FROM forum_replies WHERE user_id = ?");
$count_stmt->execute([$uid]);
$total = (int)$count_stmt->fetchColumn();

$stmt = $pdo->prepare("
    SELECT r.*, p.title as post_title
    FROM forum_replies r
    JOIN forum_posts p ON p.id = r.post_id
    WHERE r.user_id = ?
    ORDER BY r.created_at DESC
    LIMIT $per_page OFFSET $offset
");
$stmt->execute([$uid]);
$replies = $stmt->fetchAll();
$total_pages = max(1, ceil($total / $per_page));

require_once dirname(__DIR__) . '/includes/header.php';
?>
<style>
.reply-row { background: #fff; border-radius: 12px; border: 1px solid #f0e8ff; padding: 16px 20px; margin-bottom: 10px; transition: .2s; }
.reply-row:hover { box-shadow: 0 4px 16px rgba(123,47,247,.1); border-color: #c084fc; }
.reply-row-post { font-size: .78rem; color: #7b2ff7; font-weight: 600; margin-bottom: 8px; text-decoration: none; display: block; }
.reply-row-post:hover { text-decoration: underline; }
.reply-row-content { font-size: .88rem; color: #555; line-height: 1.6; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.reply-row-content img { display: none; }
.reply-row-meta { font-size: .75rem; color: #aaa; margin-top: 8px; display: flex; gap: 14px; flex-wrap: wrap; }
</style>

<div class="container py-4">
    <div class="row">
        <!-- Sidebar -->
        <div class="col-md-3 col-lg-2 mb-4">
            <?php require_once __DIR__ . '/sidebar.php'; ?>
        </div>

        <!-- Main -->
        <div class="col-md-9 col-lg-10">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h5 class="fw-bold mb-0"><i class="fas fa-reply me-2 text-primary"></i>我的回复</h5>
                <span class="text-muted small">共 <?php echo $total; ?> 条回复</span>
            </div>

            <?php if (empty($replies)): ?>
            <div class="text-center py-5 text-muted">
                <i class="fas fa-comment-slash fa-3x mb-3 d-block" style="color:#e0d0ff;"></i>
                <p>您还没有发表任何回复</p>
                <a href="/forum.php" class="btn btn-sm btn-outline-primary">去讨论区看看</a>
            </div>
            <?php else: ?>
            <?php foreach ($replies as $reply): ?>
            <div class="reply-row">
                <a href="/forum-post.php?id=<?php echo $reply['post_id']; ?>#reply-<?php echo $reply['id']; ?>" class="reply-row-post">
                    <i class="fas fa-comments me-1"></i><?php echo e($reply['post_title']); ?>
                </a>
                <div class="reply-row-content"><?php echo strip_tags($reply['content']); ?></div>
                <div class="reply-row-meta">
                    <span><i class="fas fa-clock me-1"></i><?php echo date('Y-m-d H:i', strtotime($reply['created_at'])); ?></span>
                    <a href="/forum-delete.php?type=reply&id=<?php echo $reply['id']; ?>&post_id=<?php echo $reply['post_id']; ?>" class="text-danger ms-auto" style="font-size:.72rem;" onclick="return confirm('确定删除这条回复？')">
                        <i class="fas fa-trash me-1"></i>删除
                    </a>
                </div>
            </div>
            <?php endforeach; ?>

            <?php if ($total_pages > 1): ?>
            <nav class="mt-4">
                <ul class="pagination justify-content-center">
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
                        <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                    </li>
                    <?php endfor; ?>
                </ul>
            </nav>
            <?php endif; ?>
            <?php endif; ?>
        </div><!-- .col -->
    </div><!-- .row -->
</div><!-- .container -->

<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
