<?php
require_once __DIR__ . '/config.php';

// 鉴权：需要登录才能访问
session_start();
if (!isset($_SESSION['user_id'])) {
    http_response_code(403);
    die('请先<a href="login.php">登录</a>后再访问数据库查看器。');
}

// 表名白名单
$allowedTables = [
    'budget_items', 'custom_categories', 'custom_details', 'dating_profiles',
    'invitations', 'invitation_templates', 'testimonials', 'users', 'vendors',
    'wedding_checklist', 'wedding_gallery', 'video_assets', 'wedding_phase_videos'
];
?>
<!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 { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }
        h1 { color: #333; text-align: center; }
        .container { max-width: 1200px; margin: 0 auto; }
        .table-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 15px; margin-bottom: 30px; }
        .table-card { background: white; padding: 15px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); cursor: pointer; transition: transform 0.2s; }
        .table-card:hover { transform: translateY(-2px); }
        .table-card h3 { margin: 0 0 10px 0; color: #2c3e50; }
        .table-card .count { font-size: 24px; color: #3498db; font-weight: bold; }
        .data-table { width: 100%; border-collapse: collapse; background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .data-table th, .data-table td { padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }
        .data-table th { background: #3498db; color: white; }
        .data-table tr:hover { background: #f9f9f9; }
        .back-btn { display: inline-block; padding: 10px 20px; background: #3498db; color: white; text-decoration: none; border-radius: 4px; margin-bottom: 20px; }
        .back-btn:hover { background: #2980b9; }
        .empty { text-align: center; padding: 40px; color: #999; }
    </style>
</head>
<body>
    <div class="container">
        <h1>SQLite 数据库查看器</h1>
        
        <?php if (!isset($_GET['table'])): ?>
            <div class="table-list">
                <?php
                $tables = $pdo->query("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
                while ($row = $tables->fetch(PDO::FETCH_ASSOC)) {
                    $tableName = $row['name'];
                    if ($tableName == 'sqlite_sequence') continue;
                    // 仅显示白名单内的表
                    if (!in_array($tableName, $allowedTables)) continue;
                    $countStmt = $pdo->query("SELECT COUNT(*) as cnt FROM `$tableName`");
                    $count = $countStmt->fetchColumn();
                    ?>
                    <div class="table-card" onclick="location.href='?table=<?= urlencode($tableName) ?>'">
                        <h3><?= htmlspecialchars($tableName) ?></h3>
                        <div class="count"><?= $count ?></div>
                        <div style="font-size: 12px; color: #666;">条记录</div>
                    </div>
                    <?php
                }
                ?>
            </div>
        <?php else: ?>
            <?php
            $tableName = $_GET['table'];
            // 验证表名在白名单内
            if (!in_array($tableName, $allowedTables)) {
                http_response_code(403);
                echo '<div class="empty">不允许访问该表</div>';
                echo '</div></body></html>';
                exit;
            }
            ?>
            <a href="db_viewer.php" class="back-btn">← 返回表列表</a>
            <h2><?= htmlspecialchars($tableName) ?></h2>
            
            <?php
            $stmt = $pdo->query("SELECT * FROM `$tableName`");
            $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            if (empty($rows)) {
                echo '<div class="empty">该表暂无数据</div>';
            } else {
                $columns = array_keys($rows[0]);
                ?>
                <table class="data-table">
                    <thead>
                        <tr>
                            <?php foreach ($columns as $col): ?>
                                <th><?= htmlspecialchars($col) ?></th>
                            <?php endforeach; ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($rows as $row): ?>
                            <tr>
                                <?php foreach ($columns as $col): ?>
                                    <td><?= htmlspecialchars($row[$col] ?? '') ?></td>
                                <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                <?php
            }
            ?>
        <?php endif; ?>
    </div>
</body>
</html>
