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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'delete') {
    requireMemberCsrfToken();
    $deleteId = intval($_POST['id'] ?? 0);
    if ($deleteId <= 0) {
        $error = '删除参数错误';
    } else {
        $exists = $db->fetch(
            "SELECT id FROM {$db->table('contribute')} WHERE id = ? AND member_id = ?",
            [$deleteId, $memberId]
        );
        if (!$exists) {
            $error = '投稿不存在或无权删除';
        } else {
            $db->query(
                "DELETE FROM {$db->table('contribute')} WHERE id = ? AND member_id = ?",
                [$deleteId, $memberId]
            );
            $success = '投稿已删除';
        }
    }
}

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

$totalRow = $db->fetch(
    "SELECT COUNT(*) AS cnt FROM {$db->table('contribute')} WHERE member_id = ?",
    [$memberId]
);
$total = intval($totalRow['cnt'] ?? 0);
$rows = $db->fetchAll(
    "SELECT * FROM {$db->table('contribute')} WHERE member_id = ? ORDER BY id DESC LIMIT {$offset}, {$pageSize}",
    [$memberId]
);
$totalPages = max(1, (int)ceil($total / $pageSize));
$statusMap = [
    0 => ['待审核', 'status-pending'],
    1 => ['已通过', 'status-approved'],
    2 => ['已拒绝', 'status-rejected'],
];
?>
<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 ($error): ?>
    <div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
    <?php endif; ?>

    <?php if ($success): ?>
    <div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div>
    <?php endif; ?>

    <?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:120px;">栏目</th>
                <th style="width:80px;">类型</th>
                <th style="width:110px;">时间</th>
                <th style="width:90px;">状态</th>
                <th style="width:160px;">操作</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): ?>
            <?php $statusInfo = $statusMap[intval($row['status'])] ?? ['未知', '']; ?>
            <tr>
                <td><?php echo intval($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 intval($row['content_type']) === 2 ? '产品' : '文章'; ?></td>
                <td><?php echo !empty($row['add_time']) ? date('Y-m-d', intval($row['add_time'])) : '-'; ?></td>
                <td><span class="status <?php echo $statusInfo[1]; ?>"><?php echo $statusInfo[0]; ?></span></td>
                <td>
                    <div class="op-actions">
                        <a href="?page=contribute_edit&id=<?php echo intval($row['id']); ?>" class="text-link">编辑</a>
                        <form method="post" class="inline-form" onsubmit="return confirm('确定删除这条投稿吗？');">
                            <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getMemberCsrfToken()); ?>">
                            <input type="hidden" name="action" value="delete">
                            <input type="hidden" name="id" value="<?php echo intval($row['id']); ?>">
                            <button type="submit" class="text-link text-danger">删除</button>
                        </form>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php if ($totalPages > 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 <= $totalPages; $i++): ?>
        <a href="?page=contribute&p=<?php echo $i; ?>" class="<?php echo $i === $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
        <?php endfor; ?>
        <?php if ($page < $totalPages): ?><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; }
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 14px;
    margin-bottom: 20px;
}
.alert-error { background: #fee2e2; color: #dc2626; }
.alert-success { background: #d1fae5; color: #059669; }
.op-actions { display: flex; align-items: center; gap: 12px; }
.inline-form { display: inline; margin: 0; }
.text-link {
    color: #5a8f3e;
    text-decoration: none;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0;
    font-size: 13px;
}
.text-danger { color: #dc2626; }
</style>
