<?php
// Payment Tracking Dashboard
// View all payment attempts, success, failures, and webhook calls

require_once '../config/database.php';

try {
    $pdo = getDBConnection();
    
    // Get all payment tracking records
    $stmt = $pdo->query("
        SELECT 
            id,
            transaction_id,
            merchant_order_id,
            amount,
            status,
            payment_method,
            webhook_called_at,
            created_at,
            updated_at
        FROM payment_tracking 
        ORDER BY created_at DESC 
        LIMIT 50
    ");
    $payments = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Get payment statistics
    $statsStmt = $pdo->query("
        SELECT 
            status,
            COUNT(*) as count,
            SUM(amount) as total_amount
        FROM payment_tracking 
        GROUP BY status
    ");
    $stats = $statsStmt->fetchAll(PDO::FETCH_ASSOC);
    
} catch (Exception $e) {
    die('Database error: ' . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Tracking Dashboard</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .stats { display: flex; gap: 20px; margin-bottom: 30px; }
        .stat-card { background: #f5f5f5; padding: 15px; border-radius: 5px; }
        table { width: 100%; border-collapse: collapse; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .status-success { color: green; font-weight: bold; }
        .status-failed { color: red; font-weight: bold; }
        .status-pending { color: orange; font-weight: bold; }
    </style>
</head>
<body>
    <h1>Payment Tracking Dashboard</h1>
    
    <!-- Statistics -->
    <div class="stats">
        <?php foreach ($stats as $stat): ?>
        <div class="stat-card">
            <h3><?php echo ucfirst($stat['status']); ?></h3>
            <p>Count: <?php echo $stat['count']; ?></p>
            <p>Amount: ₹<?php echo number_format($stat['total_amount'], 2); ?></p>
        </div>
        <?php endforeach; ?>
    </div>
    
    <!-- Payment Records -->
    <h2>Recent Payment Records</h2>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Transaction ID</th>
                <th>Merchant Order ID</th>
                <th>Amount</th>
                <th>Status</th>
                <th>Payment Method</th>
                <th>Webhook Called</th>
                <th>Created At</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($payments as $payment): ?>
            <tr>
                <td><?php echo $payment['id']; ?></td>
                <td><?php echo $payment['transaction_id']; ?></td>
                <td><?php echo $payment['merchant_order_id']; ?></td>
                <td>₹<?php echo number_format($payment['amount'], 2); ?></td>
                <td class="status-<?php echo strtolower($payment['status']); ?>">
                    <?php echo $payment['status']; ?>
                </td>
                <td><?php echo $payment['payment_method']; ?></td>
                <td><?php echo $payment['webhook_called_at'] ? 'Yes' : 'No'; ?></td>
                <td><?php echo $payment['created_at']; ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    
    <script>
        // Auto refresh every 30 seconds
        setTimeout(() => {
            location.reload();
        }, 30000);
    </script>
</body>
</html>
