<?php
require 'includes/db.php';
require 'includes/auth.php';

$user_id = $_SESSION['user_id'];

// Fetch user details
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// Fetch Pay-In Transactions
$stmt = $pdo->prepare("SELECT * FROM transactions WHERE user_id = ? AND type = 'payin' ORDER BY id DESC");
$stmt->execute([$user_id]);
$payins = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Pay-In History</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body {
            background-color: #f4f6f9;
            font-family: 'Segoe UI', sans-serif;
        }
        .table thead {
            background-color: #343a40;
            color: #fff;
        }
    </style>
</head>
<body class="p-1">
    <?php include 'menucompass.php';?>
    <div class="container">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h3><i class="bi bi-arrow-down-circle"></i> Pay-In Transactions</h3>
            <a href="dashboard.php" class="btn btn-outline-secondary"><i class="bi bi-arrow-left-circle"></i> Back to Dashboard</a>
        </div>
<div class="alert alert-info d-flex align-items-center" role="alert">
    <svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img" aria-label="Info:"><use xlink:href="#info-fill"/></svg>
    <div>
        <strong>Note:</strong> Balance is updated at night based on daily transactions and settlements made around midnight.
    </div>
</div>

        <?php if (count($payins) === 0): ?>
            <div class="alert alert-warning">No Pay-In transactions found.</div>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-bordered table-hover align-middle">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Amount</th>
                            <th>Fee</th>
                            <th>Pay Type</th>
                            <th>Tarnsaction ID</th>
                            <th>Date</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($payins as $txn): ?>
                        <tr>
                            <td><?= $txn['id'] ?></td>
                            <td><?= $user['currency'] ?> <?= number_format($txn['amount'], 2) ?></td>
                            <td><?= $user['currency'] ?> <?= number_format($txn['fee'], 2) ?></td>
                            <td><?= htmlspecialchars($txn['payment_gateway']) ?></td>
                            <td>
    <?= htmlspecialchars($txn['reference'] ?? $txn['txn_id'] ?? '') ?>
</td>

                            <td><?= $txn['created_at'] ?></td>
                            
        <td>
                                <?php
                                    $statusClass = 'secondary';
                                    if ($txn['status'] === 'success') $statusClass = 'success';
                                    elseif ($txn['status'] === 'pending') $statusClass = 'warning';
                                    elseif ($txn['status'] === 'failed') $statusClass = 'danger';
                                ?>
                                <span class="badge bg-<?= $statusClass ?>"><?= ucfirst($txn['status']) ?></span>
                            </td>
    
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>
