<?php

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

if(!isset($_GET['subscription'])){
    header("Location: plans.php");
    exit();
}

$subscriptionId = (int)$_GET['subscription'];

/*
|--------------------------------------------------------------------------
| Member Information
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT
u.fullname,
u.phone,
p.plan_name,
s.hands
FROM subscriptions s
INNER JOIN users u ON s.user_id=u.id
INNER JOIN plans p ON s.plan_id=p.id
WHERE s.id=?
LIMIT 1
");

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

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

if(!$member){
    die("Subscription not found.");
}

/*
|--------------------------------------------------------------------------
| Contribution History
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT
contribution_no,
amount,
due_date,
paid_date,
status
FROM contributions
WHERE subscription_id=?
ORDER BY contribution_no ASC
");

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

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

?>

<!DOCTYPE html>
<html>

<head>

<meta charset="UTF-8">

<title>Contribution History</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><?= htmlspecialchars($member['fullname']); ?></h2>

<p>

<?= htmlspecialchars($member['plan_name']); ?>

•

<?= $member['hands']; ?> Hand(s)

</p>

</div>

<div class="table-card">

<table class="member-table">

<thead>

<tr>

<th>No</th>

<th>Amount</th>

<th>Due Date</th>

<th>Paid Date</th>

<th>Status</th>

</tr>

</thead>

<tbody>

<?php foreach($contributions as $c): ?>

<tr>

<td><?= $c['contribution_no']; ?></td>

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

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

<td>

<?= $c['paid_date']
? date("d M Y", strtotime($c['paid_date']))
: "-"; ?>

</td>

<td>

<?php

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

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

}elseif($c['status']=="pending"){

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

}else{

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

}

?>

</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

</div>

</div>

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

</body>

</html>