<?php
// --- SETTINGS BRIDGE ---
$settings_file = 'theme_settings.json';
$config = file_exists($settings_file) ? json_decode(file_get_contents($settings_file), true) : ['blur' => 12, 'hue' => 120];

$dynamic_hue = $_GET['hue'] ?? $config['hue'];
$dynamic_blur = $_GET['blur'] ?? $config['blur'];

$debug_msg = "";
$transactions = [];

try {
    $db = new PDO('sqlite:app_feed.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Check if table exists
    $tableExists = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='momo_transactions'");
    if (!$tableExists->fetch()) {
        $debug_msg = "Database Error: Table 'momo_transactions' does not exist.";
    } else {
        $transactions = $db->query("SELECT * FROM momo_transactions ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
        if (empty($transactions)) {
            $debug_msg = "Table exists, but contains no records.";
        }
    }
} catch (PDOException $e) {
    $debug_msg = "Connection Error: " . $e->getMessage();
}

function getPhone($row) {
    return $row['phone'] ?? $row['user_phone'] ?? $row['phone_number'] ?? 'N/A';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        :root { --blur: <?php echo $dynamic_blur; ?>px; --hue: <?php echo $dynamic_hue; ?>; }
        @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;800&display=swap');
        body { font-family: 'Plus Jakarta Sans', sans-serif; background: #080b11; min-height: 100vh; padding: 40px 20px; margin: 0; filter: hue-rotate(calc(var(--hue) * 1deg)); }
        
        .container { width: 100%; max-width: 950px; margin: auto; background: rgba(15, 22, 36, 0.6); backdrop-filter: blur(var(--blur)); padding: 32px; border-radius: 24px; border: 1px solid rgba(16, 185, 129, 0.4); box-shadow: 0 10px 25px rgba(0,0,0,0.5); }
        h2 { color: #10b981; margin-bottom: 25px; font-size: 24px; text-align: center; }
        
        table { width: 100%; border-collapse: separate; border-spacing: 0 10px; }
        th { color: #9ca3af; font-size: 11px; text-transform: uppercase; padding: 15px; text-align: left; letter-spacing: 1px; }
        
        td { padding: 18px; background: rgba(0,0,0,0.2); border-top: 1px solid rgba(255, 255, 255, 0.03); border-bottom: 1px solid rgba(255, 255, 255, 0.03); color: #ffffff; }
        
        .status-pill { padding: 5px 12px; border-radius: 20px; font-size: 10px; font-weight: 700; background: rgba(16, 185, 129, 0.2); color: #10b981; }
        .method-pill { background: rgba(59, 130, 246, 0.2); color: #60a5fa; }
        .btn-back { display: block; width: 150px; margin: 20px auto 0; padding: 12px; background: #10b981; color: white; text-align: center; text-decoration: none; border-radius: 8px; font-weight: 800; }
        .debug-box { color: #f87171; background: rgba(239, 68, 68, 0.1); padding: 10px; border-radius: 8px; margin-bottom: 15px; font-size: 12px; text-align: center; }
    </style>
</head>
<body>

<div class="container">
    <h2>Transaction History</h2>
    
    <?php if (!empty($debug_msg)): ?>
        <div class="debug-box"><?php echo $debug_msg; ?></div>
    <?php endif; ?>

    <table>
        <thead>
            <tr>
                <th>Date</th>
                <th>Phone</th>
                <th>Amount</th>
                <th>Method</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($transactions)): ?>
                <tr><td colspan="5" style="text-align:center; color: #9ca3af;">No records found.</td></tr>
            <?php else: foreach ($transactions as $row): ?>
            <tr>
                <td><?= htmlspecialchars($row['created_at'] ?? 'N/A') ?></td>
                <td><?= htmlspecialchars(getPhone($row)) ?></td>
                <td>$<?= number_format((float)($row['amount'] ?? 0), 2) ?></td>
                <td><span class="status-pill method-pill"><?= htmlspecialchars($row['payment_method'] ?? 'MoMo') ?></span></td>
                <td><span class="status-pill"><?= htmlspecialchars($row['status'] ?? 'Pending') ?></span></td>
            </tr>
            <?php endforeach; endif; ?>
        </tbody>
    </table>
    <a href="cdashboard.php" class="btn-back">Back to Dashboard</a>
</div>

</body>
</html>