<?php
/**
 * 投稿列表
 */

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

// 投稿文章表
$total = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('contribute')} WHERE member_id = ?", [$_SESSION['member_id']])['cnt'];
$rows = $db->fetchAll("SELECT * FROM {$db->table('contribute')} WHERE member_id = ? ORDER BY id DESC LIMIT {$offset}, {$page_size}", [$_SESSION['member_id']]);

$total_pages = ceil($total / $page_size);
?>
<div class="page-header">
    <div class="page-header-actions">
        <a href="?page=contribute_add" class="btn btn-primary">
            <i class="icon-plus"></i> 投稿文章
        </a>
    </div>
    <h1 class="page-title">
        <i class="icon-file-text"></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>
        <a href="?page=contribute_add" class="btn btn-outline" style="margin-top:16px;">立即投稿</a>
    </div>
    <?php else: ?>
    <table class="table">
        <thead>
            <tr>
                <th style="width:60px;">ID</th>
                <th>标题</th>
                <th style="width:100px;">栏目</th>
                <th style="width:80px;">类型</th>
                <th style="width:100px;">时间</th>
                <th style="width:80px;">状态</th>
                <th style="width:100px;">操作</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><a href="javascript:;"><?php echo htmlspecialchars($row['title']); ?></a></td>
                <td><?php echo htmlspecialchars($row['channel_name'] ?? '-'); ?></td>
                <td><?php echo $row['content_type'] == 1 ? '文章' : '产品'; ?></td>
                <td><?php echo date('Y-m-d', $row['add_time'] ?? time()); ?></td>
                <td>
                    <?php
                    $status_map = [0 => ['待审', 'status-pending'], 1 => ['通过', 'status-approved'], 2 => ['拒绝', 'status-rejected']];
                    $s = $status_map[intval($row['status'])] ?? ['未知', ''];
                    ?>
                    <span class="status <?php echo $s[1]; ?>"><?php echo $s[0]; ?></span>
                </td>
                <td>
                    <a href="?page=contribute_edit&id=<?php echo $row['id']; ?>">编辑</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php if ($total_pages > 1): ?>
    <div class="pagination">
        <?php if ($page > 1): ?><a href="?page=contribute&p=<?php echo $page - 1; ?>">上一页</a><?php endif; ?>
        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
        <a href="?page=contribute&p=<?php echo $i; ?>" class="<?php echo $i === $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
        <?php endfor; ?>
        <?php if ($page < $total_pages): ?><a href="?page=contribute&p=<?php echo $page + 1; ?>">下一页</a><?php endif; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>
<style>
.page-header { display: flex; justify-content: space-between; align-items: center; }
.page-header-actions { display: flex; gap: 8px; }
</style>