<?php
session_start();
// 注意：因为此文件在 user 目录下，所以引用根目录的 config 需要加 ../
require_once '../config.php'; 

// 检查登录
if (!isset($_SESSION['user_logged_in']) || $_SESSION['user_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}

$user_id = $_SESSION['user_id'];

// 获取该用户所有的投稿记录
$stmt = mysqli_prepare($conn, "SELECT * FROM apps WHERE user_id = ? ORDER BY id DESC");
mysqli_stmt_bind_param($stmt, "i", $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的投稿记录</title>
    <style>
        body { background: #f4f6f9; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; padding: 20px; margin: 0; }
        .container { max-width: 900px; margin: 0 auto; background: #fff; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); overflow: hidden; }
        .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 20px; color: white; text-align: center; }
        .header h1 { margin: 0; font-size: 22px; }
        .header a { color: rgba(255,255,255,0.8); text-decoration: none; font-size: 14px; display: inline-block; margin-top: 10px; }
        
        /* 表格自适应 */
        .table-wrapper { width: 100%; overflow-x: auto; }
        table { width: 100%; border-collapse: collapse; min-width: 600px; }
        th { background: #f8f9fa; padding: 12px; text-align: left; font-size: 14px; color: #666; border-bottom: 1px solid #eee; white-space: nowrap; }
        td { padding: 12px; border-bottom: 1px solid #f0f0f0; font-size: 14px; vertical-align: middle; }
        
        .status-badge { padding: 4px 8px; border-radius: 12px; font-size: 12px; font-weight: bold; display: inline-block; }
        .status-0 { background: #fff3cd; color: #856404; } /* 待审核 */
        .status-1 { background: #d4edda; color: #155724; } /* 已通过 */
        .status-2 { background: #f8d7da; color: #721c24; } /* 已拒绝 */
        
        .app-info { display: flex; align-items: center; gap: 10px; }
        .app-info img { width: 40px; height: 40px; border-radius: 6px; object-fit: cover; border: 1px solid #eee; background: #eee; }
        .app-info .name { font-weight: bold; color: #333; }
        .app-info .price { font-size: 12px; color: #888; margin-top: 2px; }
        
        .reject-reason { color: #721c24; background: #f8d7da; padding: 4px 6px; border-radius: 4px; font-size: 12px; margin-top: 4px; display: inline-block; }
        
        .action-links a { 
            display: inline-block; 
            padding: 4px 10px; 
            border-radius: 4px; 
            text-decoration: none; 
            font-size: 12px; 
            margin-right: 5px;
            margin-bottom: 3px;
        }
        .btn-view { background: #007bff; color: white; }
        .btn-view:hover { background: #0056b3; }
        .btn-edit { background: #28a745; color: white; }
        .btn-edit:hover { background: #218838; }
        
        .empty { text-align: center; padding: 50px; color: #999; }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>📝 我的投稿记录</h1>
        <a href="home.php">← 返回个人中心</a>
    </div>
    
    <?php if(mysqli_num_rows($result) > 0): ?>
    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th>软件信息</th>
                    <th>状态</th>
                    <th>下载量</th>
                    <th>投稿时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php while($row = mysqli_fetch_assoc($result)): ?>
                <tr>
                    <td>
                        <div class="app-info">
                            <img src="../<?= htmlspecialchars($row['image']) ?>" onerror="this.src='https://via.placeholder.com/40'">
                            <div>
                                <div class="name"><?= htmlspecialchars($row['name']) ?></div>
                                <div class="price">💰 <?= $row['coin_price'] ?>币 | <?= htmlspecialchars($row['disk_type']) ?></div>
                            </div>
                        </div>
                        <?php if($row['status'] == 2 && !empty($row['reject_reason'])): ?>
                            <div style="margin-top:5px;"><span class="reject-reason">原因：<?= htmlspecialchars($row['reject_reason']) ?></span></div>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php 
                            $status_text = ['待审核', '已通过', '已拒绝'];
                            echo '<span class="status-badge status-'.$row['status'].'">'.$status_text[$row['status']].'</span>';
                        ?>
                    </td>
                    <!-- 【修复】使用 download_count 字段 -->
                    <td style="text-align:center;"><?= intval($row['download_count']) ?> 次</td>
                    <td style="font-size:12px; color:#666;"><?= date('Y-m-d H:i', strtotime($row['created_at'])) ?></td>
                    <td>
                        <div class="action-links">
                            <!-- 【修复】去掉 target="_blank" 让它在当前页打开，这样 yl.php 才能检测到来源并正确返回 -->
                            <a href="../yl.php?id=<?= $row['id'] ?>" class="btn-view">查看</a>
                            <?php if($row['status'] == 2): ?>
                                <!-- 如果被拒绝，显示编辑按钮 -->
                                <a href="../tg.php?edit_id=<?= $row['id'] ?>" class="btn-edit">重新编辑</a>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div class="empty">
        <p>暂无投稿记录</p>
        <a href="../tg.php" style="color:#007bff; text-decoration:none; margin-top:10px; display:inline-block;">去投稿</a>
    </div>
    <?php endif; ?>
</div>
</body>
</html>
