<?php

include "../includes/admin_auth.php";

include "../config/database.php";

/*
|--------------------------------------------------------------------------
| Dashboard Statistics
|--------------------------------------------------------------------------
*/

// Total Members
$totalMembers = $pdo->query("
SELECT COUNT(*)
FROM users
")->fetchColumn();

// Active Schemes
$activeSchemes = $pdo->query("
SELECT COUNT(*)
FROM plans
WHERE status='active'
")->fetchColumn();

// Active Subscriptions
$activeSubscriptions = $pdo->query("
SELECT COUNT(*)
FROM subscriptions
WHERE status='active'
")->fetchColumn();

// Total Money Collected
$totalCollected = $pdo->query("
SELECT IFNULL(SUM(amount),0)
FROM wallet_transactions
WHERE transaction_type='deduction'
AND status='successful'
")->fetchColumn();

/*
|--------------------------------------------------------------------------
| Quick Alerts
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| Overdue This Month
|--------------------------------------------------------------------------
*/

$currentMonth = date("n");
$currentYear  = date("Y");

$stmt = $pdo->prepare("
SELECT

COUNT(*) total,

IFNULL(SUM(amount),0) amount

FROM contributions

WHERE status='overdue'

AND MONTH(due_date)=?

AND YEAR(due_date)=?
");

$stmt->execute([$currentMonth,$currentYear]);

$overdueData = $stmt->fetch(PDO::FETCH_ASSOC);



/*
|--------------------------------------------------------------------------
| Pending This Month
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT

COUNT(*) total,

IFNULL(SUM(amount),0) amount

FROM contributions

WHERE status='pending'

AND MONTH(due_date)=?

AND YEAR(due_date)=?
");

$stmt->execute([$currentMonth,$currentYear]);

$pendingData = $stmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| Recent Activity
|--------------------------------------------------------------------------
*/

$stmt = $pdo->query("
SELECT
wt.*,
u.fullname
FROM wallet_transactions wt
INNER JOIN users u
ON wt.user_id=u.id
WHERE wt.status='successful'
ORDER BY wt.id DESC
LIMIT 10
");

$activities = $stmt->fetchAll(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| Top 3 Most Active Schemes
|--------------------------------------------------------------------------
*/

$stmt = $pdo->query("

SELECT

p.id,
p.plan_name,
p.amount,
p.duration,

IFNULL(sub.members,0) AS members,
IFNULL(sub.hands,0) AS hands,

IFNULL(fin.collected,0) AS collected

FROM plans p

LEFT JOIN (

    SELECT
        plan_id,
        COUNT(*) AS members,
        SUM(hands) AS hands

    FROM subscriptions

    WHERE status='active'

    GROUP BY plan_id

) sub

ON p.id=sub.plan_id

LEFT JOIN (

    SELECT

        s.plan_id,

        SUM(
            CASE
            WHEN c.status='paid'
            AND MONTH(c.created_at)=MONTH(CURDATE())
            AND YEAR(c.created_at)=YEAR(CURDATE())
            THEN c.amount
            ELSE 0
            END
        ) AS collected

    FROM subscriptions s

    LEFT JOIN contributions c
    ON s.id=c.subscription_id

    GROUP BY s.plan_id

) fin

ON p.id=fin.plan_id

WHERE p.status='active'

ORDER BY collected DESC, members DESC

LIMIT 3

");

$topSchemes = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Admin Dashboard</title>

<link rel="stylesheet" href="../assets/css/admin.css">
<link rel="stylesheet" href="../assets/css/responsive.css">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">

</head>

<body>

<?php include "../includes/admin_sidebar.php"; ?>

<div class="sidebar-overlay"></div>

<?php include "../includes/admin_topbar.php"; ?>

<div class="main-content">

<div class="page-header">

<h2>

Welcome back,

<?= htmlspecialchars($_SESSION['fullname']); ?>

👋

</h2>

<p>

Complete Bliss Administration Dashboard

</p>

</div>

<div class="stats-grid">

<div class="stat-card">

<i class="fas fa-users"></i>

<h3><?= number_format($totalMembers); ?></h3>

<p>Total Members</p>

</div>

<div class="stat-card">

<i class="fas fa-layer-group"></i>

<h3><?= number_format($activeSchemes); ?></h3>

<p>Active Schemes</p>

</div>

<div class="stat-card">

<i class="fas fa-file-signature"></i>

<h3><?= number_format($activeSubscriptions); ?></h3>

<p>Active Subscriptions</p>

</div>

<div class="stat-card">

<i class="fas fa-money-bill-wave"></i>

<h3>

₦<?= number_format($totalCollected,2); ?>

</h3>

<p>Total Collected</p>

</div>

</div>

<div class="dashboard-section">

<div class="section-title">

<i class="fas fa-triangle-exclamation"></i>

<h3>Quick Alerts</h3>

</div>

<div class="stats-grid">

<div class="stat-card warning">

<i class="fas fa-clock"></i>

<h3><?= $overdueData['total']; ?></h3>

<p>Overdue Contributions</p>

<small>

₦<?= number_format($overdueData['amount'],2); ?>

Remaining

</small>

</div>

<div class="stat-card info">

<i class="fas fa-hourglass-half"></i>

<h3><?= $pendingData['total']; ?></h3>

<p>Pending This Month</p>

<small>

₦<?= number_format($pendingData['amount'],2); ?>

Remaining

</small>

</div>

</div>

</div>

<div class="dashboard-section">

<div class="section-title">

<i class="fas fa-clock-rotate-left"></i>

<h3>Recent Activity</h3>

</div>

<div class="activity-card">

<?php if(count($activities)>0): ?>

<?php foreach($activities as $activity): ?>

<div class="activity-row">

<div class="activity-icon">

<?php

if($activity['transaction_type']=="deduction"){

echo "<i class='fas fa-arrow-up'></i>";

}else{

echo "<i class='fas fa-wallet'></i>";

}

?>

</div>

<div class="activity-details">

<strong>

<?= htmlspecialchars($activity['fullname']); ?>

</strong>

<br>

<small>

<?php

if($activity['transaction_type']=="deduction"){

echo "Contribution Payment";

}else{

echo "Wallet Funding";

}

?>

<br>

<?php

$date = strtotime($activity['created_at']);

if(date("Y-m-d",$date)==date("Y-m-d")){

echo "Today • ".date("h:i A",$date);

}elseif(date("Y-m-d",$date)==date("Y-m-d",strtotime("-1 day"))){

echo "Yesterday • ".date("h:i A",$date);

}else{

echo date("d M Y • h:i A",$date);

}

?>

</small>

</div>

<div class="activity-amount <?= $activity['transaction_type']=="deduction" ? "money-out" : "money-in"; ?>">

<?= $activity['transaction_type']=="deduction" ? "-" : "+"; ?>

₦<?= number_format($activity['amount'],2); ?>

</div>

</div>

<?php endforeach; ?>

<?php else: ?>

<p>No recent activity found.</p>

<?php endif; ?>

</div>

</div>

<div class="dashboard-section">

<div class="section-title">

<i class="fas fa-layer-group"></i>

<h3>Most Active Schemes</h3>

</div>

<div class="scheme-grid">

<?php foreach($topSchemes as $scheme): ?>

    <?php



?>

<div class="scheme-card">

<h3><?= htmlspecialchars($scheme['plan_name']); ?></h3>

<p>

₦<?= number_format($scheme['amount'],2); ?>

•

<?= $scheme['duration']; ?>

</p>

<div class="scheme-stats">

<div>

<strong><?= $scheme['members']; ?></strong>

<span>Members</span>

</div>

<div>

<strong><?= $scheme['hands']; ?></strong>

<span>Hands</span>

</div>

</div>

<div class="scheme-money">

<div>

Total Collected

<strong>

₦<?= number_format($scheme['collected'],2); ?>

</strong>

</div>

</div>

<a href="scheme_members.php?plan=<?= $scheme['id']; ?>" class="scheme-btn">

View Members

</a>

</div>

<?php endforeach; ?>

</div>

</div>

</div>

<script src="../assets/js/admin.js"></script>

</body>

</html>