<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!isset($_SESSION['username'])) {
    $_SESSION['username'] = "Guest"; 
}

require 'config.php'; 
include 'includes/header.php'; 
include 'includes/admin-sidebar.php'; 

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'super-admin') {
    die("Access Denied: You are not authorized to view this page.");
}

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

// Fetch Super-Admin details
$stmt = $conn->prepare("SELECT username, balance FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
$username = htmlspecialchars($user['username']);
$super_admin_balance = $user['balance'];

// Fetch deposit stats
$stmt = $conn->prepare("SELECT SUM(amount) AS total_deposit FROM tbl_deposits");
$stmt->execute();
$total_deposits = $stmt->get_result()->fetch_assoc()['total_deposit'] ?? 0;
$stmt->close();

$stmt = $conn->prepare("SELECT SUM(amount) AS last_month_deposit FROM tbl_deposits WHERE created_at >= CURDATE() - INTERVAL 1 MONTH");
$stmt->execute();
$last_month_deposits = $stmt->get_result()->fetch_assoc()['last_month_deposit'] ?? 0;
$stmt->close();

$stmt = $conn->prepare("SELECT SUM(amount) AS this_month_deposit FROM tbl_deposits WHERE MONTH(created_at) = MONTH(CURRENT_DATE()) AND YEAR(created_at) = YEAR(CURRENT_DATE())");
$stmt->execute();
$this_month_deposits = $stmt->get_result()->fetch_assoc()['this_month_deposit'] ?? 0;
$stmt->close();

// Fetch transactions
$stmt = $conn->prepare("SELECT d.amount, d.status, d.created_at, u.username, d.phone, d.email, d.transaction_id 
                        FROM tbl_deposits d 
                        JOIN users u ON d.user_id = u.id 
                        ORDER BY d.created_at DESC 
                        LIMIT ? OFFSET ?");
$stmt->bind_param("ii", $limit, $offset);
$stmt->execute();
$transactions = $stmt->get_result();
$stmt->close();

// Total records for pagination
$result = $conn->query("SELECT COUNT(*) AS total FROM tbl_deposits");
$total_records = $result->fetch_assoc()['total'];
$total_pages = ceil($total_records / $limit);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Super-Admin Dashboard</title>
    <link rel="stylesheet" href="includes/css/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body { font-family: Arial, sans-serif; background-color: #f8f9fa; }
        .card.current { background-color: #d1e7dd; }
        .card.last { background-color: #ffeee6; }
        .card.total { background-color: #ecf1ff; }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-10 content">
                    <div class="d-flex justify-content-between mb-4">
                        <a href="deposit-admin.php" class="btn btn-primary">Make a Deposit</a>
                    </div>

                    <div class="row">
                        <div class="col-md-4">
                            <div class="card card last p-3">
                                <h5>Last Month Deposits</h5>
                                <h3>GHS <?php echo number_format($last_month_deposits, 2); ?></h3>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card card current p-3">
                                <h5>This Month Deposits</h5>
                                <h3>GHS <?php echo number_format($this_month_deposits, 2); ?></h3>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card card total p-3">
                                <h5>Total Deposits</h5>
                                <h3>GHS <?php echo number_format($total_deposits, 2); ?></h3>
                            </div>
                        </div>
                    </div>

                    <h4 class="mt-4">Deposits History</h4>
                    <table class="table table-bordered">
                        <thead>
                            <tr><th>Date</th><th>User</th><th>Phone</th><th>Email</th><th>Transaction ID</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><?php echo htmlspecialchars($row['username']); ?></td>
                                    <td><?php echo htmlspecialchars($row['phone']); ?></td>
                                    <td><?php echo htmlspecialchars($row['email']); ?></td>
                                    <td><?php echo htmlspecialchars($row['transaction_id']); ?></td>
                                    <td>GHS <?php echo number_format($row['amount'], 2); ?></td>
                                    <td><?php echo ucfirst($row['status']); ?></td>
                                </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>

                    <nav>
                        <ul class="pagination">
                            <?php
                            $max_pages_to_show = 6;
                            $start = max(1, $page - floor($max_pages_to_show / 2));
                            $end = min($start + $max_pages_to_show - 1, $total_pages);

                            if ($start > 1): ?>
                                <li class="page-item"><a class="page-link" href="?page=1">1</a></li>
                                <li class="page-item disabled"><span class="page-link">...</span></li>
                            <?php endif; ?>

                            <?php for ($i = $start; $i <= $end; $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 ($end < $total_pages): ?>
                                <li class="page-item disabled"><span class="page-link">...</span></li>
                                <li class="page-item"><a class="page-link" href="?page=<?php echo $total_pages; ?>"><?php echo $total_pages; ?></a></li>
                            <?php endif; ?>

                            <?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>
    </div>
</div>
</body>
</html>
