<?php
session_start();
include 'config.php';
include 'includes/header.php';
include 'includes/admin-sidebar.php';

// Ensure the user is Super-Admin
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'super-admin') {
    die("Access Denied");
}

$user_id = $_SESSION['user_id'];
$limit = 20;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Fetch transactions (union)
$query = "
    (SELECT created_at, 'Deposit' AS type, amount, status 
     FROM tbl_deposits 
     WHERE user_id = ?)
    UNION ALL
    (SELECT created_at, 'Withdrawal' AS type, amount, status 
     FROM tbl_withdrawals 
     WHERE user_id = ?)
    UNION ALL
    (SELECT created_at, 'Transfer Sent' AS type, amount, status 
     FROM tbl_payouts 
     WHERE admin_id = ?)
    UNION ALL
    (SELECT created_at, 'Transfer Received' AS type, amount, status 
     FROM tbl_payouts 
     WHERE recipient_name = (SELECT username FROM users WHERE id = ?))
    ORDER BY created_at DESC
    LIMIT ? OFFSET ?
";
$stmt = $conn->prepare($query);
$stmt->bind_param("iiiiii", $user_id, $user_id, $user_id, $user_id, $limit, $offset);
$stmt->execute();
$transactions = $stmt->get_result();

// Get total count
$count_query = "
    SELECT COUNT(*) AS total FROM (
        (SELECT created_at FROM tbl_deposits WHERE user_id = ?)
        UNION ALL
        (SELECT created_at FROM tbl_withdrawals WHERE user_id = ?)
        UNION ALL
        (SELECT created_at FROM tbl_payouts WHERE admin_id = ?)
        UNION ALL
        (SELECT created_at FROM tbl_payouts WHERE recipient_name = (SELECT username FROM users WHERE id = ?))
    ) AS combined
";
$stmt = $conn->prepare($count_query);
$stmt->bind_param("iiii", $user_id, $user_id, $user_id, $user_id);
$stmt->execute();
$total_result = $stmt->get_result()->fetch_assoc();
$total_transactions = $total_result['total'];
$total_pages = ceil($total_transactions / $limit);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Super-Admin Transaction History</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="includes/css/style.css">
</head>
<body>

        <!-- Sidebar (Already included earlier but now inside layout) -->
        <div class="col-md-3">
            <?php include 'includes/admin-sidebar.php'; ?>
        </div>
        <div class="container-fluid">
    <div class="row">
        <div class="col-md-10 content">
            <div class="row">
            <div class="table-container mt-4">
        <!-- Main Content Area -->
            <h3>Transaction History</h3>

            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Amount</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $transactions->fetch_assoc()): ?>
                            <tr>
                                <td><?php echo $row['created_at']; ?></td>
                                <td>
                                    <span class="btn btn-sm
                                        <?php echo ($row['type'] === 'Deposit') ? 'btn-success' :
                                                   (($row['type'] === 'Withdrawal') ? 'btn-danger' :
                                                   (($row['type'] === 'Transfer Sent') ? 'btn-warning' : 'btn-info')); ?>">
                                        <?php echo $row['type']; ?>
                                    </span>
                                </td>
                                <td>GHS <?php echo number_format($row['amount'], 2); ?></td>
                                <td><?php echo ucfirst($row['status']); ?></td>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            <nav class="d-flex justify-content-center">
                <ul class="pagination">
                    <?php if ($page > 1): ?>
                        <li class="page-item"><a class="page-link" href="?page=<?php echo $page - 1; ?>">Previous</a></li>
                    <?php endif; ?>

                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="page-item <?php echo ($i === $page) ? 'active' : ''; ?>">
                            <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                        </li>
                    <?php endfor; ?>

                    <?php if ($page < $total_pages): ?>
                        <li class="page-item"><a class="page-link" href="?page=<?php echo $page + 1; ?>">Next</a></li>
                    <?php endif; ?>
                </ul>
            </nav>
        </div>
    </div>
</div>

</body>
</html>
