<?php

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

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

// Total Collected
$stmt = $pdo->query("
SELECT IFNULL(SUM(amount),0) total
FROM wallet_transactions
WHERE transaction_type='deduction'
");

$totalCollected = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Due Today
$today = date("Y-m-d");

$stmt = $pdo->prepare("
SELECT IFNULL(SUM(amount),0) total
FROM contributions
WHERE due_date=?
AND status='pending'
");

$stmt->execute([$today]);

$dueToday = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Overdue
$stmt = $pdo->query("
SELECT IFNULL(SUM(amount),0) total
FROM contributions
WHERE status='overdue'
");

$overdue = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// Active Subscribers
$stmt = $pdo->query("
SELECT COUNT(*) total
FROM subscriptions
WHERE status='active'
");

$activeSubscribers = $stmt->fetchColumn();


/*
|--------------------------------------------------------------------------
| Filters
|--------------------------------------------------------------------------
*/

$search = $_GET['search'] ?? '';
$status = $_GET['status'] ?? '';

$sql = "
SELECT
c.*,
u.fullname,
p.plan_name
FROM contributions c
INNER JOIN subscriptions s
ON c.subscription_id=s.id
INNER JOIN users u
ON s.user_id=u.id
INNER JOIN plans p
ON s.plan_id=p.id
WHERE 1
";

$params = [];

if($search!=''){

    $sql .= " AND (u.fullname LIKE ? OR p.plan_name LIKE ?)";

    $params[]="%$search%";
    $params[]="%$search%";

}

if($status!=''){

    $sql .= " AND c.status=?";

    $params[]=$status;

}

$sql .= " ORDER BY c.id DESC LIMIT 20";

$stmt=$pdo->prepare($sql);

$stmt->execute($params);

$recentContributions=$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>Contribution 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>

<i class="fas fa-money-check-dollar"></i>

Contribution Dashboard

</h2>

<p>

Monitor contributions, collections and overdue payments.

</p>

</div>

<div class="stats-grid">

    <div class="stat-card">

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

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

        <p>Total Collected</p>

    </div>

    <div class="stat-card">

        <i class="fas fa-calendar-day"></i>

        <h3>₦<?= number_format($dueToday,2); ?></h3>

        <p>Due Today</p>

    </div>

    <div class="stat-card">

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

        <h3>₦<?= number_format($overdue,2); ?></h3>

        <p>Overdue</p>

    </div>

    <div class="stat-card">

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

        <h3><?= $activeSubscribers; ?></h3>

        <p>Active Subscribers</p>

    </div>



</div>

<form class="filter-bar" method="GET">

<input
type="text"
name="search"
placeholder="Search member or scheme..."
value="<?= htmlspecialchars($search); ?>">

<select name="status">

<option value="">All Status</option>

<option value="paid" <?= $status=="paid"?'selected':''; ?>>Paid</option>

<option value="pending" <?= $status=="pending"?'selected':''; ?>>Pending</option>

<option value="overdue" <?= $status=="overdue"?'selected':''; ?>>Overdue</option>

</select>

<button type="submit">

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

Search

</button>

</form>

<div class="tables-card">

    <div class="tables-header">

        <h3>

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

            Recent Contributions

        </h3>

    </div>

    <div class="tables-responsive">

        <table class="admin-tables">

            <thead>

                <tr>

                    <th>Member</th>

                    <th>Scheme</th>

                    <th>Contribution</th>

                    <th>Due Date</th>

                    <th>Status</th>


                </tr>

            </thead>

            <tbody>

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

            <?php foreach($recentContributions as $row): ?>

                <tr>

                    <td><?= htmlspecialchars($row['fullname']); ?></td>

                    <td><?= htmlspecialchars($row['plan_name']); ?></td>

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

                    <td><?= date("d M Y",strtotime($row['due_date'])); ?></td>

                    <td>

<?php

if($row['status']=="paid"){

    echo "<span class='badge-paid'>Paid</span>";

}elseif($row['status']=="overdue"){

    echo "<span class='badge-overdue'>Overdue</span>";

}else{

    echo "<span class='badge-pending'>Pending</span>";

}

?>

</td>


</tr>

            <?php endforeach; ?>

            <?php else: ?>

                <tr>

                    <td colspan="5">

                        No contributions found.

                    </td>

                </tr>

            <?php endif; ?>

            </tbody>

        </table>

    </div>

</div>

</div>



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

</body>

</html>